decanter 3.2.1 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/decanter/core.rb +18 -11
- data/lib/decanter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7ee762570171d13eede776ea7715f54e3daa9228e09b8a83f278c857eaeb04c
|
4
|
+
data.tar.gz: 4dee8cfa404faeb3362925832d53b4e55c0f82cddd2427416c0fdf79a66ff85e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd1b076ac0cbe78783ab19b4ae43d8fb7d87bd46b76155ec08dcbdfb55638415667c1ad19ccdeff4f81e0b0482c2ef32292a1686df648ee52e0b3ce4824ea069
|
7
|
+
data.tar.gz: d34c06b627f4c1b4773829cd9e3003f3a8eb167a97acfa55be361628078d8230d8be1664d576d49eb6de9705d56f95c478ff52c56c2c656b1882d167df69e499
|
data/Gemfile.lock
CHANGED
data/lib/decanter/core.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Decanter
|
2
2
|
module Core
|
3
3
|
DEFAULT_VALUE_KEY = :default_value
|
4
|
+
ACTION_CONTROLLER_PARAMETERS_CLASS_NAME = 'ActionController::Parameters'
|
4
5
|
|
5
6
|
def self.included(base)
|
6
7
|
base.extend(ClassMethods)
|
@@ -9,16 +10,16 @@ module Decanter
|
|
9
10
|
module ClassMethods
|
10
11
|
|
11
12
|
def input(name, parsers=nil, **options)
|
13
|
+
# Convert all input names to symbols to correctly calculate handled vs. unhandled keys
|
14
|
+
input_names = [name].flatten.map(&:to_sym)
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
if _name.length > 1 && parsers.blank?
|
16
|
+
if input_names.length > 1 && parsers.blank?
|
16
17
|
raise ArgumentError.new("#{self.name} no parser specified for input with multiple values.")
|
17
18
|
end
|
18
19
|
|
19
|
-
handlers[
|
20
|
-
key: options.fetch(:key,
|
21
|
-
name:
|
20
|
+
handlers[input_names] = {
|
21
|
+
key: options.fetch(:key, input_names.first),
|
22
|
+
name: input_names,
|
22
23
|
options: options,
|
23
24
|
parsers: parsers,
|
24
25
|
type: :input
|
@@ -46,7 +47,7 @@ module Decanter
|
|
46
47
|
end
|
47
48
|
|
48
49
|
def ignore(*args)
|
49
|
-
keys_to_ignore.push(*args)
|
50
|
+
keys_to_ignore.push(*args).map!(&:to_sym)
|
50
51
|
end
|
51
52
|
|
52
53
|
def strict(mode)
|
@@ -57,10 +58,12 @@ module Decanter
|
|
57
58
|
def decant(args)
|
58
59
|
return handle_empty_args if args.blank?
|
59
60
|
return empty_required_input_error unless required_input_keys_present?(args)
|
60
|
-
|
61
|
+
|
62
|
+
# Convert all params passed to a decanter to a hash with indifferent access to mitigate accessor ambiguity
|
63
|
+
accessible_args = to_indifferent_hash(args)
|
61
64
|
{}.merge( default_keys )
|
62
|
-
.merge( unhandled_keys(
|
63
|
-
.merge( handled_keys(
|
65
|
+
.merge( unhandled_keys(accessible_args) )
|
66
|
+
.merge( handled_keys(accessible_args) )
|
64
67
|
end
|
65
68
|
|
66
69
|
def default_keys
|
@@ -120,7 +123,7 @@ module Decanter
|
|
120
123
|
|
121
124
|
return {} unless unhandled_keys.any?
|
122
125
|
raise(UnhandledKeysError, "#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.") if strict_mode
|
123
|
-
args.select { |key| unhandled_keys.include? key }
|
126
|
+
args.select { |key| unhandled_keys.include? key.to_sym }
|
124
127
|
end
|
125
128
|
|
126
129
|
def handled_keys(args)
|
@@ -227,6 +230,10 @@ module Decanter
|
|
227
230
|
value.nil? || value == ""
|
228
231
|
end
|
229
232
|
|
233
|
+
def to_indifferent_hash(args)
|
234
|
+
return args.to_unsafe_h if args.class.name == ACTION_CONTROLLER_PARAMETERS_CLASS_NAME
|
235
|
+
args.to_h.with_indifferent_access
|
236
|
+
end
|
230
237
|
end
|
231
238
|
end
|
232
239
|
end
|
data/lib/decanter/version.rb
CHANGED