README.textile in dirty_hashy-0.1.1 vs README.textile in dirty_hashy-0.1.2

- old
+ new

@@ -2,11 +2,11 @@ Dirty tracking within hashes with indifferent access or objects as it is expected to be! h2. Introduction -"Dirty tracking / objects":http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects is a common programming concept. In short, it is the mechanism to track whether or not the attributes of an object have been changed and if so, which ones. +"Dirty tracking / objects":http://ryandaigle.com/articles/2008/3/31/what-s-new-in-edge-rails-dirty-objects is a common programming concept. In short, it is the concept of tracking whether or not the attributes of an object have been changed and if so, which ones. It is mostly implemented within ORM's, a couple of examples in the Ruby world are "ActiveRecord":http://ar.rubyonrails.org/classes/ActiveRecord/Dirty.html, "DataMapper":http://rubydoc.info/gems/dm-core/1.1.0/file/README.rdoc, "Mongoid":http://mongoid.org/docs/documents/dirty.html and "CouchRest Model":http://www.couchrest.info/model/dirty_tracking.html. Ironically, I haven't found a gem suited for the simple desire of dirty tracking within Ruby hashes. Eventually, you can compare attributes of an ORM object with a Hash containing values. @@ -86,14 +86,41 @@ h.name = "Paul" h.dirty? #=> false h.name = "Engel" h.name_was #=> "Paul" h.name_change #=> ["Paul", "Engel"] - h.foo = "Bar" - h.changes #=> {"name"=>["Paul", "Engel"], "foo"=>[nil, "Bar"]} + h.foo = "bar" + h.changes #=> {"name"=>["Paul", "Engel"], "foo"=>[nil, "bar"]} </pre> +h3. Method mapping DirtyHashy with key restriction + +Along with providing convenience methods, you can also restrict the range of keys you are permitted to read / write / merge / replace of a DirtyHash: + +<pre> + require "rubygems" + require "dirty_hashy" + + h = DirtyHashy.new({}, true, [:name]) + h.dirty? #=> false + h.name #=> nil + h.name = "Paul" + h.dirty? #=> true + h.name_changed? #=> true + h.name_was #=> nil + h.name_change #=> [nil, "Paul"] + h.merge! :name => "Engel" + h.name #=> "Engel" + h.name_was #=> nil + h.name_change #=> [nil, "Engel"] + h.foo #=> NoMethodError: undefined method `foo' for {"name"=>"Engel"}:DirtyHashy + h.foo = "bar" #=> NoMethodError: undefined method `foo=' for {"name"=>"Engel"}:DirtyHashy + h.clean_up! + h.replace :name => "Paul" + h.changes #=> {"name"=>["Engel", "Paul"]} +</pre> + h3. Dirty tracking objects (models) Like "ActiveModel::Dirty":http://api.rubyonrails.org/classes/ActiveModel/Dirty.html, you can use "DirtyAttributes":https://github.com/archan937/dirty_hashy/blob/master/lib/dirty_attributes.rb to dirty track your own objects (models). But there are two differences: # Setting up @DirtyAttributes@ is easier to setup than @ActiveModel::Dirty@ @@ -157,9 +184,10 @@ p.name_was #=> nil p.name_change #=> [nil, "Paul"] p.clean_up! p.dirty? #=> false p.name #=> "Paul" + p.foo = "bar" #=> NoMethodError: undefined method `foo=' for #<Person:0x00000100d89860> </pre> And last but not least: don't care about specifying the attributes available? Well don't! ;) <pre> \ No newline at end of file