0.0
The project is in a healthy, maintained state
Declare typed attributes with cprop: values are converted, transformed, defaulted or frozen on assignment, with JSON import/export.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 2.0
 Project Readme

Philosophal

If you don't want to manage your parameters' conversation before valuing your objects' instance variables, Philosophal is the gem for you ;-)

Installation

gem install philosophal

Usage

require 'philosophal'

class Person
  extend Philosophal::Properties

  cprop :first_name, String, transform: :downcase
  cprop :last_name, String, transform: :upcase
  cprop :age, Integer
end

maxime = Person.new
maxime.first_name = 'Maxime'
maxime.last_name = 'Désécot'
maxime.age = '40'
puts maxime.inspect
#<Person:0x00007da7088eed88 @first_name="maxime", @last_name="DÉSÉCOT", @age=40>

Tracking changes

With track_changes: true, a property also gets <name>_was and <name>_changed?. Call changes_applied once the object is in a known state (built, loaded, saved...): <name>_was returns the value at that moment, and <name>_changed? tells whether the value differs from it.

class Article
  extend Philosophal::Properties

  cprop :title, String, track_changes: true
end

article = Article.new
article.title = 'Draft'
article.title_changed? # => true (no reference yet)
article.changes_applied
article.title_changed? # => false
article.title = 'Review'
article.title = 'Final'
article.title_was      # => "Draft"
article.title_changed? # => true
article.title = 'Draft'
article.title_changed? # => false

changed_properties returns the tracked properties that changed, with their new value:

article.title = 'Final'
article.changed_properties # => { title: "Final" }
article.changes_applied
article.changed_properties # => {}

Before the first changes_applied, the reference is "never assigned": every assigned property is changed. <name>_was returns the stored value, without falling back to the default. json.load does not call changes_applied: call it on the loaded objects if they must start unchanged.