bodhi-slam 0.4.1 → 0.5.0

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: 0787ec3a9705faa25dab09c506a0a220e8d7d9a8
4
- data.tar.gz: 7e5686d4a4417d53c0743596ef2b9e758c878a7b
3
+ metadata.gz: 98cf6623880d11b702a150492d82187dd4cba869
4
+ data.tar.gz: f9f949910898bac2ac07517e412aa5825ba29d72
5
5
  SHA512:
6
- metadata.gz: bea65f3aed37d29efd1ef2fff6ab918341037656b2db0720000fc35d5aaa2645f60acc77b86bdab2c9714b81bb209af68ee7d1678b4d3258e6c8e1b0a9c96d99
7
- data.tar.gz: 623c0e593b4e1b465bd3a4b5ad4890618b7d8e841c031367b18ca3a7efb33901c60bdc6e8bfdbb8ffe5f139a7a0e715e17da48773f6a468ea607c604161440a9
6
+ metadata.gz: badc323a3402c4ae09b67aeb19e431857204f28b940ee24beacf58fd8284627f606ef3cd73f6d9e6d9f3f32f6a5df2a1264f72c65ed6742b3112539a67b02bee
7
+ data.tar.gz: 4b911fde1096640d8620f1ace4237b08c921db7284f3d2166f021efebe41e580fe53136a7f9aaf96066330b9128dbe19df61e1fb1e416aa8848c13ef4d555ddd
@@ -8,10 +8,15 @@ require "SecureRandom"
8
8
  require 'regexp-examples'
9
9
  require 'active_model'
10
10
 
11
+ require 'bodhi-slam/support'
12
+
11
13
  require 'bodhi-slam/validations'
12
14
  require 'bodhi-slam/errors'
13
15
  require 'bodhi-slam/context'
14
16
 
17
+ require 'bodhi-slam/properties'
18
+ require 'bodhi-slam/indexes'
19
+
15
20
  require 'bodhi-slam/batches'
16
21
  require 'bodhi-slam/enumerations'
17
22
  require 'bodhi-slam/factory'
@@ -6,5 +6,10 @@ module Bodhi
6
6
  @body = params[:body]
7
7
  @status = params[:status]
8
8
  end
9
+
10
+ def to_json
11
+ { status: @status, body: @body }.to_json
12
+ end
13
+ alias :to_s :to_json
9
14
  end
10
15
  end
@@ -1,4 +1,18 @@
1
1
  module Bodhi
2
+ module Factories
3
+ module ClassMethods
4
+ def factory; @factory; end
5
+ def generates(name, options)
6
+ @factory.add_generator(name.to_sym, options)
7
+ end
8
+ end
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ base.instance_variable_set(:@factory, Bodhi::Factory.new(base))
13
+ end
14
+ end
15
+
2
16
  class Factory
3
17
  attr_reader :klass
4
18
  attr_accessor :generators
@@ -0,0 +1,20 @@
1
+ module Bodhi
2
+ module Indexes
3
+ module ClassMethods
4
+ def indexes; @indexes; end
5
+ def index(keys, options={})
6
+ # symbolize the option keys
7
+ options = options.reduce({}) do |memo, (k, v)|
8
+ memo.merge({ k.to_sym => v})
9
+ end
10
+
11
+ @indexes << Bodhi::TypeIndex.new(keys: keys.map(&:to_s), options: options)
12
+ end
13
+ end
14
+
15
+ def self.included(base)
16
+ base.extend(ClassMethods)
17
+ base.instance_variable_set(:@indexes, Array.new)
18
+ end
19
+ end
20
+ end
@@ -1,47 +1,44 @@
1
1
  module Bodhi
2
2
  class Profile
3
+ include Bodhi::Factories
4
+ include Bodhi::Properties
3
5
  include Bodhi::Validations
4
6
 
5
- ATTRIBUTES = [:name, :namespace, :dml, :subspace, :parent]
6
- attr_accessor *ATTRIBUTES
7
7
  attr_accessor :bodhi_context
