decanter 0.8.2 → 0.9.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 +2 -2
- data/README.md +31 -5
- data/lib/decanter.rb +20 -10
- data/lib/decanter/core.rb +11 -2
- data/lib/decanter/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59c4f720f9e7e8d34a163d6b06314adb05aad75e
|
4
|
+
data.tar.gz: 57fb16ed8e5d10a7b265dfe3a1b201867a938017
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 950d5a5e1a6110a19b7acb242fb8baaacd526c969caff7966fa6122c3f3ca53c96e3a6fe7dc8ad74c202ff6986384a8a2f52649282a2c1fce26bd60e72bf634b
|
7
|
+
data.tar.gz: 10666cd624edba38e980e4e5d90684e62bf8b452905e4a8b882fe0bbfc32e1e5aae237a0e88a3988358a9987195fdd8a0fe419fb07e497e7011fa74d11a15fc9
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -422,27 +422,53 @@ No Need for Strong Params
|
|
422
422
|
|
423
423
|
Since you are already defining your expected inputs in Decanter, you really don't need strong params anymore.
|
424
424
|
|
425
|
-
|
425
|
+
Note: starting with version 0.7.2, the default strict mode is ```:with_exception```. You can modify your default strict mode in your configuration file (see the "Configuration" section below).
|
426
|
+
|
427
|
+
#### Mode: with_exception (default mode)
|
428
|
+
|
429
|
+
To raise exceptions when parameters arrive in your Decanter that you didn't expect:
|
426
430
|
|
427
431
|
```ruby
|
428
432
|
class TripDecanter < Decanter::Base
|
429
|
-
strict
|
433
|
+
strict :with_exception
|
430
434
|
|
431
435
|
input :name
|
432
436
|
end
|
433
437
|
```
|
434
438
|
|
435
|
-
|
439
|
+
#### Mode: strict
|
440
|
+
|
441
|
+
In order to tell Decanter to ignore the params not defined in your Decanter, just add the ```strict``` flag to your Decanters:
|
436
442
|
|
437
443
|
```ruby
|
438
444
|
class TripDecanter < Decanter::Base
|
439
|
-
strict
|
445
|
+
strict true
|
440
446
|
|
441
447
|
input :name
|
442
448
|
end
|
443
449
|
```
|
444
450
|
|
445
|
-
|
451
|
+
#### Requiring Params
|
452
|
+
|
453
|
+
If you provide the option `:required` for an input in your decanter, an exception will be thrown if the parameters is nil or an empty string.
|
454
|
+
|
455
|
+
```ruby
|
456
|
+
class TripDecanter < Decanter::Base
|
457
|
+
input :name, :string, required: true
|
458
|
+
end
|
459
|
+
```
|
460
|
+
|
461
|
+
#### Ignoring Params
|
462
|
+
|
463
|
+
If you anticipate your decanter will receive certain params that you simply want to ignore and therefore do not want Decanter to raise an exception, you can do so by calling the `ignore` method:
|
464
|
+
|
465
|
+
```ruby
|
466
|
+
class TripDecanter < Decanter::Base
|
467
|
+
ignore :created_at, :updated_at
|
468
|
+
|
469
|
+
input :name, :string
|
470
|
+
end
|
471
|
+
```
|
446
472
|
|
447
473
|
Configuration
|
448
474
|
---
|
data/lib/decanter.rb
CHANGED
@@ -5,14 +5,20 @@ module Decanter
|
|
5
5
|
class << self
|
6
6
|
|
7
7
|
def decanter_for(klass_or_sym)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
decanter_name =
|
9
|
+
case klass_or_sym
|
10
|
+
when Class
|
11
|
+
klass_or_sym.name
|
12
|
+
when Symbol
|
13
|
+
klass_or_sym.to_s.singularize.camelize
|
14
|
+
else
|
15
|
+
raise ArgumentError.new("cannot lookup decanter for #{klass_or_sym} with class #{klass_or_sym.class}")
|
16
|
+
end.concat('Decanter')
|
17
|
+
begin
|
18
|
+
decanter_name.constantize
|
19
|
+
rescue
|
20
|
+
raise NameError.new("uninitialized constant #{decanter_name}")
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
def decanter_from(klass_or_string)
|
@@ -21,7 +27,11 @@ module Decanter
|
|
21
27
|
when Class
|
22
28
|
klass_or_string
|
23
29
|
when String
|
24
|
-
|
30
|
+
begin
|
31
|
+
klass_or_string.constantize
|
32
|
+
rescue
|
33
|
+
raise NameError.new("uninitialized constant #{klass_or_string}")
|
34
|
+
end
|
25
35
|
else
|
26
36
|
raise ArgumentError.new("cannot find decanter from #{klass_or_string} with class #{klass_or_string.class}")
|
27
37
|
end
|
@@ -51,4 +61,4 @@ require 'decanter/core'
|
|
51
61
|
require 'decanter/base'
|
52
62
|
require 'decanter/extensions'
|
53
63
|
require 'decanter/parser'
|
54
|
-
require 'decanter/railtie' if defined?(::Rails)
|
64
|
+
require 'decanter/railtie' if defined?(::Rails)
|
data/lib/decanter/core.rb
CHANGED
@@ -44,6 +44,10 @@ module Decanter
|
|
44
44
|
}
|
45
45
|
end
|
46
46
|
|
47
|
+
def ignore(*args)
|
48
|
+
keys_to_ignore.push(*args)
|
49
|
+
end
|
50
|
+
|
47
51
|
def strict(mode)
|
48
52
|
raise( ArgumentError.new("#{self.name}: Unknown strict value #{mode}")) unless [:with_exception, true, false].include? mode
|
49
53
|
@strict_mode = mode
|
@@ -61,16 +65,17 @@ module Decanter
|
|
61
65
|
def unhandled_keys(args)
|
62
66
|
unhandled_keys = args.keys.map(&:to_sym) -
|
63
67
|
handlers.keys.flatten.uniq -
|
68
|
+
keys_to_ignore -
|
64
69
|
handlers.values
|
65
70
|
.select { |handler| handler[:type] != :input }
|
66
|
-
.map { |handler| "#{handler[:name]}_attributes".to_sym }
|
71
|
+
.map { |handler| "#{handler[:name]}_attributes".to_sym }
|
67
72
|
|
68
73
|
if unhandled_keys.any?
|
69
74
|
case strict_mode
|
70
75
|
when true
|
71
76
|
p "#{self.name} ignoring unhandled keys: #{unhandled_keys.join(', ')}."
|
72
77
|
{}
|
73
|
-
when :with_exception
|
78
|
+
when :with_exception
|
74
79
|
raise ArgumentError.new("#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.")
|
75
80
|
else
|
76
81
|
args.select { |key| unhandled_keys.include? key }
|
@@ -173,6 +178,10 @@ module Decanter
|
|
173
178
|
@handlers ||= {}
|
174
179
|
end
|
175
180
|
|
181
|
+
def keys_to_ignore
|
182
|
+
@keys_to_ignore ||= []
|
183
|
+
end
|
184
|
+
|
176
185
|
def strict_mode
|
177
186
|
@strict_mode.nil? ? Decanter.configuration.strict : @strict_mode
|
178
187
|
end
|
data/lib/decanter/version.rb
CHANGED
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: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Francis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-06-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
version: '0'
|
165
165
|
requirements: []
|
166
166
|
rubyforge_project:
|
167
|
-
rubygems_version: 2.
|
167
|
+
rubygems_version: 2.5.1
|
168
168
|
signing_key:
|
169
169
|
specification_version: 4
|
170
170
|
summary: Form Parser for Rails
|