bodhi-slam 0.5.0 → 0.5.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
  SHA1:
3
- metadata.gz: 98cf6623880d11b702a150492d82187dd4cba869
4
- data.tar.gz: f9f949910898bac2ac07517e412aa5825ba29d72
3
+ metadata.gz: 5a8e7d831dd4207afd22cb638010b5e61de531bc
4
+ data.tar.gz: 1edfd75cc8d195c1f5ac87b0de92d76ec8b33988
5
5
  SHA512:
6
- metadata.gz: badc323a3402c4ae09b67aeb19e431857204f28b940ee24beacf58fd8284627f606ef3cd73f6d9e6d9f3f32f6a5df2a1264f72c65ed6742b3112539a67b02bee
7
- data.tar.gz: 4b911fde1096640d8620f1ace4237b08c921db7284f3d2166f021efebe41e580fe53136a7f9aaf96066330b9128dbe19df61e1fb1e416aa8848c13ef4d555ddd
6
+ metadata.gz: f1a1ec09ae102cd96f3902b07219ac349902f98cb04f672483a9ff20f079bde115a7b1412071e29d2e3d5744ac26ba6ceaa1ef71c37ed296933c693c0f816bb6
7
+ data.tar.gz: de78dfa1895ef645561bd36e9d010f699fe4b5b7be307917c105b47ac4219fb93d4049f55299bd21685ed4531ac5c3ea5866c43b65f9758297c527d1ccb225f3
@@ -27,19 +27,17 @@ module Bodhi
27
27
  #
28
28
  # Resource.factory.build # => #<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">
29
29
  # Resource.factory.build(name: "test") # => #<Resource:0x007fbff403e808 @name="test">
30
- def build(*args)
31
- if args.last.is_a?(Hash)
32
- params = args.last.reduce({}) do |memo, (k, v)|
33
- memo.merge({ k.to_sym => v})
34
- end
35
- else
36
- params = Hash.new
30
+ def build(options={})
31
+ # symbolize the option keys
32
+ options = options.reduce({}) do |memo, (k, v)|
33
+ memo.merge({ k.to_sym => v})
37
34
  end
38
35
 
39
36
  object = klass.new
37
+ object.bodhi_context = options[:bodhi_context]
40
38
  @generators.each_pair do |attribute, generator|
41
- if params.has_key?(attribute)
42
- object.send("#{attribute}=", params[attribute])
39
+ if options.has_key?(attribute)
40
+ object.send("#{attribute}=", options[attribute])
43
41
  else
44
42
  object.send("#{attribute}=", generator.call)
45
43
  end
@@ -51,8 +49,8 @@ module Bodhi
51
49
  #
52
50
  # Resource.factory.build_list(10) # => [#<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">, #<Resource:0x007fbff403e808 @name="p7:n#$903<u1">, ...]
53
51
  # Resource.factory.build_list(10, name: "test") # => [#<Resource:0x007fbff403e808 @name="test">, #<Resource:0x007fbff403e808 @name="test">, ...]
54
- def build_list(size, *args)
55
- size.times.collect{ build(*args) }
52
+ def build_list(size, options={})
53
+ size.times.collect{ build(options) }
56
54
  end
57
55
 
58
56
  # Builds and saves a new resource to the given +context+
@@ -61,13 +59,21 @@ module Bodhi
61
59
  # context = Bodhi::Context.new
62
60
  # Resource.factory.create(context) # => #<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">
63
61
  # Resource.factory.create(context, name: "test") # => #<Resource:0x007fbff403e808 @name="test">
64
- def create(context, params={})
65
- if context.invalid?
66
- raise context.errors, context.errors.to_a.to_s
62
+ def create(options={})
63
+ # symbolize the option keys
64
+ options = options.reduce({}) do |memo, (k, v)|
65
+ memo.merge({ k.to_sym => v})
66
+ end
67
+
68
+ if options[:bodhi_context].nil?
69
+ raise ArgumentError.new("Missing option :bodhi_context")
67
70
  end
68
71
 
69
- object = build(params)
70
- object.bodhi_context = context
72
+ if options[:bodhi_context].invalid?
73
+ raise options[:bodhi_context].errors, options[:bodhi_context].errors.to_a.to_s
74
+ end
75
+
76
+ object = build(options)
71
77
  object.save!
72
78
  object
73
79
  end
@@ -77,17 +83,22 @@ module Bodhi
77
83
  #
78
84
  # Resource.factory.create_list(10, context) # => [#<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">, #<Resource:0x007fbff403e808 @name="p7:n#$903<u1">, ...]
79
85
  # Resource.factory.create_list(10, context, name: "test") # => [#<Resource:0x007fbff403e808 @name="test">, #<Resource:0x007fbff403e808 @name="test">, ...]
80
- def create_list(size, context, params={})
81
- if context.invalid?
82
- raise context.errors, context.errors.to_a.to_s
86
+ def create_list(size, options={})
87
+ # symbolize the option keys
88
+ options = options.reduce({}) do |memo, (k, v)|
89
+ memo.merge({ k.to_sym => v})
90
+ end
91
+
92
+ if options[:bodhi_context].nil?
93
+ raise ArgumentError.new("Missing option :bodhi_context")
83
94
  end
84
95
 