8
8
 
9
+ property :name, :namespace, :dml, :subspace, :parent
10
+
9
11
  validates :name, type: "String", required: true, is_not_blank: true
10
12
  validates :namespace, type: "String", required: true
11
13
  validates :dml, type: "Object", required: true
12
14
 
13
- def initialize(params={})
14
- # same as symbolize_keys!
15
- params = params.reduce({}) do |memo, (k, v)|
16
- memo.merge({ k.to_sym => v})
15
+ generates :name, type: "String", required: true, is_not_blank: true
16
+ generates :namespace, type: "String", required: true
17
+ generates :dml, type: "Object", required: true
18
+
19
+ # Queries the Bodhi API for the given +user_name+ and
20
+ # returns a Bodhi::Profile
21
+ #
22
+ # context = BodhiContext.new(valid_params)
23
+ # profile = Bodhi::Profile.find(context, "Profile1")
24
+ # profile # => #<Bodhi::Profile:0x007fbff403e808 @name="Profile1">
25
+ def self.find(context, profile_name)
26
+ if context.invalid?
27
+ raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
17
28
  end
18
29
 
19
- # set attributes
20
- ATTRIBUTES.each do |attribute|
21
- send("#{attribute}=", params[attribute])
30
+ result = context.connection.get do |request|
31
+ request.url "/#{context.namespace}/profiles/#{profile_name}"
32
+ request.headers[context.credentials_header] = context.credentials
22
33
  end
23
- end
24
34
 
25
- # Returns a Hash of the Objects form attributes
26
- #
27
- # s = SomeResource.factory.build({foo:"test", bar:12345})
28
- # s.attributes # => { foo: "test", bar: 12345 }
29
- def attributes
30
- result = Hash.new
31
- ATTRIBUTES.each do |attribute|
32
- result[attribute] = send(attribute)
35
+ if result.status != 200
36
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
33
37
  end
34
- result
35
- end
36
38
 
37
- # Returns all the Objects attributes as JSON.
38
- # It converts any nested Objects to JSON if they respond to +to_json+
39
- #
40
- # s = SomeResource.factory.build({foo:"test", bar:12345})
41
- # s.to_json # => "{ 'foo':'test', 'bar':12345 }"
42
- def to_json(base=nil)
43
- super if base
44
- attributes.to_json
39
+ profile = Bodhi::Profile.new(result.body)
40
+ profile.bodhi_context = context
41
+ profile
45
42
  end
46
43
 
47
44
  # Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
@@ -73,38 +70,5 @@ module Bodhi
73
70
  end
74
71
  end
75
72
 
76
- # Returns a factory for the Bodhi::User class
77
- def self.factory
78
- @factory ||= Bodhi::Factory.new(Bodhi::Profile).tap do |factory|
79
- factory.add_generator(:name, type: "String", required: true, is_not_blank: true)
80
- factory.add_generator(:namespace, type: "String", required: true)
81
- factory.add_generator(:dml, type: "Object", required: true)
82
- end
83
- end
84
-
85
- # Queries the Bodhi API for the given +user_name+ and
86
- # returns a Bodhi::Profile
87
- #
88
- # context = BodhiContext.new(valid_params)
89
- # profile = Bodhi::Profile.find(context, "Profile1")
90
- # profile # => #<Bodhi::Profile:0x007fbff403e808 @name="Profile1">
91
- def self.find(context, profile_name)
92
- if context.invalid?
93
- raise Bodhi::ContextErrors.new(context.errors.messages), context.errors.to_a.to_s
94
- end
95
-
96
- result = context.connection.get do |request|
97
- request.url "/#{context.namespace}/profiles/#{profile_name}"
98
- request.headers[context.credentials_header] = context.credentials
99
- end
100
-
101
- if result.status != 200
102
- raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
103
- end
104
-
105
- profile = Bodhi::Profile.new(result.body)
106
- profile.bodhi_context = context
107
- profile
108
- end
109
73
  end
