dirty_hashy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = DirtyHashy CHANGELOG
2
+
3
+ == Version 0.1.0 (December 22, 2011)
4
+
5
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :gem_default do
6
+ gem "dirty_hashy", :path => "."
7
+ end
8
+
9
+ group :gem_development do
10
+ gem "pry"
11
+ end
12
+
13
+ group :gem_test do
14
+ gem "minitest"
15
+ gem "mocha"
16
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Paul Engel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,88 @@
1
+ h1. DirtyHashy
2
+
3
+ Dirty tracking within hashes with indifferent access
4
+
5
+ h2. Introduction
6
+
7
+ "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.
8
+
9
+ 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.
10
+
11
+ 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.
12
+
13
+ h2. Installation
14
+
15
+ h3. Using Bundler
16
+
17
+ Add DirtyHashy in @Gemfile@ as a gem dependency:
18
+
19
+ <pre>
20
+ gem "dirty_hashy"
21
+ </pre>
22
+
23
+ Run the following in your console to install with Bundler:
24
+
25
+ <pre>
26
+ bundle install
27
+ </pre>
28
+
29
+ h2. Usage
30
+
31
+ Using @DirtyHashy@ is pretty straightforward and can be used as follows:
32
+
33
+ <pre>
34
+ require "rubygems"
35
+ require "dirty_hashy"
36
+
37
+ h = DirtyHashy.new
38
+ h.dirty? #=> false
39
+ h[:name] = "Paul"
40
+ h.dirty? #=> true
41
+ h.was :name #=> nil
42
+ h.change :name #=> [nil, "Paul"]
43
+ h.clean_up!
44
+ h.dirty? #=> false
45
+ h[:name] #=> "Paul"
46
+ h[:name] = "Paul"
47
+ h.dirty? #=> false
48
+ h[:name] = "Engel"
49
+ h.change :name #=> ["Paul", "Engel"]
50
+ h[:name] = "Foo"
51
+ h.was :name #=> "Paul"
52
+ h.changes #=> {"name"=>["Paul", "Foo"]}
53
+ h["company"] = "Internetbureau Holder B.V."
54
+ h.changes #=> {"company"=>[nil, "Internetbureau Holder B.V."], "name"=>["Paul", "Foo"]}
55
+ h.merge! :name => "Paul"
56
+ h.changes #=> {"company"=>[nil, "Internetbureau Holder B.V."]}
57
+ h.clean_up!
58
+ h.dirty? #=> false
59
+ h.changes #=> {}
60
+ h.delete :company
61
+ h.dirty? #=> true
62
+ h.was :company #=> "Internetbureau Holder B.V."
63
+ h.change :company #=> ["Internetbureau Holder B.V.", nil]
64
+ </pre>
65
+
66
+ h2. Last remarks
67
+
68
+ Please check out "test/unit/test_dirty_hashy.rb":https://github.com/archan937/dirty_hashy/blob/master/test/unit/test_dirty_hashy.rb the tests available. You can run the unit tests with @rake@ within the terminal.
69
+
70
+ Also, the DirtyHashy repo is provided with @script/console@ which you can use for testing purposes.
71
+
72
+ Note: *DirtyHashy is successfully tested using Ruby 1.8.7 and Ruby 1.9.2*
73
+
74
+ h2. Contact me
75
+
76
+ For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl.
77
+
78
+ h2. License
79
+
80
+ Copyright (c) 2011 Paul Engel, released under the MIT license
81
+
82
+ "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
83
+
84
+ 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:
85
+
86
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
87
+
88
+ 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.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |test|
8
+ test.pattern = "test/**/test_*.rb"
9
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Paul Engel"]
5
+ gem.email = ["paul.engel@holder.nl"]
6
+ gem.description = %q{Dirty tracking within hashes with indifferent access}
7
+ gem.summary = %q{Dirty tracking within hashes with indifferent access}
8
+ gem.homepage = "https://github.com/archan937/dirty_hashy"
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "dirty_hashy"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = "0.1.0"
16
+
17
+ gem.add_dependency "activesupport", ">= 3.0.0"
18
+ end
@@ -0,0 +1,44 @@
1
+ require "active_support/hash_with_indifferent_access"
2
+ require "dirty_hashy/version"
3
+
4
+ class DirtyHashy < HashWithIndifferentAccess
5
+
6
+ alias :_regular_writer :regular_writer
7
+ def regular_writer(key, value)
8
+ original_value = changes.key?(key) ? was(key) : self[key]
9
+ if original_value == value
10
+ changes.delete key
11
+ else
12
+ changes[key] = [original_value, value]
13
+ end
14
+ _regular_writer key, value
15
+ end
16
+
17
+ def delete(key)
18
+ self[key] = nil
19
+ super
20
+ end
21
+
22
+ def changes
23
+ @changes ||= HashWithIndifferentAccess.new
24
+ end
25
+
26
+ def changed?(key = nil)
27
+ key.nil? ? !changes.empty? : changes.key?(key)
28
+ end
29
+ alias :dirty? :changed?
30
+
31
+ def change(key)
32
+ changes[key] if changed?(key)
33
+ end
34
+
35
+ def was(key)
36
+ change(key).first if changed?(key)
37
+ end
38
+
39
+ def clean_up!
40
+ changes.clear
41
+ nil
42
+ end
43
+
44
+ end
@@ -0,0 +1,7 @@
1
+ class DirtyHashy < HashWithIndifferentAccess
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ TINY = 0
5
+
6
+ VERSION = [MAJOR, MINOR, TINY].join(".")
7
+ end
data/script/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "bundler"
4
+
5
+ Bundler.require :gem_default, :gem_development
6
+
7
+ puts "Loading DirtyHashy development environment (#{DirtyHashy::VERSION})"
8
+ puts "[0] pry(main)> @h = DirtyHashy.new"
9
+
10
+ @h = DirtyHashy.new
11
+ Pry.start
@@ -0,0 +1,7 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+
4
+ Bundler.require :gem_default, :gem_test
5
+
6
+ require "minitest/unit"
7
+ require "minitest/autorun"
@@ -0,0 +1,140 @@
1
+ require File.expand_path("../../test_helper", __FILE__)
2
+
3
+ module Unit
4
+ class TestDirtyHashy < MiniTest::Unit::TestCase
5
+
6
+ describe DirtyHashy do
7
+ it "should behave as expected" do
8
+ hashy = DirtyHashy.new
9
+
10
+ assert_equal false, hashy.dirty?
11
+ assert_equal false, hashy.changed?
12
+ assert_equal({}, hashy.changes)
13
+
14
+ hashy["name"] = "Paul"
15
+
16
+ assert_equal true, hashy.dirty?
17
+ assert_equal true, hashy.changed?
18
+ assert_equal true, hashy.changed?(:name)
19
+ assert_equal true, hashy.changed?("name")
20
+ assert_equal [nil, "Paul"], hashy.change(:name)
21
+ assert_equal [nil, "Paul"], hashy.change("name")
22
+ assert_equal({"name" => [nil, "Paul"]}, hashy.changes)
23
+
24
+ hashy[:name] = nil
25
+
26
+ assert_equal false, hashy.dirty?
27
+ assert_equal false, hashy.changed?
28
+ assert_equal false, hashy.changed?(:name)
29
+ assert_equal false, hashy.changed?("name")
30
+ assert_equal nil, hashy.change(:name)
31
+ assert_equal nil, hashy.change("name")
32
+ assert_equal({}, hashy.changes)
33
+
34
+ hashy["name"] = "Stephan"
35
+
36
+ assert_equal true, hashy.dirty?
37
+ assert_equal true, hashy.changed?
38
+ assert_equal true, hashy.changed?(:name)
39
+ assert_equal true, hashy.changed?("name")
40
+ assert_equal [nil, "Stephan"], hashy.change(:name)
41
+ assert_equal [nil, "Stephan"], hashy.change("name")
42
+ assert_equal({"name" => [nil, "Stephan"]}, hashy.changes)
43
+
44
+ hashy.clean_up!
45
+
46
+ assert_equal false, hashy.dirty?
47
+ assert_equal false, hashy.changed?
48
+ assert_equal false, hashy.changed?(:name)
49
+ assert_equal false, hashy.changed?("name")
50
+ assert_equal nil, hashy.change(:name)
51
+ assert_equal nil, hashy.change("name")
52
+ assert_equal({}, hashy.changes)
53
+
54
+ hashy["name"] = "Chris"
55
+
56
+ assert_equal true, hashy.dirty?
57
+ assert_equal true, hashy.changed?
58
+ assert_equal true, hashy.changed?(:name)
59
+ assert_equal true, hashy.changed?("name")
60
+ assert_equal ["Stephan", "Chris"], hashy.change(:name)
61
+ assert_equal ["Stephan", "Chris"], hashy.change("name")
62
+ assert_equal({"name" => ["Stephan", "Chris"]}, hashy.changes)
63
+
64
+ hashy["name"] = "Stephan"
65
+
66
+ assert_equal false, hashy.dirty?
67
+ assert_equal false, hashy.changed?
68
+ assert_equal false, hashy.changed?(:name)
69
+ assert_equal false, hashy.changed?("name")
70
+ assert_equal nil, hashy.change(:name)
71
+ assert_equal nil, hashy.change("name")
72
+ assert_equal({}, hashy.changes)
73
+
74
+ hashy["name"] = "Paul"
75
+
76
+ assert_equal true, hashy.dirty?
77
+ assert_equal true, hashy.changed?
78
+ assert_equal true, hashy.changed?(:name)
79
+ assert_equal true, hashy.changed?("name")
80
+ assert_equal ["Stephan", "Paul"], hashy.change(:name)
81
+ assert_equal ["Stephan", "Paul"], hashy.change("name")
82
+ assert_equal({"name" => ["Stephan", "Paul"]}, hashy.changes)
83
+
84
+ hashy["name"] = "Tim"
85
+
86
+ assert_equal true, hashy.dirty?
87
+ assert_equal true, hashy.changed?
88
+ assert_equal true, hashy.changed?(:name)
89
+ assert_equal true, hashy.changed?("name")
90
+ assert_equal ["Stephan", "Tim"], hashy.change(:name)
91
+ assert_equal ["Stephan", "Tim"], hashy.change("name")
92
+ assert_equal({"name" => ["Stephan", "Tim"]}, hashy.changes)
93
+
94
+ hashy["company"] = "Holder"
95
+
96
+ assert_equal true, hashy.dirty?
97
+ assert_equal true, hashy.changed?
98
+ assert_equal true, hashy.changed?(:name)
99
+ assert_equal true, hashy.changed?("name")
100
+ assert_equal true, hashy.changed?(:company)
101
+ assert_equal true, hashy.changed?("company")
102
+ assert_equal ["Stephan", "Tim"], hashy.change(:name)
103
+ assert_equal ["Stephan", "Tim"], hashy.change("name")
104
+ assert_equal [nil, "Holder"], hashy.change(:company)
105
+ assert_equal [nil, "Holder"], hashy.change("company")
106
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Holder"]}, hashy.changes)
107
+
108
+ hashy["city"] = "Amsterdam"
109
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Holder"], "city" => [nil, "Amsterdam"]}, hashy.changes)
110
+
111
+ hashy.delete :city
112
+ assert_equal({"name" => ["Stephan", "Tim"], "company" => [nil, "Holder"]}, hashy.changes)
113
+
114
+ hashy.clean_up!
115
+
116
+ assert_equal false, hashy.dirty?
117
+ assert_equal false, hashy.changed?
118
+ assert_equal false, hashy.changed?(:name)
119
+ assert_equal false, hashy.changed?("name")
120
+ assert_equal false, hashy.changed?(:name)
121
+ assert_equal false, hashy.changed?("name")
122
+ assert_equal nil, hashy.change(:company)
123
+ assert_equal nil, hashy.change("company")
124
+ assert_equal({}, hashy.changes)
125
+
126
+ hashy.delete :company
127
+
128
+ assert_equal true, hashy.dirty?
129
+ assert_equal true, hashy.changed?
130
+ assert_equal true, hashy.changed?(:company)
131
+ assert_equal true, hashy.changed?("company")
132
+ assert_equal "Holder", hashy.was(:company)
133
+ assert_equal "Holder", hashy.was("company")
134
+ assert_equal ["Holder", nil], hashy.change(:company)
135
+ assert_equal ["Holder", nil], hashy.change("company")
136
+ end
137
+ end
138
+
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dirty_hashy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Engel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &2156836380 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2156836380
25
+ description: Dirty tracking within hashes with indifferent access
26
+ email:
27
+ - paul.engel@holder.nl
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - CHANGELOG.rdoc
34
+ - Gemfile
35
+ - MIT-LICENSE
36
+ - README.textile
37
+ - Rakefile
38
+ - VERSION
39
+ - dirty_hashy.gemspec
40
+ - lib/dirty_hashy.rb
41
+ - lib/dirty_hashy/version.rb
42
+ - script/console
43
+ - test/test_helper.rb
44
+ - test/unit/test_dirty_hashy.rb
45
+ homepage: https://github.com/archan937/dirty_hashy
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.10
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Dirty tracking within hashes with indifferent access
69
+ test_files:
70
+ - test/test_helper.rb
71
+ - test/unit/test_dirty_hashy.rb