decanter 3.1.2 → 3.2.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/README.md +19 -0
- data/lib/decanter/core.rb +24 -8
- 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: bb1e68fce23331df62b4e65bbdb40368a80bf077dd0b7685aa7486be180da948
|
4
|
+
data.tar.gz: 01b15f9d9c510b58575045fa1a726519a8729db05cf330dfc4f100de57a2b3f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1a36d91c8ba49a573c0b40caf11a45a7f2ea7c8b2891e69d314cee7564f463fe165db76acc9a0e8e77e1f5850e01e06c7f8153ac7dd48e41c1513acabe1e9af
|
7
|
+
data.tar.gz: c57d191795ed8fe7209b058b526aee19d0441360475448245e57119d56177681e3c0e351be2101ecce37d1ad0e4b3e6f33b5a60af86df253cefe902292851da0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -220,6 +220,25 @@ end
|
|
220
220
|
|
221
221
|
_Note: we recommend using [Active Record validations](https://guides.rubyonrails.org/active_record_validations.html) to check for presence of an attribute, rather than using the `required` option. This method is intended for use in non-RESTful routes or cases where Active Record validations are not available._
|
222
222
|
|
223
|
+
|
224
|
+
### Default values
|
225
|
+
|
226
|
+
If you provide the option `:default_value` for an input in your decanter, the input key will be initialized with the given default value. Input keys not found in the incoming data parameters will be set to the provided default rather than ignoring the missing key. Note: `nil` and empty keys will not be overridden.
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
class TripDecanter < Decanter::Base
|
230
|
+
input :name, :string
|
231
|
+
input :destination, :string, default_value: 'Chicago'
|
232
|
+
end
|
233
|
+
|
234
|
+
```
|
235
|
+
|
236
|
+
```
|
237
|
+
TripDecanter.decant({ name: 'Vacation 2020' })
|
238
|
+
=> { name: 'Vacation 2020', destination: 'Chicago' }
|
239
|
+
|
240
|
+
```
|
241
|
+
|
223
242
|
### Global configuration
|
224
243
|
|
225
244
|
You can generate a local copy of the default configuration with `rails generate decanter:install`. This will create an initializer where you can do global configuration:
|
data/lib/decanter/core.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
module Decanter
|
2
2
|
module Core
|
3
|
-
|
3
|
+
DEFAULT_VALUE_KEY = :default_value
|
4
|
+
|
4
5
|
def self.included(base)
|
5
6
|
base.extend(ClassMethods)
|
6
7
|
end
|
7
8
|
|
8
9
|
module ClassMethods
|
9
|
-
|
10
|
+
|
10
11
|
def input(name, parsers=nil, **options)
|
11
12
|
|
12
13
|
_name = [name].flatten
|
@@ -57,11 +58,26 @@ module Decanter
|
|
57
58
|
return handle_empty_args if args.blank?
|
58
59
|
return empty_required_input_error unless required_input_keys_present?(args)
|
59
60
|
args = args.to_unsafe_h.with_indifferent_access if args.class.name == 'ActionController::Parameters'
|
60
|
-
{}.merge(
|
61
|
+
{}.merge( default_keys )
|
62
|
+
.merge( unhandled_keys(args) )
|
61
63
|
.merge( handled_keys(args) )
|
62
64
|
end
|
63
65
|
|
64
|
-
def
|
66
|
+
def default_keys
|
67
|
+
# return keys with provided default value when key is not defined within incoming args
|
68
|
+
default_result = default_value_inputs
|
69
|
+
.map { |input| [input[:key], input[:options][DEFAULT_VALUE_KEY]] }
|
70
|
+
.to_h
|
71
|
+
|
72
|
+
# parse default values
|
73
|
+
handled_keys(default_result)
|
74
|
+
end
|
75
|
+
|
76
|
+
def default_value_inputs
|
77
|
+
handlers.values.select { |input| input[:options].key?(DEFAULT_VALUE_KEY) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def handle_empty_args
|
65
81
|
any_inputs_required? ? empty_args_error : {}
|
66
82
|
end
|
67
83
|
|
@@ -73,7 +89,7 @@ module Decanter
|
|
73
89
|
handlers.map do |h|
|
74
90
|
options = h.last[:options]
|
75
91
|
h.first.first if options && options[:required]
|
76
|
-
end
|
92
|
+
end
|
77
93
|
end
|
78
94
|
|
79
95
|
def required_input_keys_present?(args={})
|
@@ -100,7 +116,7 @@ module Decanter
|
|
100
116
|
keys_to_ignore -
|
101
117
|
handlers.values
|
102
118
|
.select { |handler| handler[:type] != :input }
|
103
|
-
.map { |handler| "#{handler[:name]}_attributes".to_sym }
|
119
|
+
.map { |handler| "#{handler[:name]}_attributes".to_sym }
|
104
120
|
|
105
121
|
return {} unless unhandled_keys.any?
|
106
122
|
raise(UnhandledKeysError, "#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.") if strict_mode
|
@@ -185,13 +201,13 @@ module Decanter
|
|
185
201
|
def parse(key, parsers, value, options)
|
186
202
|
return { key => value } unless parsers
|
187
203
|
if options[:required] && value_missing?(value)
|
188
|
-
raise ArgumentError.new("No value for required argument: #{key}")
|
204
|
+
raise ArgumentError.new("No value for required argument: #{key}")
|
189
205
|
end
|
190
206
|
parser_classes = Parser.parsers_for(parsers)
|
191
207
|
Parser.compose_parsers(parser_classes).parse(key, value, options)
|
192
208
|
end
|
193
209
|
|
194
|
-
def handlers
|
210
|
+
def handlers
|
195
211
|
@handlers ||= {}
|
196
212
|
end
|
197
213
|
|
data/lib/decanter/version.rb
CHANGED