110
74
  end
@@ -0,0 +1,84 @@
1
+ module Bodhi
2
+ module Properties
3
+
4
+ SYSTEM_PROPERTIES = [:sys_created_at, :sys_version, :sys_modified_at, :sys_modified_by,
5
+ :sys_namespace, :sys_created_by, :sys_type_version, :sys_id, :sys_embeddedType]
6
+ attr_accessor *SYSTEM_PROPERTIES
7
+
8
+ module ClassMethods
9
+ def properties; @properties; end
10
+ def property(*names)
11
+ attr_accessor *names.map(&:to_sym)
12
+ @properties << names.map(&:to_sym)
13
+ @properties.flatten!
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def id; @sys_id; end
19
+ def persisted?; !@sys_id.nil?; end
20
+ def new_record?; @sys_id.nil?; end
21
+
22
+ # Initializes a new instance of the class. Accepts a parameter Hash for mass assignment.
23
+ # If a parameter does not exist for the class, an UndefinedMethod error is raised.
24
+ #
25
+ # klass = Class.new do
26
+ # include Bodhi::Properties
27
+ # property :name, :email
28
+ # end
29
+ #
30
+ # object = klass.new(name: "Bob", email: "some@email.com")
31
+ # object.name #=> "Bob"
32
+ # object.email #=> "some@email.com"
33
+ def initialize(params={})
34
+ params.each do |param_key, param_value|
35
+ send("#{param_key}=", param_value)
36
+ end
37
+ end
38
+
39
+ # Returns a Hash of the class properties and their values.
40
+ #
41
+ # object = SomeResource.new(foo:"test", bar:12345)
42
+ # object.attributes # => { foo: "test", bar: 12345 }
43
+ def attributes
44
+ attributes = Hash.new
45
+ self.class.properties.each do |property|
46
+ attributes[property] = send(property)
47
+ end
48
+
49
+ attributes.delete_if { |k, v| v.nil? }
50
+ attributes
51
+ end
52
+
53
+ # Updates the resource with the given attributes Hash
54
+ #
55
+ # s = SomeResource.factory.build(foo:"test", bar:12345)
56
+ # s.attributes # => { foo: "test", bar: 12345 }
57
+ # s.update_attributes(bar: 10)
58
+ # s.attributes # => { foo: "test", bar: 10 }
59
+ def update_attributes(params)
60
+ params.each do |param_key, param_value|
61
+ send("#{param_key}=", param_value)
62
+ end
63
+ end
64
+
65
+ # Returns all the classes properties as JSON.
66
+ # It converts any nested objects to JSON if they respond to +to_json+
67
+ #
68
+ # resource = SomeResource.new(foo:"test", bar:12345)
69
+ # embedded_resources = AnotherResource.new( test: resource )
70
+ #
71
+ # resource.to_json # => "{ 'foo':'test', 'bar':12345 }"
72
+ # embedded_resources.to_json # => "{ 'test': { 'foo':'test', 'bar':12345 } }"
73
+ def to_json(base=nil)
74
+ attributes.to_json
75
+ end
76
+ end
77
+
78
+ def self.included(base)
79
+ base.extend(ClassMethods)
80
+ base.include(InstanceMethods)
81
+ base.instance_variable_set(:@properties, Array.new)
82
+ end
83
+ end
84
+ end
@@ -1,14 +1,44 @@
1
1
  module Bodhi
2
2
  module Resource
3
3
 
4
- SYSTEM_ATTRIBUTES = [:sys_created_at, :sys_version, :sys_modified_at, :sys_modified_by,
5
- :sys_namespace, :sys_created_by, :sys_type_version, :sys_id, :sys_embeddedType]
6
- SUPPORT_ATTRIBUTES = [:bodhi_context, :errors]
7
- attr_accessor *SYSTEM_ATTRIBUTES
8
- attr_accessor *SUPPORT_ATTRIBUTES
4
+ attr_accessor :bodhi_context
9
5
 