85
- resources = build_list(size, params)
86
- resources.each do |resource|
87
- resource.bodhi_context = context
88
- resource.save!
96
+ if options[:bodhi_context].invalid?
97
+ raise options[:bodhi_context].errors, options[:bodhi_context].errors.to_a.to_s
89
98
  end
90
99
 
100
+ resources = build_list(size, options)
101
+ resources.each{ |resource| resource.save! }
91
102
  resources
92
103
  end
93
104
 
@@ -315,7 +315,7 @@ module Bodhi
315
315
  request.url "/#{bodhi_context.namespace}/resources/#{self.class}/#{sys_id}"
316
316
  request.headers['Content-Type'] = 'application/json'
317
317
  request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
318
- request.body = params.to_json
318
+ request.body = attributes.to_json
319
319
  end
320
320
 
321
321
  if result.status != 204
@@ -326,6 +326,30 @@ module Bodhi
326
326
  end
327
327
  alias :update :update!
328
328
 
329
+ def upsert!(params={})
330
+ update_attributes(params)
331
+
332
+ if invalid?
333
+ return false
334
+ end
335
+
336
+ result = bodhi_context.connection.put do |request|
337
+ request.url "/#{bodhi_context.namespace}/resources/#{self.class}?upsert=true"
338
+ request.headers['Content-Type'] = 'application/json'
339
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
340
+ request.body = attributes.to_json
341
+ end
342
+
343
+ unless [204, 201].include?(result.status)
344
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
345
+ end
346
+
347
+ if result.headers['location']
348
+ @sys_id = result.headers['location'].match(/(?<id>[a-zA-Z0-9]{24})/)[:id]
349
+ end
350
+ end
351
+ alias :upsert :upsert!
352
+
329
353
  def patch!(params)
330
354
  result = bodhi_context.connection.patch do |request|
331
355
  request.url "/#{bodhi_context.namespace}/resources/#{self.class}/#{sys_id}"
@@ -155,8 +155,37 @@ module Bodhi
155
155
  klass = Object.const_set(type.name, Class.new { include Bodhi::Resource })
156
156
 
157
157
  type.properties.each_pair do |attr_name, attr_properties|
158
- attr_properties.delete_if{ |key, value| ["system", "trim", "unique", "default", "isCurrentUser", "toLower"].include?(key) }
159
- klass.field(attr_name, attr_properties)
158
+ # symbolize the attr_properties keys
159
+ attr_properties = attr_properties.reduce({}) do |memo, (k, v)|
160
+ memo.merge({ k.to_sym => v})
161
+ end
162
+
163
+ # remove Sanitizers
164
+ attr_properties.delete_if{ |key, value| [:trim, :unique, :default, :isCurrentUser, :toLower].include?(key) }
165
+
166
+ # Do not add factories or validations for system properties
167
+ if attr_properties[:system] == true
168
+ klass.property attr_name
169
+ else
170
+ klass.field(attr_name, attr_properties)
171
+ end
172
+ end
173
+
174
+ # add indexes to the class
175
+ unless type.indexes.nil?
176
+ type.indexes.each do |index|
177
+ if index.is_a? Bodhi::TypeIndex
178
+ klass.index index.keys, index.options
179
+ else
180
+ # index is a raw json object
181
+ # symbolize the index option keys
182
+ index = index.reduce({}) do |memo, (k, v)|
183
+ memo.merge({ k.to_sym => v})
184
+ end
185
+
186
+ klass.index index[:keys], index[:options]
187
+ end
188
+ end
160
189
  end
161
190
 
162
191
  klass
@@ -0,0 +1,40 @@
1
+ module Bodhi
2
+ class PrecisionValidator < Validator
3
+ attr_reader :number
4
+
5
+ def initialize(number)
6
+ if number.nil?
7
+ raise ArgumentError.new("Expected :number to not be nil")
8
+ end
9
+ @number = number
10
+ end
11
+
12
+ def validate(record, attribute, value)
13
+ unless value.nil?
14
+
15
+ if value.is_a?(Array)
16
+ unless value.empty?
17
+ record.errors.add(attribute, "must contain only values with #{@number} decimal points") unless value.delete_if{ |v| decimals(v) == @number }.empty?
18
+ end
19
+ else
20
+ record.errors.add(attribute, "must have #{@number} decimal points") if decimals(value) != @number
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ def to_options
27
+ {self.to_sym => @number}
28
+ end
29
+
30
+ private
31
+ def decimals(a)
32
+ num = 0
33
+ while(a != a.to_i)
34
+ num += 1
35
+ a *= 10
36
+ end
37
+ num
38
+ end
39
+ end
40
+ end
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.5.0
4
+ version: 0.5.1
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-16 00:00:00.000000000 Z
11
+ date: 2015-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -184,6 +184,7 @@ files:
184
184
  - lib/bodhi-slam/validators/min.rb
185
185
  - lib/bodhi-slam/validators/multi.rb
186
186
  - lib/bodhi-slam/validators/not_empty.rb
187
+ - lib/bodhi-slam/validators/precision.rb
187
188
  - lib/bodhi-slam/validators/required.rb
188
189
  - lib/bodhi-slam/validators/type.rb
189
190
  - lib/bodhi-slam/validators/url.rb