gate 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/gate/coercer.rb +6 -1
- data/lib/gate/configuration.rb +15 -10
- data/lib/gate/guard.rb +19 -4
- data/lib/gate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1951613a4b68d99db41f71852db4e2b100ded40
|
4
|
+
data.tar.gz: 1112aaf87c2fa1453e31be28aa38149b6ad167ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99c9230b9aae1f1cd7bd319ad08d93671423ab4bb11f807118a20139d4578eb420cd9399f147045025565ba3380474a6f362637957c4bc7af0bd227bc100863b
|
7
|
+
data.tar.gz: ad095bcd11b76c9fa802fc8e0b145dd9d0086a0e0f466231809136bbbd7f9da111b817764f0daa5ed5a5689e4a504dd6ab80f516c343a194bbd08750a8d9f596
|
data/README.md
CHANGED
@@ -47,6 +47,19 @@ result.attributes # => hash with only allowed parameters
|
|
47
47
|
result.errors # => hash { key => error }
|
48
48
|
```
|
49
49
|
|
50
|
+
If you need to handle `nil` values you can use `allow_nil` flag:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gate = Gate.rules do
|
54
|
+
required :id, :Integer, allow_nil: true
|
55
|
+
required :message, allow_nil: true do
|
56
|
+
required :title
|
57
|
+
optional :value, :Decimal
|
58
|
+
end
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
|
50
63
|
## Development
|
51
64
|
|
52
65
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/gate/coercer.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
module Gate
|
2
2
|
class Coercer
|
3
|
-
def initialize(engine, type)
|
3
|
+
def initialize(engine, type, allow_nil: false)
|
4
4
|
unless Axiom::Types.const_defined?(type)
|
5
5
|
fail CoercionError, "Doesn't know how to coerce into #{type}"
|
6
6
|
end
|
7
7
|
|
8
8
|
@engine = engine
|
9
9
|
@type = type
|
10
|
+
@allow_nil = allow_nil
|
10
11
|
@coercion_method = Axiom::Types.const_get(type).coercion_method
|
11
12
|
end
|
12
13
|
|
@@ -16,6 +17,10 @@ module Gate
|
|
16
17
|
raise CoercionError, "Doesn't know how to coerce #{input} into #{type}"
|
17
18
|
end
|
18
19
|
|
20
|
+
def allow_nil?
|
21
|
+
@allow_nil
|
22
|
+
end
|
23
|
+
|
19
24
|
private
|
20
25
|
|
21
26
|
attr_reader :engine, :type, :coercion_method
|
data/lib/gate/configuration.rb
CHANGED
@@ -8,12 +8,13 @@ module Gate
|
|
8
8
|
|
9
9
|
def_delegator :@rules, :reduce
|
10
10
|
|
11
|
-
def initialize(coercer
|
11
|
+
def initialize(coercer: Coercible::Coercer.new, allow_nil: false, &block)
|
12
12
|
@coercer = coercer
|
13
13
|
@required_set = Set.new
|
14
14
|
@optional_set = Set.new
|
15
15
|
@nested_set = Set.new
|
16
16
|
@rules = {}
|
17
|
+
@allow_nil = allow_nil
|
17
18
|
|
18
19
|
instance_eval(&block)
|
19
20
|
end
|
@@ -22,28 +23,32 @@ module Gate
|
|
22
23
|
required_set.include?(name)
|
23
24
|
end
|
24
25
|
|
26
|
+
def allow_nil?
|
27
|
+
@allow_nil
|
28
|
+
end
|
29
|
+
|
25
30
|
private
|
26
31
|
|
27
|
-
def required(name, type = :String, &block)
|
32
|
+
def required(name, type = :String, allow_nil: false, &block)
|
28
33
|
required_set.add(name)
|
29
|
-
register(name, type, &block)
|
34
|
+
register(name, type, allow_nil: allow_nil, &block)
|
30
35
|
end
|
31
36
|
|
32
|
-
def optional(name, type = :String, &block)
|
37
|
+
def optional(name, type = :String, allow_nil: false, &block)
|
33
38
|
optional_set.add(name)
|
34
|
-
register(name, type, &block)
|
39
|
+
register(name, type, allow_nil: allow_nil, &block)
|
35
40
|
end
|
36
41
|
|
37
|
-
def register(name, type, &block)
|
38
|
-
@rules[name] = setup_rule(name, type, &block)
|
42
|
+
def register(name, type, allow_nil:, &block)
|
43
|
+
@rules[name] = setup_rule(name, type, allow_nil: allow_nil, &block)
|
39
44
|
end
|
40
45
|
|
41
|
-
def setup_rule(name, type, &block)
|
46
|
+
def setup_rule(name, type, allow_nil:, &block)
|
42
47
|
if block_given?
|
43
48
|
nested_set.add(name)
|
44
|
-
Configuration.new(coercer, &block)
|
49
|
+
Configuration.new(coercer: coercer, allow_nil: allow_nil, &block)
|
45
50
|
else
|
46
|
-
Coercer.new(coercer, type)
|
51
|
+
Coercer.new(coercer, type, allow_nil: allow_nil)
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
data/lib/gate/guard.rb
CHANGED
@@ -28,10 +28,11 @@ module Gate
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def handle(input, name, rule)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
case
|
32
|
+
when input.nil?
|
33
|
+
coerce_nil(name, rule)
|
34
|
+
when rule.is_a?(Gate::Configuration)
|
35
|
+
coerce_nested(input, name, rule)
|
35
36
|
else
|
36
37
|
coerce(input, name, rule)
|
37
38
|
end
|
@@ -44,6 +45,20 @@ module Gate
|
|
44
45
|
{ errors: { name => :coercion_error } }
|
45
46
|
end
|
46
47
|
|
48
|
+
def coerce_nested(input, name, rule)
|
49
|
+
result = Gate::Guard.new(rule).verify(input)
|
50
|
+
{ attributes: { name => result.attributes },
|
51
|
+
errors: { name => result.errors } }
|
52
|
+
end
|
53
|
+
|
54
|
+
def coerce_nil(name, rule)
|
55
|
+
if rule.allow_nil?
|
56
|
+
{ attributes: { name => nil } }
|
57
|
+
else
|
58
|
+
{ errors: { name => :nil_not_allowed } }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
47
62
|
def update(result, attributes: {}, errors: {})
|
48
63
|
{ attributes: result.attributes.merge(attributes),
|
49
64
|
errors: result.errors.merge(errors) }
|
data/lib/gate/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Dudulski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coercible
|