symbolmatrix 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,7 +69,7 @@ class SymbolMatrix < Hash
69
69
  end
70
70
 
71
71
  # Merges this SymbolMatrix with another SymbolMatrix recursively
72
- def recursive_merge hash
72
+ def recursive_merge hash, override = false
73
73
  result = SymbolMatrix.new
74
74
  self.keys.each do |key|
75
75
  result[key] = self[key]
@@ -77,7 +77,15 @@ class SymbolMatrix < Hash
77
77
 
78
78
  hash.keys.each do |key|
79
79
  if result.has_key? key
80
- result[key] = result[key].recursive_merge hash[key]
80
+ if result[key].respond_to? :recursive_merge
81
+ result[key] = result[key].recursive_merge hash[key], override
82
+ else
83
+ if override
84
+ result[key] = hash[key]
85
+ else
86
+ raise MergeError, "The value of the :#{key} key is already defined. Run recursive merge with flag true if you want it to override"
87
+ end
88
+ end
81
89
  else
82
90
  result[key] = hash[key]
83
91
  end
@@ -86,10 +94,18 @@ class SymbolMatrix < Hash
86
94
  end
87
95
 
88
96
  # Merges recursively the passed SymbolMatrix into self
89
- def recursive_merge! symbolmatrix
97
+ def recursive_merge! symbolmatrix, override = false
90
98
  symbolmatrix.each do |key, value|
91
99
  if self.has_key? key
92
- self[key].recursive_merge! value
100
+ if self[key].respond_to? :recursive_merge!
101
+ self[key].recursive_merge! value, override
102
+ else
103
+ if override
104
+ self[key] = value
105
+ else
106
+ raise MergeError, "The value of the :#{key} key is already defined. Run recursive merge with flag true if you want it to override"
107
+ end
108
+ end
93
109
  else
94
110
  self[key] = value
95
111
  end
@@ -98,6 +114,7 @@ class SymbolMatrix < Hash
98
114
 
99
115
  class KeyNotDefinedException < RuntimeError; end
100
116
  class InvalidKeyException < RuntimeError; end
117
+ class MergeError < RuntimeError; end
101
118
  end
102
119
 
103
120
  def SymbolMatrix *args
@@ -1,3 +1,3 @@
1
1
  module Symbolmatrix
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
@@ -120,6 +120,25 @@ describe SymbolMatrix do
120
120
  end
121
121
 
122
122
  describe "#recursive_merge" do
123
+ it 'should raise a relevant error when the two matrices collide' do
124
+ sm = SymbolMatrix a: "hola"
125
+ expect { sm.recursive_merge SymbolMatrix a: "hey"
126
+ }.to raise_error SymbolMatrix::MergeError,
127
+ "The value of the :a key is already defined. Run recursive merge with flag true if you want it to override"
128
+ end
129
+
130
+ it 'should override on collide if the override flag is on' do
131
+ sm = SymbolMatrix a: "hola"
132
+ result = sm.recursive_merge SymbolMatrix(a: "hey"), true
133
+ result.a.should == "hey"
134
+ end
135
+
136
+ it 'should send the override directive into the recursion' do
137
+ sm = SymbolMatrix a: { b: "hola" }
138
+ result = sm.recursive_merge SymbolMatrix( a: { b: "hey" } ), true
139
+ result.a.b.should == "hey"
140
+ end
141
+
123
142
  it 'should merge two symbolmatrices' do
124
143
  sm = SymbolMatrix.new a: "hola"
125
144
  result = sm.recursive_merge SymbolMatrix.new b: "chau"
@@ -171,6 +190,25 @@ describe SymbolMatrix do
171
190
  end
172
191
 
173
192
  describe '#recursive_merge!' do
193
+ it 'should raise a relevant error when the two matrices collide' do
194
+ sm = SymbolMatrix a: "hola"
195
+ expect { sm.recursive_merge! SymbolMatrix a: "hey"
196
+ }.to raise_error SymbolMatrix::MergeError,
197
+ "The value of the :a key is already defined. Run recursive merge with flag true if you want it to override"
198
+ end
199
+
200
+ it 'should override on collide if the override flag is on' do
201
+ sm = SymbolMatrix a: "hola"
202
+ sm.recursive_merge! SymbolMatrix(a: "hey"), true
203
+ sm.a.should == "hey"
204
+ end
205
+
206
+ it 'should send the override directive into the recursion' do
207
+ sm = SymbolMatrix a: { b: "hola" }
208
+ sm.recursive_merge! SymbolMatrix( a: { b: "hey" } ), true
209
+ sm.a.b.should == "hey"
210
+ end
211
+
174
212
  it 'should merge a symbolmatrix into this' do
175
213
  sm = SymbolMatrix.new a: "hola"
176
214
  sm.recursive_merge! SymbolMatrix.new b: "chau"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: symbolmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-19 00:00:00.000000000 Z
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: discoverer