hash_param 0.0.2 → 0.0.3

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: 9fa41452500af1f6485cf963eddba85b8b136480
4
- data.tar.gz: 092c5a37c775949c30911a7a5f5f663bc1ec6da0
3
+ metadata.gz: 45532520911d02ddcc01d5c2caa1b20b6a4f17ab
4
+ data.tar.gz: 7d0da178550b70700f6c35bb2b4cfc6a8853a131
5
5
  SHA512:
6
- metadata.gz: 9a2ab5099f422e870b06826926a4259af2b2688882fca1122fddad9c86cf9e6bd5cf3e13c1c694aa44d14d80a2a10a1e4bad003ce8565204f7814d81fcea0978
7
- data.tar.gz: ee4207b9b643940dfcc2d9d95c217aff7fdc10ce861dc6ab182f2729e2e91541b8af4b00d195e4d6b09b967cb65cc8db2ed298a2b60cff16a3ef6ca262f0e737
6
+ metadata.gz: d8b62bb1818ef0687bdca8b2f7395f71b95f446633cb49d97e22305f0c0d2946c9460ebc2000997a04898d5818eec6a2e332fe85fce3a666e9ed4debea299432
7
+ data.tar.gz: 4bd5db600d8e0df17c3e9df7d99cea4f4700256c2f7810b5615e03e2519f4f4a4150debfb8a42e0af950268080941c45f2b99cb3a88fc56fc5ae4d4819c8e90d
@@ -6,7 +6,7 @@ module HashParam
6
6
  end
7
7
 
8
8
  module ClassMethods
9
- def hash_param(*method_names)
9
+ def hash_param(*method_names, transform_key: nil, from: nil)
10
10
  method_names.each do |method_name|
11
11
  visibility = instance_method_visibility(method_name)
12
12
  hidden_name = "#{method_name}_before_hash_param"
@@ -14,11 +14,26 @@ module HashParam
14
14
  alias_method hidden_name, method_name
15
15
  private hidden_name
16
16
 
17
- define_method(method_name) do |*args|
18
- hash_param_dispatch(hidden_name, *args)
17
+ if from.nil?
18
+ # We're passed the argument, e.g., caller expects f(data)
19
+ define_method(method_name) do |arg|
20
+ hash_param_dispatch(hidden_name, arg, transform_key: transform_key)
21
+ end
22
+ elsif from.is_a?(Symbol) && from.to_s[0] == '@'
23
+ # Pull the data from an instance variable
24
+ define_method(method_name) do
25
+ hash_param_dispatch(hidden_name, instance_variable_get(from),
26
+ transform_key: transform_key)
27
+ end
28
+ elsif from.is_a?(Symbol)
29
+ # Pull the Hash from a method, e.g., Rails' `params`
30
+ define_method(method_name) do
31
+ hash_param_dispatch(hidden_name, send(from),
32
+ transform_key: transform_key)
33
+ end
19
34
  end
20
35
 
21
- # We set the new wrapping method's visibility to what the original was.
36
+ # Set the new wrapping method's visibility to what the original was.
22
37
  send(visibility, method_name)
23
38
  end
24
39
  end
@@ -35,13 +50,27 @@ module HashParam
35
50
  end
36
51
 
37
52
  private
38
- def hash_param_dispatch(method_name, data)
53
+ def hash_param_dispatch(method_name, data, transform_key: nil)
39
54
  args, kwargs, rest_index, has_kwrest = [], {}, nil, nil
40
55
  public_method_name = method_name
41
56
 
57
+ if transform_key
58
+ data = data.map do |k, v|
59
+ [k.to_s.send(transform_key), v]
60
+ end.to_h
61
+ end
62
+
42
63
  method(method_name).parameters.each.with_index do |(type, name), i|
43
- key_name = name.to_s
64
+ # attempt symbol
65
+ key_name = name
44
66
  exists = data.key?(key_name)
67
+
68
+ # try again with string key
69
+ if !exists
70
+ key_name = name.to_s
71
+ exists = data.key?(key_name)
72
+ end
73
+
45
74
  value = exists ? data.delete(key_name) : nil
46
75
 
47
76
  case type
@@ -1,3 +1,3 @@
1
1
  module HashParam
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike A. Owens