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 +4 -4
- data/lib/hash_param.rb +35 -6
- data/lib/hash_param/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: 45532520911d02ddcc01d5c2caa1b20b6a4f17ab
|
4
|
+
data.tar.gz: 7d0da178550b70700f6c35bb2b4cfc6a8853a131
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8b62bb1818ef0687bdca8b2f7395f71b95f446633cb49d97e22305f0c0d2946c9460ebc2000997a04898d5818eec6a2e332fe85fce3a666e9ed4debea299432
|
7
|
+
data.tar.gz: 4bd5db600d8e0df17c3e9df7d99cea4f4700256c2f7810b5615e03e2519f4f4a4150debfb8a42e0af950268080941c45f2b99cb3a88fc56fc5ae4d4819c8e90d
|
data/lib/hash_param.rb
CHANGED
@@ -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
|
-
|
18
|
-
|
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
|
-
#
|
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
|
-
|
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
|
data/lib/hash_param/version.rb
CHANGED