bodhi-slam 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bodhi-slam.rb +13 -109
- data/lib/bodhi-slam/context.rb +37 -51
- data/lib/bodhi-slam/enumerations.rb +35 -0
- data/lib/bodhi-slam/errors.rb +81 -4
- data/lib/bodhi-slam/resource.rb +201 -64
- data/lib/bodhi-slam/types.rb +191 -0
- data/lib/bodhi-slam/validations.rb +141 -0
- data/lib/bodhi-slam/validators.rb +60 -0
- data/lib/bodhi-slam/validators/blank.rb +24 -0
- data/lib/bodhi-slam/validators/email.rb +24 -0
- data/lib/bodhi-slam/validators/matches.rb +27 -0
- data/lib/bodhi-slam/validators/max.rb +27 -0
- data/lib/bodhi-slam/validators/min.rb +27 -0
- data/lib/bodhi-slam/validators/multi.rb +14 -0
- data/lib/bodhi-slam/validators/not_empty.rb +20 -0
- data/lib/bodhi-slam/validators/required.rb +14 -0
- data/lib/bodhi-slam/validators/type.rb +123 -0
- data/lib/bodhi-slam/validators/url.rb +24 -0
- metadata +61 -5
@@ -0,0 +1,27 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class MaxValidator < Validator
|
3
|
+
attr_reader :value
|
4
|
+
|
5
|
+
def initialize(value)
|
6
|
+
@value = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate(record, attribute, value)
|
10
|
+
unless value.nil?
|
11
|
+
|
12
|
+
if value.is_a?(Array)
|
13
|
+
unless value.empty?
|
14
|
+
record.errors.add(attribute, "must only contain values less than or equal to #{@value}") unless value.delete_if{ |v| v <= @value }.empty?
|
15
|
+
end
|
16
|
+
else
|
17
|
+
record.errors.add(attribute, "must be less than or equal to #{@value}") unless value <= @value
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_options
|
24
|
+
{self.to_sym => @value}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class MinValidator < Validator
|
3
|
+
attr_reader :value
|
4
|
+
|
5
|
+
def initialize(value)
|
6
|
+
@value = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate(record, attribute, value)
|
10
|
+
unless value.nil?
|
11
|
+
|
12
|
+
if value.is_a?(Array)
|
13
|
+
unless value.empty?
|
14
|
+
record.errors.add(attribute, "must only contain values greater than or equal to #{@value}") unless value.delete_if{ |v| v >= @value }.empty?
|
15
|
+
end
|
16
|
+
else
|
17
|
+
record.errors.add(attribute, "must be greater than or equal to #{@value}") unless value >= @value
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_options
|
24
|
+
{self.to_sym => @value}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class MultiValidator < Validator
|
3
|
+
|
4
|
+
def initialize(value); end
|
5
|
+
|
6
|
+
def validate(record, attribute, value)
|
7
|
+
record.errors.add(attribute, "must be an array") unless value.is_a? Array
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_options
|
11
|
+
{self.to_sym => true}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class IsNotEmptyValidator < Validator
|
3
|
+
|
4
|
+
def initialize(value); end
|
5
|
+
|
6
|
+
def validate(record, attribute, value)
|
7
|
+
unless value.nil?
|
8
|
+
|
9
|
+
if value.is_a?(Array)
|
10
|
+
record.errors.add(attribute, "must not be empty") if value.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_options
|
17
|
+
{self.to_sym => true}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class RequiredValidator < Validator
|
3
|
+
|
4
|
+
def initialize(value); end
|
5
|
+
|
6
|
+
def validate(record, attribute, value)
|
7
|
+
record.errors.add(attribute, "is required") if value.nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_options
|
11
|
+
{self.to_sym => true}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class TypeValidator < Validator
|
3
|
+
attr_reader :type, :reference
|
4
|
+
|
5
|
+
def initialize(type, reference=nil)
|
6
|
+
if type.nil?
|
7
|
+
raise ArgumentError.new("Expected :type to not be nil")
|
8
|
+
end
|
9
|
+
|
10
|
+
@type = type
|
11
|
+
@reference = reference if reference
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(record, attribute, value)
|
15
|
+
unless value.nil?
|
16
|
+
|
17
|
+
# Default values for comparators and messages
|
18
|
+
klass = nil
|
19
|
+
single_comparator = ->(item){ item.is_a? klass }
|
20
|
+
array_comparator = ->(items){ items.select{ |item| !item.is_a?(klass) }.empty? }
|
21
|
+
single_message = "must be a #{@type}"
|
22
|
+
array_message = "must contain only #{@type}s"
|
23
|
+
|
24
|
+
# Check what the given type is, and assign the correct comparator and messages
|
25
|
+
case @type
|
26
|
+
when "GeoJSON"
|
27
|
+
klass = Hash
|
28
|
+
when "DateTime"
|
29
|
+
single_comparator = lambda do |item|
|
30
|
+
begin
|
31
|
+
DateTime.iso8601(item)
|
32
|
+
rescue
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
array_comparator = lambda do |items|
|
37
|
+
begin
|
38
|
+
items.collect{ |item| DateTime.iso8601(item) }
|
39
|
+
rescue
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
when "Object"
|
44
|
+
klass = Hash
|
45
|
+
single_message = "must be a JSON object"
|
46
|
+
array_message = "must contain only JSON objects"
|
47
|
+
when "Real"
|
48
|
+
klass = Float
|
49
|
+
when "Boolean"
|
50
|
+
single_comparator = ->(item){ item.is_a?(TrueClass) || item.is_a?(FalseClass) }
|
51
|
+
array_comparator = ->(items){ items.delete_if{ |item| item.is_a?(TrueClass) || item.is_a?(FalseClass) }.empty? }
|
52
|
+
when "Enumerated"
|
53
|
+
if @reference.nil?
|
54
|
+
raise RuntimeError.new("Enumerated reference is missing! Cannot validate #{record.class}.#{attribute}=#{value}")
|
55
|
+
end
|
56
|
+
|
57
|
+
single_message = "must be a #{@reference}"
|
58
|
+
array_message = "must contain only #{@reference} objects"
|
59
|
+
|
60
|
+
name = @reference.split(".")[0]
|
61
|
+
field = @reference.split(".")[1]
|
62
|
+
|
63
|
+
enumeration = Bodhi::Enumeration.cache[name.to_sym]
|
64
|
+
if field.nil?
|
65
|
+
single_comparator = ->(item){ enumeration.values.include?(item) }
|
66
|
+
array_comparator = ->(items){ items.select{ |item| !enumeration.values.include?(item) }.empty? }
|
67
|
+
else
|
68
|
+
flattened_values = enumeration.values.map{|val| val[field.to_sym] }
|
69
|
+
single_comparator = ->(item){ flattened_values.include?(item) }
|
70
|
+
array_comparator = ->(items){ items.select{ |item| !flattened_values.include?(item) }.empty? }
|
71
|
+
end
|
72
|
+
else # type is an embedded type
|
73
|
+
klass = Object.const_get(@type)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Do the validations and add any error messages
|
77
|
+
if value.is_a?(Array)
|
78
|
+
if !value.empty?
|
79
|
+
if !array_comparator.call(value)
|
80
|
+
record.errors.add(attribute, array_message)
|
81
|
+
else # validate each value in the array if it responds to :valid?
|
82
|
+
value.each_with_index do |item, index|
|
83
|
+
if item.respond_to?(:valid?)
|
84
|
+
item.valid?
|
85
|
+
if item.errors.any?
|
86
|
+
item.errors.each do |error_attribute, message|
|
87
|
+
full_name = "#{attribute}[#{index}].#{error_attribute}".to_sym
|
88
|
+
record.errors.add(full_name, message)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
else
|
96
|
+
if !single_comparator.call(value)
|
97
|
+
record.errors.add(attribute, single_message)
|
98
|
+
else # validate the value if it responds to :valid?
|
99
|
+
if value.respond_to?(:valid?)
|
100
|
+
value.valid?
|
101
|
+
if value.errors.any?
|
102
|
+
value.errors.each do |error_attribute, error|
|
103
|
+
full_path = "#{attribute}.#{error_attribute}".to_sym
|
104
|
+
record.errors.add(full_path, error)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Party time, excellent!
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def to_options
|
116
|
+
if @reference.nil?
|
117
|
+
{self.to_sym => @type}
|
118
|
+
else
|
119
|
+
{self.to_sym => @type, ref: @reference}
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Bodhi
|
2
|
+
class UrlValidator < Validator
|
3
|
+
|
4
|
+
def initialize(value); end
|
5
|
+
|
6
|
+
def validate(record, attribute, value)
|
7
|
+
unless value.nil?
|
8
|
+
|
9
|
+
if value.is_a?(Array)
|
10
|
+
unless value.empty?
|
11
|
+
record.errors.add(attribute, "must contain only valid URLs") unless value.delete_if{ |v| v =~ /\A#{URI::regexp}\z/ }.empty?
|
12
|
+
end
|
13
|
+
else
|
14
|
+
record.errors.add(attribute, "must be a valid URL") unless value =~ /\A#{URI::regexp}\z/
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_options
|
21
|
+
{self.to_sym => true}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bodhi-slam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- willdavis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-http-persistent
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.9'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: factory_girl
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,7 +80,35 @@ dependencies:
|
|
66
80
|
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
|
-
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: dotenv
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: BodhiSlam is an ORM for the Bodhi API and helps with randomly generating.
|
70
112
|
email: will.davis@hotschedules.com
|
71
113
|
executables: []
|
72
114
|
extensions: []
|
@@ -74,8 +116,22 @@ extra_rdoc_files: []
|
|
74
116
|
files:
|
75
117
|
- lib/bodhi-slam.rb
|
76
118
|
- lib/bodhi-slam/context.rb
|
119
|
+
- lib/bodhi-slam/enumerations.rb
|
77
120
|
- lib/bodhi-slam/errors.rb
|
78
121
|
- lib/bodhi-slam/resource.rb
|
122
|
+
- lib/bodhi-slam/types.rb
|
123
|
+
- lib/bodhi-slam/validations.rb
|
124
|
+
- lib/bodhi-slam/validators.rb
|
125
|
+
- lib/bodhi-slam/validators/blank.rb
|
126
|
+
- lib/bodhi-slam/validators/email.rb
|
127
|
+
- lib/bodhi-slam/validators/matches.rb
|
128
|
+
- lib/bodhi-slam/validators/max.rb
|
129
|
+
- lib/bodhi-slam/validators/min.rb
|
130
|
+
- lib/bodhi-slam/validators/multi.rb
|
131
|
+
- lib/bodhi-slam/validators/not_empty.rb
|
132
|
+
- lib/bodhi-slam/validators/required.rb
|
133
|
+
- lib/bodhi-slam/validators/type.rb
|
134
|
+
- lib/bodhi-slam/validators/url.rb
|
79
135
|
homepage:
|
80
136
|
licenses:
|
81
137
|
- MIT
|
@@ -99,5 +155,5 @@ rubyforge_project:
|
|
99
155
|
rubygems_version: 2.2.2
|
100
156
|
signing_key:
|
101
157
|
specification_version: 4
|
102
|
-
summary:
|
158
|
+
summary: Ruby bindings for the Bodhi API
|
103
159
|
test_files: []
|