Dirty tracking within hashes (with or without indifferent access) or objects as it is expected to be!
Introduction
Dirty tracking / 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, DataMapper, Mongoid and CouchRest Model.
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.
Installation
Using Bundler
Add DirtyHashy in Gemfile
as a gem dependency:
gem "dirty_hashy"
Run the following in your console to install with Bundler:
bundle install
DirtyHashy versus DirtyIndifferentHashy
On request of @technoweenie, I have released DirtyHashy v0.2.0. Previous to this release, the DirtyHashy
class inherited from ActiveSupport’s HashWithIndifferentAccess
but that has changed.
As of the 0.2.0
release, the DirtyHashy
class is inherited from the Hash
class. So I have introduced DirtyIndifferentHashy
which resembles the DirtyHashy
class of the 0.1.x
releases. You can check out the different types of behaviour by looking at the DirtyHashy test and DirtyIndifferentHashy test.
Note: This README will continue focusing on the usage of the DirtyIndifferentHashy
class and Dirty::Attributes
module.
Usage
Using DirtyIndifferentHashy
is pretty straightforward and can be used as follows:
require "rubygems" require "dirty_hashy" h = DirtyIndifferentHashy.new h.dirty? #=> false h[:name] = "Paul" h.dirty? #=> true h.changed? :name #=> true h.was :name #=> nil h.change :name #=> [nil, "Paul"] h.clean_up! h.dirty? #=> false h[:name] #=> "Paul" h[:name] = "Paul" h.dirty? #=> false h[:name] = "Engel" h.change :name #=> ["Paul", "Engel"] h[:name] = "Foo" h.was :name #=> "Paul" h.changes #=> {"name"=>["Paul", "Foo"]} h["company"] = "Internetbureau Holder B.V." h.changes #=> {"company"=>[nil, "Internetbureau Holder B.V."], "name"=>["Paul", "Foo"]} h.merge! :name => "Paul" h.changes #=> {"company"=>[nil, "Internetbureau Holder B.V."]} h.clean_up! h.dirty? #=> false h.changes #=> {} h.delete :company h.dirty? #=> true h.was :company #=> "Internetbureau Holder B.V." h.change :company #=> ["Internetbureau Holder B.V.", nil]
Method mapping DirtyIndifferentHashy
You can map methods within a DirtyIndifferentHashy in order to provide convenience methods like name
, name=
, name_changed?
, name_was
and name_change
. Just pass true
for the map_methods
argument when initializing a DirtyIndifferentHashy:
require "rubygems" require "dirty_hashy" h = DirtyIndifferentHashy.new({}, true) h.dirty? #=> false h.name #=> NoMethodError: undefined method `name' for {}:DirtyIndifferentHashy h.name = "Paul" h.dirty? #=> true h.name_changed? #=> true h.name_was #=> nil h.name_change #=> [nil, "Paul"] h.clean_up! h.dirty? #=> false h.name #=> "Paul" 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"]}
Method mapping DirtyIndifferentHashy 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 DirtyIndifferentHashy:
require "rubygems" require "dirty_hashy" h = DirtyIndifferentHashy.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"}:DirtyIndifferentHashy h.foo = "bar" #=> NoMethodError: undefined method `foo=' for {"name"=>"Engel"}:DirtyIndifferentHashy h.clean_up! h.replace :name => "Paul" h.changes #=> {"name"=>["Engel", "Paul"]}
Dirty tracking objects (models)
Like ActiveModel::Dirty, you can use Dirty::Attributes to dirty track your own objects (models). But there are two differences:
- Setting up
Dirty::Attributes
is easier to setup thanActiveModel::Dirty
- The implementation of Dirty::Attributes is more minimalistic and thus looks a bit cleaner than ActiveModel::Dirty with ActiveModel::AttributeMethods
The following illustrates the differences between Dirty::Attributes
and ActiveModel::Dirty
when implementing a simple Person
model:
When using ActiveModel::Dirty
class Person include ActiveModel::Dirty define_attribute_methods = [:name] def name @name end def name=(val) name_will_change! unless val == @name @name = val end def save @previously_changed = changes @changed_attributes.clear end end
When using Dirty::Attributes
class Person include Dirty::Attributes attrs :name def save clean_up! end end
You can use Person
objects as you would expect:
require "rubygems" require "dirty_hashy" class Person include Dirty::Attributes attrs :name end p = Person.new p.dirty? #=> false p.name #=> nil p.name = "Paul" p.dirty? #=> true p.name_changed? #=> true 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>
And last but not least: don’t care about specifying the attributes available? Well don’t! ;)
require "rubygems" require "dirty_hashy" class Person include Dirty::Attributes end p = Person.new p.dirty? #=> false p.name #=> NoMethodError: undefined method `name' for #<Person:0x00000100d5cd88> p.name = "Paul" p.dirty? #=> true p.name #=> "Paul" p.name_changed? #=> true p.name_was #=> nil p.name_change #=> [nil, "Paul"] p.clean_up! p.dirty? #=> false p.name #=> "Paul" p.foo = "bar" p.foo #=> "bar"
Last remarks
Please check out test/unit/test_dirty_hashy.rb, test/unit/test_dirty_indifferent_hashy.rb and test/unit/dirty/test_attributes.rb the tests available.
You can run the unit tests with rake
within the terminal.
Also, the DirtyHashy repo is provided with script/console
which you can use for testing purposes.
Note: DirtyHashy is successfully tested using Ruby 1.8.7, Ruby 1.9.2 and Ruby 1.9.3
Contact me
For support, remarks and requests please mail me at paul.engel@holder.nl.
License
Copyright © 2011 Paul Engel, released under the MIT license
http://holder.nl – http://codehero.es – http://gettopup.com – http://github.com/archan937 – http://twitter.com/archan937 – paul.engel@holder.nl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.