h1. DirtyHashy 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 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 #
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 #
  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"
h2. Last remarks Please check out "test/unit/test_dirty_hashy.rb":https://github.com/archan937/dirty_hashy/blob/master/test/unit/test_dirty_hashy.rb, "test/unit/test_dirty_indifferent_hashy.rb":https://github.com/archan937/dirty_hashy/blob/master/test/unit/test_dirty_indifferent_hashy.rb and "test/unit/test_dirty_attributes.rb":https://github.com/archan937/dirty_hashy/blob/master/test/unit/test_dirty_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* h2. Contact me For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl. h2. License Copyright (c) 2011 Paul Engel, released under the MIT license "http://holder.nl":http://holder.nl – "http://codehero.es":http://codehero.es – "http://gettopup.com":http://gettopup.com – "http://github.com/archan937":http://github.com/archan937 – "http://twitter.com/archan937":http://twitter.com/archan937 – "paul.engel@holder.nl":mailto: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.