altair 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/altair/conversion_error.rb +14 -0
- data/lib/altair/field.rb +19 -1
- data/lib/altair/validation_error.rb +14 -0
- data/lib/altair.rb +19 -4
- data/lib/utils.rb +8 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0902fce40d561d547f971d15c31b80c550b5d5468b7e7c64626e5a5575036893'
|
4
|
+
data.tar.gz: a6f21dfa539d694e8dc990d07cb5c686d4d404cdf9bbf4d5bfeebadc07b3c66b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0456b48dd5bb4e6d8e5f0f778154249275157a483ed6397da64b16af2410017923f9d2768e2f7d13a3fd9727e5d41b6a067c594b84f03d418535efc64168454
|
7
|
+
data.tar.gz: 7cc62af703ef1425ab49bde4fd903aaca715cc77826416cf633a6153488d5bad32dc01489d564f87b7a290a06cbf63db09584f6157ddd2e7a8185a52faa7dbec
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Altair
|
2
|
+
class ConversionError < StandardError
|
3
|
+
attr_reader :type, :value
|
4
|
+
|
5
|
+
def initialize(type, value)
|
6
|
+
@type = type
|
7
|
+
@value = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def message
|
11
|
+
"Cannot convert `#{value}` (of class #{value.class}) to #{type}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/altair/field.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'utils'
|
2
|
+
require 'altair/validation_error'
|
3
|
+
require 'altair/conversion_error'
|
4
|
+
|
1
5
|
module Altair
|
2
6
|
class Field
|
3
7
|
attr_accessor :type, :options
|
@@ -8,7 +12,21 @@ module Altair
|
|
8
12
|
end
|
9
13
|
|
10
14
|
def parse!(value)
|
11
|
-
|
15
|
+
validate! begin
|
16
|
+
Altair.converter[value.class][type].call(value)
|
17
|
+
rescue
|
18
|
+
raise ConversionError.new(type, value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate!(value)
|
23
|
+
Altair.validators.each do |name, validator|
|
24
|
+
if options.has_key? name
|
25
|
+
raise ValidationError.new(name, value) unless validator.call(resolve_proc(options[name]), value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
value
|
12
30
|
end
|
13
31
|
end
|
14
32
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Altair
|
2
|
+
class ValidationError < StandardError
|
3
|
+
attr_reader :validator, :value
|
4
|
+
|
5
|
+
def initialize(validator, value)
|
6
|
+
@validator = validator
|
7
|
+
@value = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def message
|
11
|
+
"Validator :#{validator} failed against `#{value}` (of class #{value.class})"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/altair.rb
CHANGED
@@ -1,14 +1,31 @@
|
|
1
1
|
require 'boolean'
|
2
2
|
require 'altair/field'
|
3
3
|
require 'altair/document'
|
4
|
+
require 'altair/validation_error'
|
5
|
+
require 'altair/conversion_error'
|
4
6
|
|
5
7
|
module Altair
|
6
|
-
attr_accessor :converter
|
8
|
+
attr_accessor :converter, :validators
|
7
9
|
|
8
10
|
def config(options = {})
|
11
|
+
@validators = options[:validators] || default_validators
|
9
12
|
@converter = options[:converter] || default_converter
|
10
13
|
end
|
11
14
|
|
15
|
+
def define_validator(name, &block)
|
16
|
+
validators[name] = block
|
17
|
+
end
|
18
|
+
|
19
|
+
def default_validators
|
20
|
+
{
|
21
|
+
min: Proc.new { |bound, value| bound <= value },
|
22
|
+
max: Proc.new { |bound, value| value <= bound },
|
23
|
+
more_than: Proc.new { |bound, value| bound < value },
|
24
|
+
less_than: Proc.new { |bound, value| value < bound },
|
25
|
+
in: Proc.new { |list, value| list.include? value }
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
12
29
|
def default_converter
|
13
30
|
{
|
14
31
|
String => {
|
@@ -42,6 +59,4 @@ module Altair
|
|
42
59
|
end
|
43
60
|
|
44
61
|
extend self
|
45
|
-
end
|
46
|
-
|
47
|
-
Altair.config
|
62
|
+
end
|
data/lib/utils.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: altair
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Cabrera
|
@@ -31,9 +31,12 @@ extensions: []
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
33
|
- lib/altair.rb
|
34
|
+
- lib/altair/conversion_error.rb
|
34
35
|
- lib/altair/document.rb
|
35
36
|
- lib/altair/field.rb
|
37
|
+
- lib/altair/validation_error.rb
|
36
38
|
- lib/boolean.rb
|
39
|
+
- lib/utils.rb
|
37
40
|
homepage: https://gitlab.com/armizh/altair
|
38
41
|
licenses:
|
39
42
|
- BSD-2-Clause
|