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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65b7c35da511837c4258f69bb240843582ad39e4f7fb769c8e73acdb5becea7e
4
- data.tar.gz: 138c89449ed2e6a2abcd5beee0b85d76142c60d0de0c7b9b8d337852a283cc1b
3
+ metadata.gz: '0902fce40d561d547f971d15c31b80c550b5d5468b7e7c64626e5a5575036893'
4
+ data.tar.gz: a6f21dfa539d694e8dc990d07cb5c686d4d404cdf9bbf4d5bfeebadc07b3c66b
5
5
  SHA512:
6
- metadata.gz: 93b3ac5f2d0da4b7bcd7212b5a0ae73fa8ee6502241e55cf23bb354d1db74b8ad49f83e58ce7ed68bb1350602712417640de7ec2bdd6a0181552a2078e119726
7
- data.tar.gz: 2ec97f4ad2626306e7c03e6e0d77a3c73d505ae4524f6093fb933bd773fdf9dd181f59c68a39122b18eea369fe0cc2b4e1c1e98697c90ed03f96e3245749bf7a
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
- Altair.converter[value.class][type].call(value)
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
@@ -0,0 +1,8 @@
1
+ def resolve_proc(value)
2
+ case value
3
+ when Proc
4
+ value.call
5
+ else
6
+ value
7
+ end
8
+ end
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.0
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