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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bcc235910fd86ed830c20f5c07f53b3154911cb26aec56d45fece483207d4bd
4
- data.tar.gz: a9db5721c34fdd0c9857ce8647ad84b2a4bbfdb6b1b76f67fe4e5a8b1650d9d2
3
+ metadata.gz: b7ee762570171d13eede776ea7715f54e3daa9228e09b8a83f278c857eaeb04c
4
+ data.tar.gz: 4dee8cfa404faeb3362925832d53b4e55c0f82cddd2427416c0fdf79a66ff85e
5
5
  SHA512:
6
- metadata.gz: 170e5256eda3f2b36f259f6f475420218c1a04aa360f553a0fb0f74479c28469a521e32fbfb852521d8c84e5dc22d754f7455e66e18702493b303b285cb1f7a6
7
- data.tar.gz: 35a01b51460f2f831a40e52fc5f11a8e800b5e50eeb08d0623f8241d72f00b5f20e1543235904083e793b0be76a02c1378c4f31edbb4b3934fa155b01f409b92
6
+ metadata.gz: dd1b076ac0cbe78783ab19b4ae43d8fb7d87bd46b76155ec08dcbdfb55638415667c1ad19ccdeff4f81e0b0482c2ef32292a1686df648ee52e0b3ce4824ea069
7
+ data.tar.gz: d34c06b627f4c1b4773829cd9e3003f3a8eb167a97acfa55be361628078d8230d8be1664d576d49eb6de9705d56f95c478ff52c56c2c656b1882d167df69e499
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decanter (3.2.1)
4
+ decanter (3.3.0)
5
5
  actionpack (>= 4.2.10)
6
6
  activesupport (~> 5.2)
7
7
  rails-html-sanitizer (>= 1.0.4)
@@ -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
- _name = [name].flatten
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[_name] = {
20
- key: options.fetch(:key, _name.first),
21
- name: _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
- args = args.to_unsafe_h.with_indifferent_access if args.class.name == 'ActionController::Parameters'
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(args) )
63
- .merge( handled_keys(args) )
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
@@ -1,3 +1,3 @@
1
1
  module Decanter
2
- VERSION = '3.2.1'.freeze
2
+ VERSION = '3.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decanter
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.1
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Francis