gate 0.1.0 → 0.2.0
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/.codeclimate.yml +8 -0
- data/.rubocop.yml +19 -0
- data/Gemfile +1 -1
- data/README.md +22 -4
- data/Rakefile +2 -2
- data/bin/setup +5 -0
- data/gate.gemspec +5 -5
- data/lib/gate.rb +9 -9
- data/lib/gate/coercer.rb +13 -2
- data/lib/gate/guard.rb +2 -2
- data/lib/gate/result.rb +3 -7
- data/lib/gate/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 09c3db4ff831be9cbcee9a0fc5c4666cf25e0fd6
|
4
|
+
data.tar.gz: b8fe4ff7b05976382e00b91dca04496b330cd0a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2e1837f27d8e33f1f73365c9a231f465b3e3abdcca1b036f22b7194dfd1d918341de980d492115b5e1feac1b797450d2b83356e303d4633ccd06414e77d50b1
|
7
|
+
data.tar.gz: 32fd22c8502b461615a89d6fe4b8edf6592218083ead3d796d627593b84b6fa990d1c23926c32eb557b41b8a04eba90eedff0f70446e8f439a060d16a1fa5ac6
|
data/.codeclimate.yml
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Style/Documentation:
|
2
|
+
Description: Document classes and non-namespace modules.
|
3
|
+
Enabled: false
|
4
|
+
Style/StringLiterals:
|
5
|
+
Description: Checks if uses of quotes match the configured preference.
|
6
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
|
7
|
+
Enabled: true
|
8
|
+
EnforcedStyle: double_quotes
|
9
|
+
SupportedStyles:
|
10
|
+
- single_quotes
|
11
|
+
- double_quotes
|
12
|
+
Style/StringLiteralsInInterpolation:
|
13
|
+
Description: Checks if uses of quotes inside expressions in interpolated strings
|
14
|
+
match the configured preference.
|
15
|
+
Enabled: true
|
16
|
+
EnforcedStyle: single_quotes
|
17
|
+
SupportedStyles:
|
18
|
+
- single_quotes
|
19
|
+
- double_quotes
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
# Gate
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/gate)
|
3
4
|
[](https://circleci.com/gh/monterail/gate)
|
4
5
|
[](https://gemnasium.com/monterail/gate)
|
5
6
|
[](https://codeclimate.com/github/monterail/gate)
|
6
7
|
[](https://codeclimate.com/github/monterail/gate/coverage)
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
TODO: Delete this and the text above, and describe your gem
|
9
|
+
Gate is a small library which allows you to define allowed structure for user input with required and optional parameters and to coerce them into defined types.
|
11
10
|
|
12
11
|
## Installation
|
13
12
|
|
@@ -27,7 +26,26 @@ Or install it yourself as:
|
|
27
26
|
|
28
27
|
## Usage
|
29
28
|
|
30
|
-
|
29
|
+
Define structure
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gate = Gate.rules do
|
33
|
+
required :id, :Integer
|
34
|
+
required :message do
|
35
|
+
required :title # :String by default
|
36
|
+
optional :value, :Decimal
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Verify it
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
result = gate.verify(params)
|
45
|
+
result.valid? # => true / false
|
46
|
+
result.attributes # => hash with only allowed parameters
|
47
|
+
result.errors # => hash { key => error }
|
48
|
+
```
|
31
49
|
|
32
50
|
## Development
|
33
51
|
|
data/Rakefile
CHANGED
data/bin/setup
ADDED
data/gate.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "gate/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "gate"
|
@@ -9,12 +9,12 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Jan Dudulski"]
|
10
10
|
spec.email = ["jan@dudulski.pl"]
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
12
|
+
spec.summary = "Handling user input with ease"
|
13
|
+
spec.description = "Validate and coerce user input against defined structure."
|
14
14
|
spec.homepage = "https://github.com/monterail/gate"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^test/}) }
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_runtime_dependency "coercible", "~> 1.0"
|
data/lib/gate.rb
CHANGED
@@ -8,13 +8,13 @@ module Gate
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
11
|
+
require "axiom-types"
|
12
|
+
require "coercible"
|
13
|
+
require "forwardable"
|
14
|
+
require "set"
|
15
15
|
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
16
|
+
require "gate/coercer"
|
17
|
+
require "gate/configuration"
|
18
|
+
require "gate/guard"
|
19
|
+
require "gate/result"
|
20
|
+
require "gate/version"
|
data/lib/gate/coercer.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
module Gate
|
2
2
|
class Coercer
|
3
3
|
def initialize(engine, type)
|
4
|
-
|
4
|
+
unless Axiom::Types.const_defined?(type)
|
5
|
+
fail CoercionError, "Doesn't know how to coerce into #{type}"
|
6
|
+
end
|
5
7
|
|
6
8
|
@engine = engine
|
7
9
|
@type = type
|
@@ -9,7 +11,7 @@ module Gate
|
|
9
11
|
end
|
10
12
|
|
11
13
|
def coerce(input)
|
12
|
-
engine[
|
14
|
+
engine[detect_input_type(input)].public_send(coercion_method, input)
|
13
15
|
rescue Coercible::UnsupportedCoercion
|
14
16
|
raise CoercionError, "Doesn't know how to coerce #{input} into #{type}"
|
15
17
|
end
|
@@ -17,5 +19,14 @@ module Gate
|
|
17
19
|
private
|
18
20
|
|
19
21
|
attr_reader :engine, :type, :coercion_method
|
22
|
+
|
23
|
+
def detect_input_type(input)
|
24
|
+
case input
|
25
|
+
when TrueClass, FalseClass, Array, Hash
|
26
|
+
input.class
|
27
|
+
else
|
28
|
+
String
|
29
|
+
end
|
30
|
+
end
|
20
31
|
end
|
21
32
|
end
|
data/lib/gate/guard.rb
CHANGED
@@ -16,7 +16,7 @@ module Gate
|
|
16
16
|
|
17
17
|
def _verify(input, name, rule, result)
|
18
18
|
case
|
19
|
-
when input.
|
19
|
+
when input.key?(name)
|
20
20
|
coerced = handle(input[name], name, rule)
|
21
21
|
update(result, coerced)
|
22
22
|
when configuration.required?(name)
|
@@ -28,7 +28,7 @@ module Gate
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def handle(input, name, rule)
|
31
|
-
if rule.
|
31
|
+
if rule.is_a?(Gate::Configuration)
|
32
32
|
result = Gate::Guard.new(rule).verify(input)
|
33
33
|
{ attributes: { name => result.attributes },
|
34
34
|
errors: { name => result.errors } }
|
data/lib/gate/result.rb
CHANGED
@@ -25,18 +25,14 @@ module Gate
|
|
25
25
|
private
|
26
26
|
|
27
27
|
def _errors(hash)
|
28
|
-
hash.
|
29
|
-
if v.
|
28
|
+
hash.each_with_object({}) do |(k, v), result|
|
29
|
+
if v.is_a?(Hash)
|
30
30
|
nested = _errors(v)
|
31
31
|
|
32
|
-
if nested.any?
|
33
|
-
result[k] = nested
|
34
|
-
end
|
32
|
+
result[k] = nested if nested.any?
|
35
33
|
else
|
36
34
|
result[k] = v
|
37
35
|
end
|
38
|
-
|
39
|
-
result
|
40
36
|
end
|
41
37
|
end
|
42
38
|
end
|
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.2.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-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coercible
|
@@ -115,13 +115,16 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- ".codeclimate.yml"
|
118
119
|
- ".gitignore"
|
120
|
+
- ".rubocop.yml"
|
119
121
|
- ".travis.yml"
|
120
122
|
- Gemfile
|
121
123
|
- LICENSE.txt
|
122
124
|
- README.md
|
123
125
|
- Rakefile
|
124
126
|
- bin/console
|
127
|
+
- bin/setup
|
125
128
|
- circle.yml
|
126
129
|
- gate.gemspec
|
127
130
|
- lib/gate.rb
|
@@ -150,9 +153,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
153
|
version: '0'
|
151
154
|
requirements: []
|
152
155
|
rubyforge_project:
|
153
|
-
rubygems_version: 2.
|
156
|
+
rubygems_version: 2.4.5
|
154
157
|
signing_key:
|
155
158
|
specification_version: 4
|
156
159
|
summary: Handling user input with ease
|
157
160
|
test_files: []
|
158
|
-
has_rdoc:
|