decanter 1.0.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +5 -7
- data/README.md +23 -0
- data/lib/decanter.rb +1 -0
- data/lib/decanter/core.rb +39 -6
- data/lib/decanter/exceptions.rb +7 -0
- data/lib/decanter/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de971a9c8d08bf65660675e95ca43de42cddf43b23b3d3445106da15cf53b6c8
|
4
|
+
data.tar.gz: 13feb40fcd6deaa283ed88242dd6b9f6920c251d292f373a929f7eab1b273579
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b354554c39f316a26d12b0b936c051ee1ecbaf944450486dd93114f6b0c4ed96ae1dbac9399c57e8c37daef177175a25749e5bdb207f725e1c43e3f30c94527
|
7
|
+
data.tar.gz: a7f12000a741edb01768496935c1870cce41613c7e2ae2f842da6330892e9b22c4154fb504bb93398cdc25a94a864acc888663a2268c47162896581c4b569cd3
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
decanter (1.0.
|
4
|
+
decanter (1.0.3)
|
5
5
|
actionpack (>= 4.2.10)
|
6
6
|
activesupport
|
7
7
|
|
@@ -40,15 +40,14 @@ GEM
|
|
40
40
|
loofah (2.1.1)
|
41
41
|
crass (~> 1.0.2)
|
42
42
|
nokogiri (>= 1.5.9)
|
43
|
-
method_source (0.
|
43
|
+
method_source (0.9.0)
|
44
44
|
mini_portile2 (2.3.0)
|
45
45
|
minitest (5.10.3)
|
46
46
|
nokogiri (1.8.1)
|
47
47
|
mini_portile2 (~> 2.3.0)
|
48
|
-
pry (0.
|
48
|
+
pry (0.11.3)
|
49
49
|
coderay (~> 1.1.0)
|
50
|
-
method_source (~> 0.
|
51
|
-
slop (~> 3.4)
|
50
|
+
method_source (~> 0.9.0)
|
52
51
|
rack (1.6.8)
|
53
52
|
rack-test (0.6.3)
|
54
53
|
rack (>= 1.0)
|
@@ -88,7 +87,6 @@ GEM
|
|
88
87
|
json (>= 1.8, < 3)
|
89
88
|
simplecov-html (~> 0.10.0)
|
90
89
|
simplecov-html (0.10.2)
|
91
|
-
slop (3.6.0)
|
92
90
|
thor (0.20.0)
|
93
91
|
thread_safe (0.3.6)
|
94
92
|
tzinfo (1.2.4)
|
@@ -107,4 +105,4 @@ DEPENDENCIES
|
|
107
105
|
simplecov (~> 0.15.1)
|
108
106
|
|
109
107
|
BUNDLED WITH
|
110
|
-
1.
|
108
|
+
1.15.2
|
data/README.md
CHANGED
@@ -40,6 +40,18 @@ class TripDecanter < Decanter::Base
|
|
40
40
|
end
|
41
41
|
```
|
42
42
|
|
43
|
+
By default, Decanter will use the [default parser](https://github.com/LaunchPadLab/decanter#default-parsers) that matches your input data type.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
input :name, :string #=> StringParser
|
47
|
+
```
|
48
|
+
|
49
|
+
To reference a custom or modified parser,
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
input :name, :string, :custom_string_parser
|
53
|
+
```
|
54
|
+
|
43
55
|
In your controller:
|
44
56
|
|
45
57
|
```ruby
|
@@ -491,3 +503,14 @@ Decanter.configuration.strict = true
|
|
491
503
|
|
492
504
|
Likewise, you can put the above code in a specific environment configuration.
|
493
505
|
|
506
|
+
Decanter Exceptions
|
507
|
+
---
|
508
|
+
|
509
|
+
- MissingRequiredInputValue
|
510
|
+
|
511
|
+
Raised when required inputs have been enabled, but provided arguments to `decant()` do not contain values for those required inputs.
|
512
|
+
|
513
|
+
- UnhandledKeysError
|
514
|
+
|
515
|
+
Raised when there are unhandled keys.
|
516
|
+
|
data/lib/decanter.rb
CHANGED
data/lib/decanter/core.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
1
3
|
module Decanter
|
2
4
|
module Core
|
3
|
-
|
5
|
+
|
4
6
|
def self.included(base)
|
5
7
|
base.extend(ClassMethods)
|
6
8
|
end
|
7
9
|
|
8
10
|
module ClassMethods
|
9
|
-
|
11
|
+
|
10
12
|
def input(name, parsers=nil, **options)
|
11
13
|
|
12
14
|
_name = [name].flatten
|
@@ -49,17 +51,48 @@ module Decanter
|
|
49
51
|
end
|
50
52
|
|
51
53
|
def strict(mode)
|
52
|
-
raise(
|
54
|
+
raise(ArgumentError, "#{self.name}: Unknown strict value #{mode}") unless [:with_exception, true, false].include? mode
|
53
55
|
@strict_mode = mode
|
54
56
|
end
|
55
57
|
|
56
58
|
def decant(args)
|
57
|
-
return
|
59
|
+
return handle_empty_args if args.blank?
|
60
|
+
return empty_required_input_error unless
|
61
|
+
required_input_values_present?(args)
|
58
62
|
args = args.to_unsafe_h.with_indifferent_access if args.class.name == 'ActionController::Parameters'
|
59
63
|
{}.merge( unhandled_keys(args) )
|
60
64
|
.merge( handled_keys(args) )
|
61
65
|
end
|
62
66
|
|
67
|
+
def handle_empty_args
|
68
|
+
any_inputs_required? ? empty_args_error : {}
|
69
|
+
end
|
70
|
+
|
71
|
+
def any_inputs_required?
|
72
|
+
required_inputs.any?
|
73
|
+
end
|
74
|
+
|
75
|
+
def required_inputs
|
76
|
+
handlers.map do |h|
|
77
|
+
options = h.last[:options]
|
78
|
+
h.first.first if options && options[:required]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def required_input_values_present?(args={})
|
83
|
+
required_inputs.all? do |input|
|
84
|
+
args.keys.include?(input)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def empty_required_input_error
|
89
|
+
raise(MissingRequiredInputValue, 'Required inputs have been declared, but no values for those inputs were passed.')
|
90
|
+
end
|
91
|
+
|
92
|
+
def empty_args_error
|
93
|
+
raise(ArgumentError, 'Decanter has required inputs but no values were passed')
|
94
|
+
end
|
95
|
+
|
63
96
|
# protected
|
64
97
|
|
65
98
|
def unhandled_keys(args)
|
@@ -76,7 +109,7 @@ module Decanter
|
|
76
109
|
p "#{self.name} ignoring unhandled keys: #{unhandled_keys.join(', ')}."
|
77
110
|
{}
|
78
111
|
when :with_exception
|
79
|
-
raise
|
112
|
+
raise(UnhandledKeysError, "#{self.name} received unhandled keys: #{unhandled_keys.join(', ')}.")
|
80
113
|
else
|
81
114
|
args.select { |key| unhandled_keys.include? key }
|
82
115
|
end
|
@@ -174,7 +207,7 @@ module Decanter
|
|
174
207
|
end
|
175
208
|
end
|
176
209
|
|
177
|
-
def handlers
|
210
|
+
def handlers
|
178
211
|
@handlers ||= {}
|
179
212
|
end
|
180
213
|
|
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: 1.
|
4
|
+
version: 1.1.4
|
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: 2017-12-
|
12
|
+
date: 2017-12-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -146,6 +146,7 @@ files:
|
|
146
146
|
- lib/decanter/base.rb
|
147
147
|
- lib/decanter/configuration.rb
|
148
148
|
- lib/decanter/core.rb
|
149
|
+
- lib/decanter/exceptions.rb
|
149
150
|
- lib/decanter/extensions.rb
|
150
151
|
- lib/decanter/parser.rb
|
151
152
|
- lib/decanter/parser/base.rb
|