10
6
  module ClassMethods
11
- def factory; @factory; end
7
+
8
+ def embedded(bool); @embedded = bool; end
9
+ def is_embedded?; @embedded; end
10
+
11
+ # Defines the given +name+ and +options+ as a form attribute for the class.
12
+ # The +name+ is set as a property, and validations & factory generators are added based on the supplied +options+
13
+ #
14
+ # class User
15
+ # include Bodhi::Resource
16
+ #
17
+ # field :first_name, type: "String", required: true, is_not_blank: true
18
+ # field :last_name, type: "String", required: true, is_not_blank: true
19
+ # field :email, type: "String", required: true, is_not_blank: true, is_email: true
20
+ # end
21
+ #
22
+ # object = User.new(first_name: "John", last_name: "Smith", email: "jsmith@email.com")
23
+ # object.to_json #=> { "first_name": "John", "last_name": "Smith", "email": "jsmith@email.com" }
24
+ def field(name, options)
25
+ property(name.to_sym)
26
+ validates(name.to_sym, options)
27
+ generates(name.to_sym, options)
28
+ end
29
+
30
+ def build_type
31
+ # reduce the properties array into a Hash.
32
+ # lookup the validators for each property and add them as options for the property
33
+ type_properties = self.properties.reduce({}) do |result, property|
34
+ options = self.validators[property].map(&:to_options).reduce({}) do |options_result, options_hash|
35
+ options_result.merge(options_hash)
36
+ end
37
+ result.merge({ property => options})
38
+ end
39
+
40
+ Bodhi::Type.new(name: self.name, properties: type_properties, indexes: self.indexes, embedded: self.is_embedded?)
41
+ end
12
42
 
13
43
  # Saves a batch of resources to the Bodhi Cloud in the given +context+
14
44
  # Returns an array of JSON objects describing the results for each record in the batch
@@ -182,9 +212,6 @@ module Bodhi
182
212
  end
183
213
 
184
214
  module InstanceMethods
185
- def id; @sys_id; end
186
- def persisted?; !@sys_id.nil?; end
187
- def new_record?; @sys_id.nil?; end
188
215
 
189
216
  def initialize(params={})
190
217
  params.each do |param_key, param_value|
@@ -207,46 +234,6 @@ module Bodhi
207
234
  end
208
235
  end
209
236
 
210
- # Returns a Hash of the Objects form attributes
211
- #
212
- # s = SomeResource.build({foo:"test", bar:12345})
213
- # s.attributes # => { foo: "test", bar: 12345 }
214
- def attributes
215
- attributes = Hash.new
216
- self.instance_variables.each do |variable|
217
- attribute_name = variable.to_s.delete('@').to_sym
218
- unless SYSTEM_ATTRIBUTES.include?(attribute_name) || SUPPORT_ATTRIBUTES.include?(attribute_name)
219
- attributes[attribute_name] = send(attribute_name)
220
- end
221
- end
222
- attributes
223
- end
224
-
225
- # Updates the resource with the given attributes Hash
226
- #
227
- # s = SomeResource.factory.build(foo:"test", bar:12345)
228
- # s.attributes # => { foo: "test", bar: 12345 }
229
- # s.update_attributes(foo:"12345", bar:10)
230
- # s.attributes # => { foo: "12345", bar: 10 }
231
- def update_attributes(params)
232
- self.instance_variables.each do |variable|
233
- attribute_name = variable.to_s.delete('@').to_sym
234
- unless SYSTEM_ATTRIBUTES.include?(attribute_name) || SUPPORT_ATTRIBUTES.include?(attribute_name)
235
- send("#{attribute_name}=", params[attribute_name])
236
- end
237
- end
238
- end
239
-
240
- # Returns all the Objects attributes as JSON.
241
- # It converts any nested Objects to JSON if they respond to +to_json+
242
- #
243
- # s = SomeResource.build({foo:"test", bar:12345})
244
- # s.to_json # => { "foo":"test", "bar":12345 }
245
- def to_json(base=nil)
246
- super if base
247
- attributes.to_json
248
- end
249
-
250
237
  # Saves the resource to the Bodhi Cloud. Returns true if record was saved
