bodhi-slam 0.3.1 → 0.3.2
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 +1 -0
- data/lib/bodhi-slam/factory.rb +22 -4
- data/lib/bodhi-slam/resource.rb +18 -0
- data/lib/bodhi-slam/types.rb +5 -30
- data/lib/bodhi-slam/validations.rb +6 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9e0fc2114db2a841586eb0937833f53d7696cf4
|
4
|
+
data.tar.gz: c47da0ee237f0a404ec5d3e8b8f12c2c957ba1b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fd1e845bdf7e809e7bf8ba750a60e9d1a2e27924846c576fa838ec1ce300e8f78eba9b1b6d23dd02c00886c982d37456ae9e3bf651428ceb62ff037568b3d87
|
7
|
+
data.tar.gz: 86440102243b951b235e7a4eb9bce696c530d86fe6a8193a9e1fe7470915b9582d30c4df36948da0b666a0e03ae549a6152aedf6d6b2d7ea44bb174de1b9e290
|
data/lib/bodhi-slam.rb
CHANGED
data/lib/bodhi-slam/factory.rb
CHANGED
@@ -82,16 +82,34 @@ module Bodhi
|
|
82
82
|
# Resource.factory.add_generator("name", type: "String")
|
83
83
|
# Resource.factory.add_generator("test", type: "Integer", multi: true, required: true)
|
84
84
|
def add_generator(name, options)
|
85
|
+
options = options.reduce({}) do |memo, (k, v)|
|
86
|
+
memo.merge({ k.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase.to_sym => v})
|
87
|
+
end
|
88
|
+
|
85
89
|
case options[:type]
|
86
90
|
when "String"
|
87
91
|
characters = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
|
88
92
|
"~!@#$%^&*()_+-=:;<>?,./ ".each_char{ |c| characters.push(c) }
|
89
93
|
|
90
|
-
|
91
|
-
if options[:
|
92
|
-
[*0..5].sample.times.collect{ [*
|
94
|
+
if options[:multi]
|
95
|
+
if options[:is_not_blank]
|
96
|
+
generator = lambda{ [*0..5].sample.times.collect{ [*1..100].sample.times.map{ characters[rand(characters.length)] }.join } }
|
97
|
+
elsif options[:is_email]
|
98
|
+
generator = lambda{ [*0..5].sample.times.collect{ /\p{Alnum}{5,10}@\p{Alnum}{5,10}\.\p{Alnum}{2,3}/i.random_example } }
|
99
|
+
elsif options[:matches]
|
100
|
+
generator = lambda{ [*0..5].sample.times.collect{ Regexp.new(options[:matches]).random_example } }
|
101
|
+
else
|
102
|
+
generator = lambda{ [*0..5].sample.times.collect{ [*0..100].sample.times.map{ characters[rand(characters.length)] }.join } }
|
103
|
+
end
|
104
|
+
else
|
105
|
+
if options[:is_not_blank]
|
106
|
+
generator = lambda{ [*1..100].sample.times.map{ characters[rand(characters.length)] }.join }
|
107
|
+
elsif options[:is_email]
|
108
|
+
generator = lambda{ /\p{Alnum}{5,10}@\p{Alnum}{5,10}\.\p{Alnum}{2,3}/i.random_example }
|
109
|
+
elsif options[:matches]
|
110
|
+
generator = lambda{ Regexp.new(options[:matches]).random_example }
|
93
111
|
else
|
94
|
-
[*0..100].sample.times.map{ characters[rand(characters.length)] }.join
|
112
|
+
generator = lambda{ [*0..100].sample.times.map{ characters[rand(characters.length)] }.join }
|
95
113
|
end
|
96
114
|
end
|
97
115
|
|
data/lib/bodhi-slam/resource.rb
CHANGED
@@ -21,6 +21,24 @@ module Bodhi
|
|
21
21
|
batch
|
22
22
|
end
|
23
23
|
|
24
|
+
# Counts all of the Resources records and returns the result
|
25
|
+
def count(context)
|
26
|
+
if context.invalid?
|
27
|
+
raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
result = context.connection.get do |request|
|
31
|
+
request.url "/#{context.namespace}/resources/#{name}/count"
|
32
|
+
request.headers[context.credentials_header] = context.credentials
|
33
|
+
end
|
34
|
+
|
35
|
+
if result.status != 200
|
36
|
+
raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
|
37
|
+
end
|
38
|
+
|
39
|
+
JSON.parse(result.body)
|
40
|
+
end
|
41
|
+
|
24
42
|
# Returns a single resource from the Bodhi Cloud that matches the given +id+
|
25
43
|
#
|
26
44
|
# context = Bodhi::Context.new
|
data/lib/bodhi-slam/types.rb
CHANGED
@@ -8,7 +8,6 @@ module Bodhi
|
|
8
8
|
|
9
9
|
attr_accessor *ATTRIBUTES
|
10
10
|
attr_reader *SYSTEM_ATTRIBUTES
|
11
|
-
attr_reader :validations
|
12
11
|
attr_accessor :bodhi_context
|
13
12
|
|
14
13
|
validates :name, required: true, is_not_blank: true
|
@@ -38,31 +37,10 @@ module Bodhi
|
|
38
37
|
send("#{attribute}=", params[attribute])
|
39
38
|
end
|
40
39
|
|
40
|
+
# Format type name to be compatible with Ruby Constants
|
41
41
|
if !name.nil? && name[0] == name[0].downcase
|
42
42
|
name.capitalize!
|
43
43
|
end
|
44
|
-
|
45
|
-
# build validator objects
|
46
|
-
@validations = {}
|
47
|
-
if properties
|
48
|
-
properties.each_pair do |attr_name, attr_properties|
|
49
|
-
@validations[attr_name.to_sym] = []
|
50
|
-
attr_properties.each_pair do |option, value|
|
51
|
-
underscored_name = option.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase.to_sym
|
52
|
-
unless [:system, :trim, :ref, :unique, :default, :is_current_user, :to_lower].include? underscored_name
|
53
|
-
klass = Bodhi::Validator.constantize(underscored_name)
|
54
|
-
if option.to_s == "type" && value == "Enumerated"
|
55
|
-
if attr_properties["ref"].nil?
|
56
|
-
raise RuntimeError.new("No reference property found! Cannot build enumeration validator for #{name}.#{attr_name}")
|
57
|
-
end
|
58
|
-
@validations[attr_name.to_sym] << klass.new(value, attr_properties["ref"])
|
59
|
-
else
|
60
|
-
@validations[attr_name.to_sym] << klass.new(value)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
44
|
end
|
67
45
|
|
68
46
|
# Returns a Hash of the Objects form attributes
|
@@ -183,8 +161,6 @@ module Bodhi
|
|
183
161
|
# type = Bodhi::Type.new({name: "TestType", properties: { foo:{ type:"String" }}})
|
184
162
|
# klass = Bodhi::Type.create_class_with(type)
|
185
163
|
# klass # => #<Class:0x007fbff403e808 @name="TestType">
|
186
|
-
#
|
187
|
-
# # Additional class methods
|
188
164
|
# klass.validations # => { foo: [#<TypeValidator:0x007fbff403e808 @type="String">] }
|
189
165
|
# klass.factory # => #<Bodhi::Factory:0x007fbff403e808 @klass="TestType", @generators=[]>
|
190
166
|
def self.create_class_with(type)
|
@@ -197,11 +173,10 @@ module Bodhi
|
|
197
173
|
attr_accessor *type.properties.keys
|
198
174
|
})
|
199
175
|
|
200
|
-
type.
|
201
|
-
|
202
|
-
|
203
|
-
klass.
|
204
|
-
klass.factory.add_generator(attribute, attr_options)
|
176
|
+
type.properties.each_pair do |attr_name, attr_properties|
|
177
|
+
attr_properties.delete_if{ |key, value| ["system", "trim", "unique", "default", "isCurrentUser"].include?(key) }
|
178
|
+
klass.validates(attr_name.to_sym, attr_properties)
|
179
|
+
klass.factory.add_generator(attr_name.to_sym, attr_properties)
|
205
180
|
end
|
206
181
|
|
207
182
|
klass
|
@@ -45,8 +45,14 @@ module Bodhi
|
|
45
45
|
raise ArgumentError.new("Invalid :options argument. Options can not be empty")
|
46
46
|
end
|
47
47
|
|
48
|
+
options = options.reduce({}) do |memo, (k, v)|
|
49
|
+
memo.merge({ k.to_sym => v})
|
50
|
+
end
|
51
|
+
|
48
52
|
@validators[attribute] = []
|
49
53
|
options.each_pair do |key, value|
|
54
|
+
key = key.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase.to_sym
|
55
|
+
|
50
56
|
unless [:ref].include?(key)
|
51
57
|
if key == :type && value == "Enumerated"
|
52
58
|
@validators[attribute] << Bodhi::Validator.constantize(key).new(value, options[:ref])
|
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.3.
|
4
|
+
version: 0.3.2
|
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-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http-persistent
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: regexp-examples
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.1'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|