dirty_hashy
dirty_hashy copied to clipboard
Dirty tracking within hashes (with or without indifferent access) or objects as it is expected to be!
h1. DirtyHashy "!https://secure.travis-ci.org/archan937/dirty_hashy.png!":http://travis-ci.org/archan937/dirty_hashy
Dirty tracking within hashes (with or without 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 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.
h2. Installation
h3. 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
h2. DirtyHashy versus DirtyIndifferentHashy
"On request":https://github.com/archan937/dirty_hashy/issues/1 of "@technoweenie":https://twitter.com/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":https://github.com/archan937/dirty_hashy/blob/279da72873a91216dbecadb3a6d53ebae2fd8198/test/unit/test_dirty_hashy.rb and "DirtyIndifferentHashy test":https://github.com/archan937/dirty_hashy/blob/279da72873a91216dbecadb3a6d53ebae2fd8198/test/unit/test_dirty_indifferent_hashy.rb.
Note: This README will continue focusing on the usage of the @DirtyIndifferentHashy@ class and @Dirty::Attributes@ module.
h2. 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]
h3. 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"]}
h3. 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"]}
h3. Dirty tracking objects (models)
Like "ActiveModel::Dirty":http://api.rubyonrails.org/classes/ActiveModel/Dirty.html, you can use "Dirty::Attributes":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 @Dirty::Attributes@ is easier to setup than @ActiveModel::Dirty@
The implementation of "Dirty::Attributes":https://github.com/archan937/dirty_hashy/tree/master/lib is more minimalistic and thus looks a bit cleaner than "ActiveModel::Dirty":https://github.com/rails/rails/blob/master/activemodel/lib/active_model/dirty.rb with "ActiveModel::AttributeMethods":https://github.com/rails/rails/blob/master/activemodel/lib/active_model/attribute_methods.rb
The following illustrates the differences between @Dirty::Attributes@ and @ActiveModel::Dirty@ when implementing a simple @Person@ model:
h4. 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
h4. 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 #<0x00000100d89860>0x00000100d89860><0x00000100d5cd88>0x00000100d5cd88>
""""""
""
""""""""""""
""
""