251
238
  #
252
239
  # obj = Resource.new
@@ -355,8 +342,8 @@ module Bodhi
355
342
 
356
343
  def self.included(base)
357
344
  base.extend(ClassMethods)
358
- base.include(InstanceMethods, Bodhi::Validations, ActiveModel::Model)
359
- base.instance_variable_set(:@factory, Bodhi::Factory.new(base))
345
+ base.include(InstanceMethods, Bodhi::Validations, Bodhi::Properties, Bodhi::Indexes, Bodhi::Factories, ActiveModel::Model)
346
+ base.instance_variable_set(:@embedded, false)
360
347
  end
361
348
  end
362
349
  end
@@ -0,0 +1,23 @@
1
+ module Bodhi
2
+ class Support
3
+ def self.underscore(string)
4
+ string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_").
7
+ downcase
8
+ end
9
+
10
+ def self.uncapitalize(string)
11
+ string[0, 1].downcase + string[1..-1]
12
+ end
13
+
14
+ def self.camelize(string)
15
+ underscore(string).split('_').collect(&:capitalize).join
16
+ end
17
+
18
+ def self.reverse_camelize(string)
19
+ result = underscore(string).split('_').collect(&:capitalize).join
20
+ uncapitalize(result)
21
+ end
22
+ end
23
+ end
@@ -1,41 +1,42 @@
1
1
  module Bodhi
2
2
  class Type
3
+ include Bodhi::Factories
4
+ include Bodhi::Properties
3
5
  include Bodhi::Validations
4
6
 
5
- SYSTEM_ATTRIBUTES = [:sys_created_at, :sys_version, :sys_modified_at, :sys_modified_by,
6
- :sys_namespace, :sys_created_by, :sys_type_version, :sys_id, :sys_embeddedType]
7
- ATTRIBUTES = [:name, :namespace, :package, :embedded, :properties, :version, :extends]
8
-
9
- attr_accessor *ATTRIBUTES
10
- attr_reader *SYSTEM_ATTRIBUTES
11
7
  attr_accessor :bodhi_context
12
8
 
9
+ property :name, :storage_name, :namespace,
10
+ :package, :embedded, :properties, :version,
11
+ :extends, :indexes, :hidden, :events, :documentation,
12
+ :metadata, :encapsulated
13
+
13
14
  validates :name, required: true, is_not_blank: true
14
- validates :namespace, required: true
15
15
  validates :properties, required: true
16
+ validates :indexes, type: "Bodhi::TypeIndex", multi: true
16
17
 
17
- # Returns a factory for the Bodhi::Type class
18
- def self.factory
19
- @factory ||= Bodhi::Factory.new(Bodhi::Type).tap do |factory|
20
- factory.add_generator(:extends, type: "String", matches: "[a-zA-Z_-]{10,20}")
21
- factory.add_generator(:name, type: "String", required: true, is_not_blank: true)
22
- factory.add_generator(:namespace, type: "String", required: true)
23
- factory.add_generator(:properties, type: "Object", required: true)
24
- factory.add_generator(:package, type: "String")
25
- factory.add_generator(:embedded, type: "Boolean")
26
- factory.add_generator(:version, type: "String")
27
- end
28
- end
18
+ generates :extends, type: "String", matches: "[a-zA-Z_-]{10,20}"
19
+ generates :name, type: "String", required: true, is_not_blank: true
20
+ generates :namespace, type: "String", required: true
21
+ generates :properties, type: "Object", required: true
22
+ generates :package, type: "String"
23
+ generates :embedded, type: "Boolean"
24
+ generates :version, type: "String"
29
25
 
30
26
  def initialize(params={})
