document_hash 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,5 +3,9 @@ module DocumentHash
3
3
  def after_change &block
4
4
  @after_change = block
5
5
  end
6
+
7
+ def before_change &block
8
+ @before_change = block
9
+ end
6
10
  end
7
11
  end
@@ -31,8 +31,9 @@ module DocumentHash
31
31
  val.__send__ :parent_key=, key;
32
32
  end
33
33
 
34
+ val = execute_before_change_callback key,val
34
35
  super key, val
35
- changed_key key
36
+ changed_key key, val
36
37
  end
37
38
 
38
39
  def reset!
@@ -55,12 +56,21 @@ module DocumentHash
55
56
 
56
57
  attr_accessor :parent, :parent_key
57
58
 
58
- def changed_key key
59
+ def changed_key key, value
59
60
  path = Array(key)
60
61
 
61
- @after_change.call path if @after_change
62
+ @after_change.call path, value if @after_change
62
63
  changed_attributes << path.first
63
- parent.__send__ :changed_key, path.unshift(parent_key) if parent
64
+ parent.__send__ :changed_key, path.unshift(parent_key), value if parent
65
+ end
66
+
67
+ def execute_before_change_callback key, value
68
+ path = Array(key)
69
+
70
+ value = @before_change.call path, value if @before_change
71
+ value = parent.__send__ :execute_before_change_callback, path.unshift(parent_key), value if parent
72
+
73
+ value
64
74
  end
65
75
 
66
76
  def changed_attributes
@@ -1,3 +1,3 @@
1
1
  module DocumentHash
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -91,10 +91,10 @@ describe DocumentHash::Core do
91
91
  it "triggers the after change block" do
92
92
  subject = DocumentHash::Core.new
93
93
  test_mock = mock("test")
94
- test_mock.should_receive(:callback_mock).with([:test])
94
+ test_mock.should_receive(:callback_mock).with([:test], "value")
95
95
 
96
- subject.after_change do |path|
97
- test_mock.callback_mock path
96
+ subject.after_change do |path, value|
97
+ test_mock.callback_mock path, value
98
98
  end
99
99
 
100
100
  subject["test"] = "value"
@@ -103,12 +103,36 @@ describe DocumentHash::Core do
103
103
  it "receives the right path when multilevel" do
104
104
  subject = DocumentHash::Core[ { inner: { attribute: "value" } } ]
105
105
  test_mock = mock("test")
106
- test_mock.should_receive(:callback_mock).with([:inner, :attribute])
106
+ test_mock.should_receive(:callback_mock).with([:inner, :attribute], "hello")
107
107
 
108
- subject.after_change do |path|
109
- test_mock.callback_mock path
108
+ subject.after_change do |path, value|
109
+ test_mock.callback_mock path, value
110
110
  end
111
111
 
112
112
  subject[:inner][:attribute] = "hello"
113
113
  end
114
+
115
+ it "triggers a callback before change" do
116
+ subject = DocumentHash::Core[ { inner: { attribute: "value" } } ]
117
+ test_mock = mock("test")
118
+ test_mock.should_receive(:callback_mock).with([:inner, :attribute], "hello")
119
+
120
+ subject.before_change do |path, value|
121
+ test_mock.callback_mock path, value
122
+ end
123
+
124
+ subject[:inner][:attribute] = "hello"
125
+ end
126
+
127
+ it "overrides the value being written by the before_change callback" do
128
+ subject = DocumentHash::Core[ { inner: { attribute: "value" } } ]
129
+
130
+ subject.before_change do |path, value|
131
+ "hola"
132
+ end
133
+
134
+ subject[:inner][:attribute] = "hello"
135
+ subject[:inner][:attribute].should == "hola"
136
+ end
137
+
114
138
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: document_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: