mock_proxy 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c118fdb5559a6e6906597476ffe927e417339419
4
- data.tar.gz: 909cf35aad9069f92cce0f21f17de03fe6489356
3
+ metadata.gz: f8c93f0ddff097c44b0ba16b2887f9b904655de2
4
+ data.tar.gz: 23738c6f5c1a902962ebef15fed5d97f0ed4e4b4
5
5
  SHA512:
6
- metadata.gz: de97377cfc310783879d568738f0fc7acf57bb5455190f8b6846132da1e83fd415ab8bb70a22f7c2f0b4bd09eff66320482c5930a835e503e01bfcf0ef64f326
7
- data.tar.gz: 58f8f9033e815d3ccda25ea216b3c3335c7090fc4aa509dfecdcf772521c5f19b2cb9629dc2a98edc6e3b73fa8fc1a0798a17edc223687fb3f1fca051ff33534
6
+ metadata.gz: f529ec870c4388f683991a8c5b9da8fe370ad9d6a45a593bc59b488ee92d12001bc9f5f911120678b82597fe1891f537937cc91cd83f9d43ae7829a94116d6a4
7
+ data.tar.gz: 8f329cb371bf6edff177ea6e0466e9241411dcc93fd34be87688b53eea96e3a7c6eba1d84241857a01f631bd963a41b98fea7c0cf4a02ccd1af2a226bde11958
data/lib/mock_proxy.rb CHANGED
@@ -194,23 +194,35 @@ class MockProxy
194
194
  # @return [MockProxy] if proc existed at key path
195
195
  # @raise [ArgumentError] if proc not found or hash found at key path
196
196
  def self.set_callback(proxy, key_path, proc, validate = true)
197
+ fail ArgumentError, 'proc must be provided' unless proc
198
+ fail ArgumentError, 'proc must be a proc' unless proc.is_a?(Proc)
197
199
  # Validate by checking if proc exists at key path
198
200
  get_and_validate_callback(proxy, key_path) if validate
199
201
  # Set callback at key path, validating if set
200
202
  key_paths = key_path.is_a?(Array) ? key_path.map(&:to_s) : key_path.to_s.split('.')
201
203
  copied_callback_hash = Hash[proxy.instance_variable_get('@callback_hash')]
204
+ # NOTE: Using reduce for accumulator but don't need the return value
202
205
  key_paths.reduce(copied_callback_hash) do |callback_hash, key|
203
- if !callback_hash || !callback_hash[key]
204
- if validate
206
+ # Last key
207
+ if key_paths.last == key
208
+ # Type check value, if validate
209
+ if validate && !callback_hash[key].is_a?(Proc)
210
+ fail ArgumentError, "The existing callback tree contains the full key path you provided but continues going (i.e. no proc at exact key path). If you want to shorten the callback tree, use MockProxy.set_at. The callback tree looks like this: #{copied_callback_hash}"
211
+ else
212
+ # Assign new proc if pass validations
213
+ callback_hash[key] = proc
214
+ end
215
+ else
216
+ # In-between keys
217
+ # Check presence, if validate
218
+ if validate && !callback_hash[key]
205
219
  fail ArgumentError, "The existing callback tree does not contain the full key path you provided. We stopped at #{key} and the callback tree looks like this: #{copied_callback_hash}"
206
220
  else
207
- callback_hash[key] = {}
221
+ # Assign new hash (i.e. create new key path) if there is none (validate won't
222
+ # create new path because it would have failed above)
223
+ callback_hash[key] ||= {}
208
224
  end
209
225
  end
210
- if callback_hash[key].is_a?(Proc)
211
- callback_hash[key] = proc
212
- else
213
- callback_hash[key]
214
226
  end
215
227
  end
216
228
  proxy.instance_variable_set('@callback_hash', copied_callback_hash)
@@ -1,4 +1,4 @@
1
1
  class MockProxy
2
2
  # The version number
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - matrinox