31
- # same as symbolize_keys!
32
- params = params.reduce({}) do |memo, (k, v)|
33
- memo.merge({ k.to_sym => v})
34
- end
35
-
36
- # set attributes
37
- ATTRIBUTES.each do |attribute|
38
- send("#{attribute}=", params[attribute])
27
+ params.each do |param_key, param_value|
28
+ if param_key.to_sym == :indexes
29
+ # check if the param_value is already an Array of Bodhi::TypeIndex objects
30
+ if param_value.is_a?(Array) && param_value.first.is_a?(Bodhi::TypeIndex)
31
+ send("#{param_key}=", param_value)
32
+ else
33
+ # the param_value is a raw json object and needs to be turned into an array of Bodhi::TypeIndex objects
34
+ values = param_value.map{ |index| Bodhi::TypeIndex.new(index) }
35
+ send("#{param_key}=", values)
36
+ end
37
+ else
38
+ send("#{param_key}=", param_value)
39
+ end
39
40
  end
40
41
 
41
42
  # Format type name to be compatible with Ruby Constants
@@ -44,29 +45,6 @@ module Bodhi
44
45
  end
45
46
  end
46
47
 
47
- # Returns a Hash of the Objects form attributes
48
- #
49
- # s = SomeResource.factory.build({foo:"test", bar:12345})
50
- # s.attributes # => { foo: "test", bar: 12345 }
51
- def attributes
52
- result = Hash.new
53
- ATTRIBUTES.each do |attribute|
54
- result[attribute] = send(attribute)
55
- end
56
- result.delete_if { |k, v| v.nil? }
57
- result
58
- end
59
-
60
- # Returns all the Objects attributes as JSON.
61
- # It converts any nested Objects to JSON if they respond to +to_json+
62
- #
63
- # s = SomeResource.factory.build({foo:"test", bar:12345})
64
- # s.to_json # => "{ 'foo':'test', 'bar':12345 }"
65
- def to_json(base=nil)
66
- super if base
67
- attributes.to_json
68
- end
69
-
70
48
  # Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
71
49
  #
72
50
  # obj = Resouce.new
@@ -83,6 +61,10 @@ module Bodhi
83
61
  if result.status != 201
84
62
  raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
85
63
  end
64
+
65
+ if result.headers['location']
66
+ @sys_id = result.headers['location'].match(/types\/(?<name>[a-zA-Z0-9]+)/)[:name]
67
+ end
86
68
  end
87
69
 
88
70
  def delete!
@@ -170,19 +152,17 @@ module Bodhi
170
152
  raise ArgumentError.new("Expected #{type.class} to be a Bodhi::Type")
171
153
  end
172
154
 
173
- klass = Object.const_set(type.name, Class.new {
174
- include Bodhi::Resource
175
- attr_accessor *type.properties.keys
176
- })
155
+ klass = Object.const_set(type.name, Class.new { include Bodhi::Resource })
177
156
 
178
157
  type.properties.each_pair do |attr_name, attr_properties|
179
158
  attr_properties.delete_if{ |key, value| ["system", "trim", "unique", "default", "isCurrentUser", "toLower"].include?(key) }
180
- klass.validates(attr_name.to_sym, attr_properties)
181
- klass.factory.add_generator(attr_name.to_sym, attr_properties)
159
+ klass.field(attr_name, attr_properties)
182
160
  end
183
161
 
184
162
  klass
185
163
  end
186
164
 
187
165
  end
188
- end
166
+ end
167
+
168
+ Dir[File.dirname(__FILE__) + "/types/*.rb"].each { |file| require file }
@@ -0,0 +1,11 @@
1
+ module Bodhi
2
+ class TypeIndex
3
+ include Bodhi::Properties
4
+ include Bodhi::Validations
5
+
6
+ property :keys, :options
7
+
8
+ validates :keys, required: true, multi: true, type: "String"
9
+ validates :options, type: "Object"
10
+ end
11
+ end
@@ -1,11 +1,13 @@
1
1
  module Bodhi
