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 +4 -4
- data/lib/mock_proxy.rb +19 -7
- data/lib/mock_proxy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8c93f0ddff097c44b0ba16b2887f9b904655de2
|
4
|
+
data.tar.gz: 23738c6f5c1a902962ebef15fed5d97f0ed4e4b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
204
|
-
|
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
|
-
|
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)
|
data/lib/mock_proxy/version.rb
CHANGED