bodhi-slam 0.2.3 → 0.2.5
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/lib/bodhi-slam/enumerations.rb +8 -1
- data/lib/bodhi-slam/errors/api.rb +10 -0
- data/lib/bodhi-slam/errors/context.rb +4 -0
- data/lib/bodhi-slam/errors.rb +27 -19
- data/lib/bodhi-slam/factory.rb +34 -6
- data/lib/bodhi-slam/resource.rb +80 -42
- data/lib/bodhi-slam/types.rb +16 -0
- data/lib/bodhi-slam/validations.rb +9 -10
- data/lib/bodhi-slam/validators/type.rb +2 -0
- data/lib/bodhi-slam/validators.rb +20 -15
- data/lib/bodhi-slam.rb +17 -5
- 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: 4c3c82642a103662a2881ac8e07c236853d2e812
|
4
|
+
data.tar.gz: ac2ebf8f9226eb5395ace0a7a8d4da99a3159b24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcccae214b0272c9fed879dc74e49980521db7f8bc27fb5cda37350b63c795f40c852902b59c95e64788ab8edf8d8d97c8f7c1e95eb144d88857e75c83a0c472
|
7
|
+
data.tar.gz: 799f5e418a7ce569b70543002dd9dcbccc897c6658f088f2fc2622e530e75b2d708b6262445747fe37c1ba0d5393f8f04ad96ff1675b4f0acd40c12f4a1fe5bd
|
@@ -23,7 +23,11 @@ module Bodhi
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
|
+
# Gets all Bodhi::Enumerations from a given +context+
|
28
|
+
# and adds them to the Bodhi::Enumeration cache
|
29
|
+
#
|
30
|
+
# Bodhi::Enumeration.find_all(context) # => [#<Bodhi::Enumeration:0x007fbff403e808>, #<Bodhi::Enumeration:0x007fbff403e808>, ...]
|
27
31
|
def self.find_all(context)
|
28
32
|
raise context.errors unless context.valid?
|
29
33
|
|
@@ -42,6 +46,9 @@ module Bodhi
|
|
42
46
|
JSON.parse(result.body).collect{ |enum| Bodhi::Enumeration.new(enum) }
|
43
47
|
end
|
44
48
|
|
49
|
+
# Returns a Hash of all Bodhi::Enumerations in the cache
|
50
|
+
#
|
51
|
+
# Bodhi::Enumerations.cache # => [#<Bodhi::Enumeration:0x007fbff403e808>, #<Bodhi::Enumeration:0x007fbff403e808>, ...]
|
45
52
|
def self.cache
|
46
53
|
@cache ||= Hash.new
|
47
54
|
end
|
data/lib/bodhi-slam/errors.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Bodhi
|
2
|
-
class Errors <
|
2
|
+
class Errors < StandardError
|
3
3
|
include Enumerable
|
4
4
|
attr_accessor :messages
|
5
5
|
|
@@ -9,22 +9,27 @@ module Bodhi
|
|
9
9
|
|
10
10
|
# Adds the given +message+ to the errors hash under the +name+ key
|
11
11
|
#
|
12
|
-
#
|
13
|
-
#
|
12
|
+
# user.errors.add(:test, "has bad value")
|
13
|
+
# user.errors.any? # => true
|
14
14
|
def add(name, message)
|
15
15
|
@messages.has_key?(name) ? @messages[name].push(message) : @messages[name] = [message]
|
16
16
|
end
|
17
17
|
|
18
|
-
# Clears all error messages
|
18
|
+
# Clears all current error messages
|
19
19
|
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
20
|
+
# user.errors.add(:name, "is wrong")
|
21
|
+
# user.errors.any? # => true
|
22
|
+
# user.errors.clear # => nil
|
23
|
+
# user.errors.any? # => false
|
23
24
|
def clear
|
24
25
|
@messages.clear
|
25
26
|
end
|
26
27
|
|
27
28
|
# Returns an array of all error messages
|
29
|
+
#
|
30
|
+
# user.errors.add(:name, "is wrong")
|
31
|
+
# user.errors.add(:address, "is not valid")
|
32
|
+
# user.errors.full_messages # => ["name is wrong", "address is not valid"]
|
28
33
|
def full_messages
|
29
34
|
results = []
|
30
35
|
@messages.each{ |key, values| values.each{ |value| results.push("#{key} #{value}") }}
|
@@ -41,16 +46,16 @@ module Bodhi
|
|
41
46
|
# Yields the attribute and the error for that attribute. If the attribute
|
42
47
|
# has more than one error message, yields once for each error message.
|
43
48
|
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
49
|
+
# user.errors.add(:test, "is required")
|
50
|
+
# user.errors.each do |attribute, error|
|
51
|
+
# # yields :test and "is required"
|
52
|
+
# end
|
48
53
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
+
# user.errors.add(:foo, "is awesome!")
|
55
|
+
# user.errors.each do |attribute, error|
|
56
|
+
# # yields :test and "is required"
|
57
|
+
# # then yields :foo and "is awesome!"
|
58
|
+
# end
|
54
59
|
def each
|
55
60
|
@messages.each_key do |attribute|
|
56
61
|
@messages[attribute].each{ |error| yield attribute, error }
|
@@ -82,6 +87,7 @@ module Bodhi
|
|
82
87
|
#
|
83
88
|
# user.errors.add(:name, "is required")
|
84
89
|
# user.errors.size # => 1
|
90
|
+
#
|
85
91
|
# user.errors.add(:name, "can not be blank")
|
86
92
|
# user.errors.size # => 2
|
87
93
|
def size
|
@@ -91,11 +97,13 @@ module Bodhi
|
|
91
97
|
|
92
98
|
# Returns +true+ if no errors are present, +false+ otherwise.
|
93
99
|
#
|
94
|
-
#
|
95
|
-
#
|
100
|
+
# user.errors.add(:name, "test error")
|
101
|
+
# user.errors.empty? # => false
|
96
102
|
def empty?
|
97
103
|
size == 0
|
98
104
|
end
|
99
105
|
alias :blank? :empty?
|
100
106
|
end
|
101
|
-
end
|
107
|
+
end
|
108
|
+
|
109
|
+
Dir[File.dirname(__FILE__) + "/errors/*.rb"].each { |file| require file }
|
data/lib/bodhi-slam/factory.rb
CHANGED
@@ -8,6 +8,11 @@ module Bodhi
|
|
8
8
|
@generators = Hash.new
|
9
9
|
end
|
10
10
|
|
11
|
+
# Returns a new randomly generated resource.
|
12
|
+
# Accepts an options hash to override specified values.
|
13
|
+
#
|
14
|
+
# Resource.factory.build # => #<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">
|
15
|
+
# Resource.factory.build(name: "test") # => #<Resource:0x007fbff403e808 @name="test">
|
11
16
|
def build(*args)
|
12
17
|
if args.last.is_a?(Hash)
|
13
18
|
params = args.last.reduce({}) do |memo, (k, v)|
|
@@ -28,13 +33,23 @@ module Bodhi
|
|
28
33
|
object
|
29
34
|
end
|
30
35
|
|
36
|
+
# Returns an array of randomly generated resources
|
37
|
+
#
|
38
|
+
# Resource.factory.build_list(10) # => [#<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">, #<Resource:0x007fbff403e808 @name="p7:n#$903<u1">, ...]
|
39
|
+
# Resource.factory.build_list(10, name: "test") # => [#<Resource:0x007fbff403e808 @name="test">, #<Resource:0x007fbff403e808 @name="test">, ...]
|
31
40
|
def build_list(size, *args)
|
32
41
|
size.times.collect{ build(*args) }
|
33
42
|
end
|
34
43
|
|
44
|
+
# Builds and saves a new resource to the given +context+
|
45
|
+
# Accepts an options hash to override specified values.
|
46
|
+
#
|
47
|
+
# context = Bodhi::Context.new
|
48
|
+
# Resource.factory.create(context) # => #<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">
|
49
|
+
# Resource.factory.create(context, name: "test") # => #<Resource:0x007fbff403e808 @name="test">
|
35
50
|
def create(context, params={})
|
36
51
|
if context.invalid?
|
37
|
-
raise context.errors
|
52
|
+
raise context.errors, context.errors.to_a.to_s
|
38
53
|
end
|
39
54
|
|
40
55
|
object = build(params)
|
@@ -43,9 +58,14 @@ module Bodhi
|
|
43
58
|
object
|
44
59
|
end
|
45
60
|
|
61
|
+
# Builds and saves a list of resources to the given +context+
|
62
|
+
# Accepts an options hash to override specified values.
|
63
|
+
#
|
64
|
+
# Resource.factory.create_list(10, context) # => [#<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">, #<Resource:0x007fbff403e808 @name="p7:n#$903<u1">, ...]
|
65
|
+
# Resource.factory.create_list(10, context, name: "test") # => [#<Resource:0x007fbff403e808 @name="test">, #<Resource:0x007fbff403e808 @name="test">, ...]
|
46
66
|
def create_list(size, context, params={})
|
47
67
|
if context.invalid?
|
48
|
-
raise context.errors
|
68
|
+
raise context.errors, context.errors.to_a.to_s
|
49
69
|
end
|
50
70
|
|
51
71
|
resources = build_list(size, params)
|
@@ -59,10 +79,7 @@ module Bodhi
|
|
59
79
|
#puts "\033[33mResult Body\033[0m: \033[36m#{result.body}\033[0m"
|
60
80
|
|
61
81
|
if result.status != 200
|
62
|
-
|
63
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
64
|
-
errors["status"] = result.status if errors.is_a? Hash
|
65
|
-
raise errors.to_s
|
82
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
66
83
|
end
|
67
84
|
|
68
85
|
#puts "\033[33mRecords\033[0m: \033[36m#{records.map(&:attributes)}\033[0m"
|
@@ -70,6 +87,10 @@ module Bodhi
|
|
70
87
|
resources
|
71
88
|
end
|
72
89
|
|
90
|
+
# Adds a new generator to the class with the specified +name+ and +options+
|
91
|
+
#
|
92
|
+
# Resource.factory.add_generator("name", type: "String")
|
93
|
+
# Resource.factory.add_generator("test", type: "Integer", multi: true, required: true)
|
73
94
|
def add_generator(name, options)
|
74
95
|
case options[:type]
|
75
96
|
when "String"
|
@@ -132,6 +153,13 @@ module Bodhi
|
|
132
153
|
generator = lambda { {SecureRandom.hex => SecureRandom.hex} }
|
133
154
|
end
|
134
155
|
|
156
|
+
when "Link"
|
157
|
+
if options[:multi]
|
158
|
+
generator = lambda { [*0..5].sample.times.collect{ Hash.new } }
|
159
|
+
else
|
160
|
+
generator = lambda { Hash.new }
|
161
|
+
end
|
162
|
+
|
135
163
|
when "Enumerated"
|
136
164
|
generator = lambda do
|
137
165
|
ref = options[:ref].split('.')
|
data/lib/bodhi-slam/resource.rb
CHANGED
@@ -7,8 +7,40 @@ module Bodhi
|
|
7
7
|
module ClassMethods
|
8
8
|
def factory; @factory; end
|
9
9
|
|
10
|
+
# Saves a batch of resources to the Bodhi Cloud in the given +context+
|
11
|
+
# Returns an array of JSON objects describing the results for each record in the batch
|
12
|
+
#
|
13
|
+
# context = Bodhi::Context.new
|
14
|
+
# list = Resource.factory.build_list(10)
|
15
|
+
# Resource.save_batch(context, list)
|
16
|
+
def save_batch(context, objects)
|
17
|
+
if context.invalid?
|
18
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
result = context.connection.post do |request|
|
22
|
+
request.url "/#{context.namespace}/resources/#{name}"
|
23
|
+
request.headers['Content-Type'] = 'application/json'
|
24
|
+
request.headers[context.credentials_header] = context.credentials
|
25
|
+
request.body = objects.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
if result.status != 200
|
29
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
30
|
+
end
|
31
|
+
|
32
|
+
objects
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns a single resource from the Bodhi Cloud that matches the given +id+
|
36
|
+
#
|
37
|
+
# context = Bodhi::Context.new
|
38
|
+
# id = Resource.factory.create(context).sys_id
|
39
|
+
# obj = Resource.find(context, id)
|
10
40
|
def find(context, id)
|
11
|
-
|
41
|
+
if context.invalid?
|
42
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
43
|
+
end
|
12
44
|
|
13
45
|
unless id.is_a? String
|
14
46
|
raise ArgumentError.new("Expected 'id' to be a String. 'id' #=> #{id.class}")
|
@@ -20,18 +52,22 @@ module Bodhi
|
|
20
52
|
end
|
21
53
|
|
22
54
|
if result.status != 200
|
23
|
-
|
24
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
25
|
-
errors["status"] = result.status if errors.is_a? Hash
|
26
|
-
raise errors.to_s
|
55
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
27
56
|
end
|
28
57
|
|
29
58
|
resource_attributes = JSON.parse(result.body)
|
30
59
|
factory.build(context, resource_attributes)
|
31
60
|
end
|
32
61
|
|
62
|
+
# Returns all records of the given resource from the Bodhi Cloud.
|
63
|
+
#
|
64
|
+
# context = Bodhi::Context.new
|
65
|
+
# Resource.find_all(context) # => [#<Resource:0x007fbff403e808>, #<Resource:0x007fbff403e808>, ...]
|
33
66
|
def find_all(context)
|
34
|
-
|
67
|
+
if context.invalid?
|
68
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
69
|
+
end
|
70
|
+
|
35
71
|
page = 1
|
36
72
|
records = []
|
37
73
|
|
@@ -42,10 +78,7 @@ module Bodhi
|
|
42
78
|
end
|
43
79
|
|
44
80
|
if result.status != 200
|
45
|
-
|
46
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
47
|
-
errors["status"] = result.status if errors.is_a? Hash
|
48
|
-
raise errors.to_s
|
81
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
49
82
|
end
|
50
83
|
|
51
84
|
page += 1
|
@@ -55,8 +88,14 @@ module Bodhi
|
|
55
88
|
records.flatten.collect{ |record| factory.build(record) }
|
56
89
|
end
|
57
90
|
|
91
|
+
# Aggregates the given resource based on the supplied +pipeline+
|
92
|
+
#
|
93
|
+
# context = Bodhi::Context.new
|
94
|
+
# Resource.aggregate(context, "[{ $match: { property: { $gte: 20 }}}]")
|
58
95
|
def aggregate(context, pipeline)
|
59
|
-
|
96
|
+
if context.invalid?
|
97
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
98
|
+
end
|
60
99
|
|
61
100
|
unless pipeline.is_a? String
|
62
101
|
raise ArgumentError.new("Expected 'pipeline' to be a String. 'pipeline' #=> #{pipeline.class}")
|
@@ -68,17 +107,20 @@ module Bodhi
|
|
68
107
|
end
|
69
108
|
|
70
109
|
if result.status != 200
|
71
|
-
|
72
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
73
|
-
errors["status"] = result.status if errors.is_a? Hash
|
74
|
-
raise errors.to_s
|
110
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
75
111
|
end
|
76
112
|
|
77
113
|
JSON.parse(result.body)
|
78
114
|
end
|
79
115
|
|
116
|
+
# Returns all records for a resource which match the given +query+
|
117
|
+
#
|
118
|
+
# context = Bodhi::Context.new
|
119
|
+
# Resource.where(context, "{property: 'value'}")
|
80
120
|
def where(context, query)
|
81
|
-
|
121
|
+
if context.invalid?
|
122
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
123
|
+
end
|
82
124
|
|
83
125
|
unless query.is_a? String
|
84
126
|
raise ArgumentError.new("Expected 'query' to be a String. 'query' #=> #{query.class}")
|
@@ -90,18 +132,21 @@ module Bodhi
|
|
90
132
|
end
|
91
133
|
|
92
134
|
if result.status != 200
|
93
|
-
|
94
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
95
|
-
errors["status"] = result.status if errors.is_a? Hash
|
96
|
-
raise errors.to_s
|
135
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
97
136
|
end
|
98
137
|
|
99
138
|
resources = JSON.parse(result.body)
|
100
139
|
resources.map{ |attributes| factory.build(context, attributes) }
|
101
140
|
end
|
102
141
|
|
142
|
+
# Deletes all records from a resource in the given +context+
|
143
|
+
#
|
144
|
+
# context = Bodhi::Context.new
|
145
|
+
# Resource.delete_all(context)
|
103
146
|
def delete_all(context)
|
104
|
-
|
147
|
+
if context.invalid?
|
148
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
149
|
+
end
|
105
150
|
|
106
151
|
result = context.connection.delete do |request|
|
107
152
|
request.url "/#{context.namespace}/resources/#{name}"
|
@@ -109,10 +154,7 @@ module Bodhi
|
|
109
154
|
end
|
110
155
|
|
111
156
|
if result.status != 204
|
112
|
-
|
113
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
114
|
-
errors["status"] = result.status if errors.is_a? Hash
|
115
|
-
raise errors.to_s
|
157
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
116
158
|
end
|
117
159
|
end
|
118
160
|
end
|
@@ -120,8 +162,8 @@ module Bodhi
|
|
120
162
|
module InstanceMethods
|
121
163
|
# Returns a Hash of the Objects form attributes
|
122
164
|
#
|
123
|
-
#
|
124
|
-
#
|
165
|
+
# s = SomeResource.build({foo:"test", bar:12345})
|
166
|
+
# s.attributes # => { foo: "test", bar: 12345 }
|
125
167
|
def attributes
|
126
168
|
attributes = Hash.new
|
127
169
|
self.instance_variables.each do |variable|
|
@@ -132,15 +174,20 @@ module Bodhi
|
|
132
174
|
end
|
133
175
|
|
134
176
|
# Returns all the Objects attributes as JSON.
|
135
|
-
#
|
177
|
+
# It converts any nested Objects to JSON if they respond to +to_json+
|
136
178
|
#
|
137
|
-
#
|
138
|
-
#
|
179
|
+
# s = SomeResource.build({foo:"test", bar:12345})
|
180
|
+
# s.to_json # => { "foo":"test", "bar":12345 }
|
139
181
|
def to_json(base=nil)
|
140
182
|
super if base
|
141
183
|
attributes.to_json
|
142
184
|
end
|
143
185
|
|
186
|
+
# Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
|
187
|
+
#
|
188
|
+
# obj = Resouce.new
|
189
|
+
# obj.save!
|
190
|
+
# obj.persisted? # => true
|
144
191
|
def save!
|
145
192
|
result = bodhi_context.connection.post do |request|
|
146
193
|
request.url "/#{bodhi_context.namespace}/resources/#{self.class}"
|
@@ -150,10 +197,7 @@ module Bodhi
|
|
150
197
|
end
|
151
198
|
|
152
199
|
if result.status != 201
|
153
|
-
|
154
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
155
|
-
errors["status"] = result.status if errors.is_a? Hash
|
156
|
-
raise errors.to_s
|
200
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
157
201
|
end
|
158
202
|
|
159
203
|
if result.headers['location']
|
@@ -168,10 +212,7 @@ module Bodhi
|
|
168
212
|
end
|
169
213
|
|
170
214
|
if result.status != 204
|
171
|
-
|
172
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
173
|
-
errors["status"] = result.status if errors.is_a? Hash
|
174
|
-
raise errors.to_s
|
215
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
175
216
|
end
|
176
217
|
end
|
177
218
|
|
@@ -184,10 +225,7 @@ module Bodhi
|
|
184
225
|
end
|
185
226
|
|
186
227
|
if result.status != 204
|
187
|
-
|
188
|
-
errors.each{|error| error['status'] = result.status } if errors.is_a? Array
|
189
|
-
errors["status"] = result.status if errors.is_a? Hash
|
190
|
-
raise errors.to_s
|
228
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
191
229
|
end
|
192
230
|
end
|
193
231
|
end
|
data/lib/bodhi-slam/types.rb
CHANGED
@@ -52,6 +52,12 @@ module Bodhi
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
# Queries the Bodhi API for all types within the given +context+
|
56
|
+
# Returns an array of Bodhi::Type objects
|
57
|
+
#
|
58
|
+
# context = BodhiContext.new(valid_params)
|
59
|
+
# types = Bodhi::Type.find_all(context)
|
60
|
+
# types # => [#<Bodhi::Type:0x007fbff403e808 @name="MyType">, #<Bodhi::Type:0x007fbff403e808 @name="MyType2">, ...]
|
55
61
|
def self.find_all(context)
|
56
62
|
raise context.errors unless context.valid?
|
57
63
|
page = 1
|
@@ -78,6 +84,16 @@ module Bodhi
|
|
78
84
|
all_records.flatten.collect{ |type| Bodhi::Type.new(type) }
|
79
85
|
end
|
80
86
|
|
87
|
+
# Dynamically defines a new Ruby class for the given +type+
|
88
|
+
# Class validations, factory, and helper methods will also be added
|
89
|
+
#
|
90
|
+
# type = Bodhi::Type.new({name: "TestType", properties: { foo:{ type:"String" }}})
|
91
|
+
# klass = Bodhi::Type.create_class_with(type)
|
92
|
+
# klass # => #<Class:0x007fbff403e808 @name="TestType">
|
93
|
+
#
|
94
|
+
# # Additional class methods
|
95
|
+
# klass.validations # => { foo: [#<TypeValidator:0x007fbff403e808 @type="String">] }
|
96
|
+
# klass.factory # => #<Bodhi::Factory:0x007fbff403e808 @klass="TestType", @generators=[]>
|
81
97
|
def self.create_class_with(type)
|
82
98
|
unless type.is_a? Bodhi::Type
|
83
99
|
raise ArgumentError.new("Expected #{type.class} to be a Bodhi::Type")
|
@@ -12,16 +12,15 @@ module Bodhi
|
|
12
12
|
# validates :tags, requried: true, multi: true
|
13
13
|
# validates :name, required: true
|
14
14
|
#
|
15
|
-
# User.validations
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# # }
|
15
|
+
# User.validations # => {
|
16
|
+
# tags: [
|
17
|
+
# #<RequiredValidator:0x007fbff403e808 @options={}>,
|
18
|
+
# #<MultiValidator:0x007fbff403e808 @options={}>
|
19
|
+
# ],
|
20
|
+
# name: [
|
21
|
+
# #<RequiredValidator:0x007fbff403e808 @options={}>
|
22
|
+
# ]
|
23
|
+
# }
|
25
24
|
def validators; @validators; end
|
26
25
|
|
27
26
|
# Creates a new validation on the given +attribute+ using the supplied +options+
|
@@ -7,12 +7,14 @@ module Bodhi
|
|
7
7
|
raise NotImplementedError, "Subclasses must implement a validate(record, attribute, value) method."
|
8
8
|
end
|
9
9
|
|
10
|
-
# Calls +underscore+ on the
|
11
|
-
# Namespaces and the trailing "
|
10
|
+
# Calls +underscore+ on the validator and returns it's class name as a symbol.
|
11
|
+
# Namespaces and the trailing "_validator" text will be trimmed
|
12
12
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
13
|
+
# type = Bodhi::TypeValidator.new("String")
|
14
|
+
# is_not_blank = Bodhi::IsNotBlankValidator.new(true)
|
15
|
+
#
|
16
|
+
# type.to_sym # => :type
|
17
|
+
# is_not_blank.to_sym # => :is_not_blank
|
16
18
|
def to_sym
|
17
19
|
underscore.
|
18
20
|
gsub("bodhi/", "").
|
@@ -22,9 +24,11 @@ module Bodhi
|
|
22
24
|
|
23
25
|
# Returns the validation's class name in snake_case.
|
24
26
|
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
27
|
+
# type = Bodhi::TypeValidator.new("String")
|
28
|
+
# is_not_blank = Bodhi::IsNotBlankValidator.new(true)
|
29
|
+
#
|
30
|
+
# type.underscore # => "bodhi/type_validator"
|
31
|
+
# is_not_blank.underscore # => "bodhi/is_not_blank_validator"
|
28
32
|
def underscore
|
29
33
|
self.class.name.gsub(/::/, '/').
|
30
34
|
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
@@ -36,9 +40,11 @@ module Bodhi
|
|
36
40
|
# Returns the validation as an options Hash.
|
37
41
|
# The options hash is suitable to be used in the Bodhi::Validations.valdiates method
|
38
42
|
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
43
|
+
# type = Bodhi::TypeValidator.new("String")
|
44
|
+
# is_not_blank = Bodhi::IsNotBlankValidator.new(true)
|
45
|
+
#
|
46
|
+
# type_validation.to_options # => { type: "String" }
|
47
|
+
# is_not_blank_validation.to_options # => { is_not_blank: true }
|
42
48
|
def to_options
|
43
49
|
raise NotImplementedError, "Subclasses must implement a to_options method."
|
44
50
|
end
|
@@ -46,12 +52,11 @@ module Bodhi
|
|
46
52
|
# Returns the validator class with the given +name+
|
47
53
|
# Raises NameError if no validator class is found
|
48
54
|
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# Bodhi::Validator.constantize("required") # => Bodhi::RequriedValidator
|
55
|
+
# Bodhi::Validator.constantize("type") # => #<Bodhi::TypeValidator:0x007fbff403e808>
|
56
|
+
# Bodhi::Validator.constantize("is_not_blank") # => #<Bodhi::IsNotBlankValidator:0x007fbff403e808>
|
52
57
|
def self.constantize(name)
|
53
58
|
camelized_name = name.to_s.split('_').collect(&:capitalize).join
|
54
|
-
full_name = "Bodhi::#{camelized_name}Validator"
|
59
|
+
full_name = "Bodhi::#{camelized_name}Validator"
|
55
60
|
Object.const_get(full_name)
|
56
61
|
end
|
57
62
|
end
|
data/lib/bodhi-slam.rb
CHANGED
@@ -13,17 +13,29 @@ require 'bodhi-slam/enumerations'
|
|
13
13
|
require 'bodhi-slam/factory'
|
14
14
|
|
15
15
|
class BodhiSlam
|
16
|
+
# Defines a context to interact with the Bodhi API
|
17
|
+
# Including a +server+, +namespace+, +username+, +password+ or +cookie+
|
18
|
+
#
|
19
|
+
# context = Bodhi::Context.new(server: "https://test.com", namespace: "MyNamespace", username: "MyUser", password: "MyPassword")
|
20
|
+
# context = Bodhi::Context.new(server: "https://test.com", namespace: "MyNamespace", username: "MyUser", cookie: "MyAuthCookie")
|
16
21
|
def self.context(params, &block)
|
17
22
|
bodhi_context = Bodhi::Context.new params
|
18
|
-
raise bodhi_context.errors unless bodhi_context.valid?
|
19
23
|
|
20
|
-
|
24
|
+
if bodhi_context.invalid?
|
25
|
+
raise Bodhi::ContextErrors.new(bodhi_context.errors.messages), bodhi_context.errors.to_a.to_s
|
26
|
+
end
|
27
|
+
|
21
28
|
yield bodhi_context
|
22
|
-
#puts "Exiting context: #{bodhi_context.attributes}"
|
23
29
|
end
|
24
|
-
|
30
|
+
|
31
|
+
# Dynamically creates Ruby Classes for each type in the given +context+
|
32
|
+
#
|
33
|
+
# context = Bodhi::Context.new(valid_params)
|
34
|
+
# BodhiSlam.analyze(context) # => [#<Class:0x007fbff403e808 @name="TestType">, #<Class:0x007fbff403e808 @name="TestType2">, ...]
|
25
35
|
def self.analyze(context)
|
26
|
-
|
36
|
+
if context.invalid?
|
37
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
38
|
+
end
|
27
39
|
|
28
40
|
all_enums = Bodhi::Enumeration.find_all(context)
|
29
41
|
all_types = Bodhi::Type.find_all(context)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bodhi-slam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
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-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http-persistent
|
@@ -104,6 +104,8 @@ files:
|
|
104
104
|
- lib/bodhi-slam/context.rb
|
105
105
|
- lib/bodhi-slam/enumerations.rb
|
106
106
|
- lib/bodhi-slam/errors.rb
|
107
|
+
- lib/bodhi-slam/errors/api.rb
|
108
|
+
- lib/bodhi-slam/errors/context.rb
|
107
109
|
- lib/bodhi-slam/factory.rb
|
108
110
|
- lib/bodhi-slam/resource.rb
|
109
111
|
- lib/bodhi-slam/types.rb
|
@@ -140,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
142
|
version: '0'
|
141
143
|
requirements: []
|
142
144
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.
|
145
|
+
rubygems_version: 2.2.2
|
144
146
|
signing_key:
|
145
147
|
specification_version: 4
|
146
|
-
summary: Ruby bindings for the Bodhi API
|
148
|
+
summary: Ruby bindings for the Bodhi API & factories for random data generation
|
147
149
|
test_files: []
|