2
2
  class User
3
+ include Bodhi::Factories
4
+ include Bodhi::Properties
3
5
  include Bodhi::Validations
4
6
 
5
- ATTRIBUTES = [:username, :password, :profiles, :email, :firstName, :lastName, :phone]
6
- attr_accessor *ATTRIBUTES
7
7
  attr_accessor :bodhi_context
8
8
 
9
+ property :username, :password, :profiles, :authorizations, :email, :firstName, :lastName, :phone, :usertype, :namespace
10
+
9
11
  validates :username, type: "String", required: true, is_not_blank: true
10
12
  validates :password, type: "String", required: true, is_not_blank: true
11
13
  validates :profiles, type: "String", required: true, multi: true
@@ -14,81 +16,13 @@ module Bodhi
14
16
  validates :lastName, type: "String"
15
17
  validates :phone, type: "String"
16
18
 
17
- def initialize(params={})
18
- # same as symbolize_keys!
19
- params = params.reduce({}) do |memo, (k, v)|
20
- memo.merge({ k.to_sym => v})
21
- end
22
-
23
- # set attributes
24
- ATTRIBUTES.each do |attribute|
25
- send("#{attribute}=", params[attribute])
26
- end
27
- end
28
-
29
- # Returns a Hash of the Objects form attributes
30
- #
31
- # s = SomeResource.factory.build({foo:"test", bar:12345})
32
- # s.attributes # => { foo: "test", bar: 12345 }
33
- def attributes
34
- result = Hash.new
35
- ATTRIBUTES.each do |attribute|
36
- result[attribute] = send(attribute)
37
- end
38
- result
39
- end
40
-
41
- # Returns all the Objects attributes as JSON.
42
- # It converts any nested Objects to JSON if they respond to +to_json+
43
- #
44
- # s = SomeResource.factory.build({foo:"test", bar:12345})
45
- # s.to_json # => "{ 'foo':'test', 'bar':12345 }"
46
- def to_json(base=nil)
47
- super if base
48
- attributes.to_json
49
- end
50
-
51
- # Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
52
- #
53
- # obj = Resouce.new
54
- # obj.save!
55
- # obj.persisted? # => true
56
- def save!
57
- result = bodhi_context.connection.post do |request|
58
- request.url "/#{bodhi_context.namespace}/users"
59
- request.headers['Content-Type'] = 'application/json'
60
- request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
61
- request.body = attributes.to_json
62
- end
63
-
64
- if result.status != 201
65
- raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
66
- end
67
- end
68
-
69
- def delete!
70
- result = bodhi_context.connection.delete do |request|
71
- request.url "/#{bodhi_context.namespace}/users/#{username}"
72
- request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
73
- end
74
-
75
- if result.status != 204
76
- raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
77
- end
78
- end
79
-
80
- # Returns a factory for the Bodhi::User class
81
- def self.factory
82
- @factory ||= Bodhi::Factory.new(Bodhi::User).tap do |factory|
83
- factory.add_generator(:username, type: "String", required: true, is_not_blank: true)
84
- factory.add_generator(:password, type: "String", required: true, is_not_blank: true)
85
- factory.add_generator(:profiles, type: "String", required: true, multi: true)
86
- factory.add_generator(:email, type: "String", is_email: true)
87
- factory.add_generator(:firstName, type: "String")
88
- factory.add_generator(:lastName, type: "String")
89
- factory.add_generator(:phone, type: "String")
90
- end
91
- end
19
+ generates :username, type: "String", required: true, is_not_blank: true
20
+ generates :password, type: "String", required: true, is_not_blank: true
21
+ generates :profiles, type: "String", required: true, multi: true
22
+ generates :email, type: "String", is_email: true
23
+ generates :firstName, type: "String"
24
+ generates :lastName, type: "String"
25
+ generates :phone, type: "String"
92
26
 
93
27
  # Queries the Bodhi API for the given +user_name+ and
94
28
  # returns a Bodhi::User
@@ -135,5 +69,35 @@ module Bodhi
135
69
 
136
70
  result.body
137
71
  end
72
+
73
+ # Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
74
+ #
75
+ # obj = Resouce.new
76
+ # obj.save!
77
+ # obj.persisted? # => true
78
+ def save!
79
+ result = bodhi_context.connection.post do |request|
80
+ request.url "/#{bodhi_context.namespace}/users"
81
+ request.headers['Content-Type'] = 'application/json'
82
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
83
+ request.body = attributes.to_json
84
+ end
85
+
86
+ if result.status != 201
87
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
88
+ end
89
+ end
90
+
91
+ def delete!
92
+ result = bodhi_context.connection.delete do |request|
93
+ request.url "/#{bodhi_context.namespace}/users/#{username}"
94
+ request.headers[bodhi_context.credentials_header] = bodhi_context.credentials
95
+ end
96
+
97
+ if result.status != 204
98
+ raise Bodhi::ApiErrors.new(body: result.body, status: result.status), "status: #{result.status}, body: #{result.body}"
99
+ end
100
+ end
101
+
138
102
  end
139
103
  end
@@ -16,10 +16,8 @@ module Bodhi
16
16
  # type.to_sym # => :type
17
17
  # is_not_blank.to_sym # => :is_not_blank
18
18
  def to_sym
19
- underscore.
20
- gsub("bodhi/", "").
21
- gsub("_validator", "").
22
- to_sym
19
+ name = self.underscore.gsub("bodhi::", "").gsub("_validator", "")
20
+ Bodhi::Support.reverse_camelize(name).to_sym
23
21
  end
24
22
 
25
23
  # Returns the validation's class name in snake_case.
@@ -30,11 +28,7 @@ module Bodhi
30
28
  # type.underscore # => "bodhi/type_validator"
31
29
  # is_not_blank.underscore # => "bodhi/is_not_blank_validator"
32
30
  def underscore
33
- self.class.name.gsub(/::/, '/').
34
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
35
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
36
- tr("-", "_").
37
- downcase
31
+ Bodhi::Support.underscore(self.class.name)
38
32
  end
39
33
 
40
34
  # Returns the validation as an options Hash.
@@ -55,9 +49,8 @@ module Bodhi
55
49
  # Bodhi::Validator.constantize("type") # => #<Bodhi::TypeValidator:0x007fbff403e808>
56
50
  # Bodhi::Validator.constantize("is_not_blank") # => #<Bodhi::IsNotBlankValidator:0x007fbff403e808>
57
51
  def self.constantize(name)
58
- camelized_name = name.to_s.split('_').collect(&:capitalize).join
59
- full_name = "Bodhi::#{camelized_name}Validator"
60
- Object.const_get(full_name)
52
+ camelized_name = Bodhi::Support.camelize(name.to_s)
53
+ Object.const_get("Bodhi::#{camelized_name}Validator")
61
54
  end
62
55
  end
63
56
  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.4.1
4
+ version: 0.5.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-10-26 00:00:00.000000000 Z
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -165,10 +165,14 @@ files:
165
165
  - lib/bodhi-slam/errors/api.rb
166
166
  - lib/bodhi-slam/errors/context.rb
167
167
  - lib/bodhi-slam/factory.rb
168
+ - lib/bodhi-slam/indexes.rb
168
169
  - lib/bodhi-slam/profiles.rb
170
+ - lib/bodhi-slam/properties.rb
169
171
  - lib/bodhi-slam/queries.rb
170
172
  - lib/bodhi-slam/resource.rb
173
+ - lib/bodhi-slam/support.rb
171
174
  - lib/bodhi-slam/types.rb
175
+ - lib/bodhi-slam/types/index.rb
172
176
  - lib/bodhi-slam/users.rb
173
177
  - lib/bodhi-slam/validations.rb
174
178
  - lib/bodhi-slam/validators.rb