bodhi-slam 0.5.4 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5555fc31ea61537f2cf853ef20bc9abcc3be90cd
4
- data.tar.gz: 9e8a0aef642e02f2a562f882efb5e7209783ea5c
3
+ metadata.gz: 9e09a87b26acc4339643d71bf277dad6594e7ab4
4
+ data.tar.gz: 2e9a4a7d6c16223aeb5048f7db9ad62f67b16432
5
5
  SHA512:
6
- metadata.gz: 95543f83c7aa4df83dd787c8286104408ef8c9f2eba82ed6716e7140549e2ed31ba7fd54710e1b3aded240d904b805bab3c4cb20ff50e83ea47ab28fc362b7c3
7
- data.tar.gz: 07de99d9bbdc16829deb95a056cc979ac959d03ed054ecdb60b5e997ca7c411e0402d3c25ba9c8a50d68c50f4f0eaad7cfe075a640cd231f943e7c75a667b6ad
6
+ metadata.gz: b0bab9be47dc879b044428bd45ec76717d2bd0b9e75f14db089bd3d0777c53ee0ff8590ce0d4032cb507876b66c8fa460aa0e559d19cf79473b2421c4941d78e
7
+ data.tar.gz: b7be96e86df8a30bdc2692a025a873c68a5f6574badaa80a3bfc272de81a1adac11e857b6128f394d714e043d1a7e15c8d25ca321b6808d265b15315ac9ae0af
@@ -1,5 +1,3 @@
1
- require 'bodhi-slam/validations'
2
-
3
1
  module Bodhi
4
2
  class Context
5
3
  include Bodhi::Validations
@@ -28,12 +28,9 @@ module Bodhi
28
28
  # Resource.factory.build # => #<Resource:0x007fbff403e808 @name="2-3lmwp^oef@245">
29
29
  # Resource.factory.build(name: "test") # => #<Resource:0x007fbff403e808 @name="test">
30
30
  def build(options={})
31
- # symbolize the option keys
32
- options = options.reduce({}) do |memo, (k, v)|
33
- memo.merge({ k.to_sym => v})
34
- end
35
-
31
+ options = Bodhi::Support.symbolize_keys(options)
36
32
  object = klass.new(bodhi_context: options[:bodhi_context])
33
+
37
34
  @generators.each_pair do |attribute, generator|
38
35
  if options.has_key?(attribute)
39
36
  object.send("#{attribute}=", options[attribute])
@@ -41,6 +38,7 @@ module Bodhi
41
38
  object.send("#{attribute}=", generator.call)
42
39
  end
43
40
  end
41
+
44
42
  object
45
43
  end
46
44
 
@@ -3,11 +3,7 @@ module Bodhi
3
3
  module ClassMethods
4
4
  def indexes; @indexes; end
5
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
-
6
+ options = Bodhi::Support.symbolize_keys(options)
11
7
  @indexes << Bodhi::TypeIndex.new(keys: keys.map(&:to_s), options: options)
12
8
  end
13
9
  end
@@ -6,7 +6,11 @@ module Bodhi
6
6
 
7
7
  attr_accessor :bodhi_context
8
8
 
9
- property :name, :namespace, :dml, :subspace, :parent
9
+ property :dml, type: "Object"
10
+ property :name, type: "String"
11
+ property :namespace, type: "String"
12
+ property :subspace, type: "String"
13
+ property :parent, type: "String"
10
14
 
11
15
  validates :name, type: "String", required: true, is_not_blank: true
12
16
  validates :namespace, type: "String", required: true
@@ -7,10 +7,12 @@ module Bodhi
7
7
 
8
8
  module ClassMethods
9
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!
10
+ def property_names; @properties.keys; end
11
+ def property(name, options)
12
+ attr_accessor name.to_sym
13
+ @properties[name.to_sym] = options.reduce({}) do |memo, (k, v)|
14
+ memo.merge({ Bodhi::Support.reverse_camelize(k.to_s).to_sym => v})
15
+ end
14
16
  end
15
17
  end
16
18
 
@@ -30,9 +32,15 @@ module Bodhi
30
32
  # object = klass.new(name: "Bob", email: "some@email.com")
31
33
  # object.name #=> "Bob"
32
34
  # object.email #=> "some@email.com"
33
- def initialize(params={})
34
- params.each do |param_key, param_value|
35
- send("#{param_key}=", param_value)
35
+ def initialize(options={})
36
+ options = Bodhi::Support.symbolize_keys(options)
37
+ options.each do |property, value|
38
+ property_options = self.class.properties[property]
39
+ if property_options.nil?
40
+ send("#{property}=", value)
41
+ else
42
+ send("#{property}=", Bodhi::Support.coerce(value, property_options))
43
+ end
36
44
  end
37
45
  end
38
46
 
@@ -43,7 +51,7 @@ module Bodhi
43
51
  def attributes
44
52
  attributes = Hash.new
45
53
 
46
- self.class.properties.each do |property|
54
+ self.class.property_names.each do |property|
47
55
  value = send(property)
48
56
  if value.respond_to?(:attributes)
49
57
  attributes[property] = value.attributes.delete_if { |k, v| v.nil? }
@@ -86,7 +94,7 @@ module Bodhi
86
94
  def self.included(base)
87
95
  base.extend(ClassMethods)
88
96
  base.include(InstanceMethods)
89
- base.instance_variable_set(:@properties, Array.new)
97
+ base.instance_variable_set(:@properties, Hash.new)
90
98
  end
91
99
  end
92
100
  end
@@ -22,22 +22,13 @@ module Bodhi
22
22
  # object = User.new(first_name: "John", last_name: "Smith", email: "jsmith@email.com")
23
23
  # object.to_json #=> { "first_name": "John", "last_name": "Smith", "email": "jsmith@email.com" }
24
24
  def field(name, options)
25
- property(name.to_sym)
25
+ property(name.to_sym, options)
26
26
  validates(name.to_sym, options)
27
27
  generates(name.to_sym, options)
28
28
  end
29
29
 
30
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?)
31
+ Bodhi::Type.new(name: self.name, properties: self.properties, indexes: self.indexes, embedded: self.is_embedded?)
41
32
  end
42
33
 
43
34
  # Saves a batch of resources to the Bodhi Cloud in the given +context+
@@ -212,28 +203,6 @@ module Bodhi
212
203
  end
213
204
 
214
205
  module InstanceMethods
215
-
216
- def initialize(params={})
217
- params.each do |param_key, param_value|
218
- if param_value.is_a? Hash
219
- type_validator = self.class.validators[param_key.to_sym].find{ |validator| validator.is_a? Bodhi::TypeValidator }
220
- if Object.const_defined?(type_validator.type)
221
- klass = Object.const_get(type_validator.type)
222
- if klass.ancestors.include?(Bodhi::Resource)
223
- object = klass.new(param_value)
224
- send("#{param_key}=", object)
225
- else
226
- send("#{param_key}=", param_value)
227
- end
228
- else
229
- send("#{param_key}=", param_value)
230
- end
231
- else
232
- send("#{param_key}=", param_value)
233
- end
234
- end
235
- end
236
-
237
206
  # Saves the resource to the Bodhi Cloud. Returns true if record was saved
238
207
  #
239
208
  # obj = Resource.new
@@ -19,5 +19,71 @@ module Bodhi
19
19
  result = underscore(string).split('_').collect(&:capitalize).join
20
20
  uncapitalize(result)
21
21
  end
22
+
23
+ def self.symbolize_keys(hash)
24
+ hash.reduce({}) do |memo, (k, v)|
25
+ value = v.is_a?(Hash) ? symbolize_keys(v) : v
26
+ value = value.is_a?(Array) && value.first.is_a?(Hash) ? value.map{|item| symbolize_keys(item) } : value
27
+ memo.merge({ k.to_sym => value })
28
+ end
29
+ end
30
+
31
+ def self.coerce(value, options)
32
+ options = symbolize_keys(options)
33
+ case options[:type].to_s
34
+ when "Object"
35
+ if options[:multi] == true
36
+ if value.is_a?(String)
37
+ values = JSON.parse(value)
38
+ values.map{|item| Bodhi::Support.symbolize_keys(item) }
39
+ elsif value.is_a?(Array) && value.first.is_a?(Hash)
40
+ value.map{|item| Bodhi::Support.symbolize_keys(item) }
41
+ else
42
+ raise RuntimeError.new("Unable to coerce: #{value} to a JSON Object")
43
+ end
44
+ else
45
+ if value.is_a?(Hash)
46
+ Bodhi::Support.symbolize_keys(value)
47
+ else
48
+ Bodhi::Support.symbolize_keys(JSON.parse(value))
49
+ end
50
+ end
51
+ when "String"
52
+ if options[:multi] == true
53
+ value.map(&:to_s)
54
+ else
55
+ value.to_s
56
+ end
57
+ when "Real"
58
+ if options[:multi] == true
59
+ value.map(&:to_f)
60
+ else
61
+ value.to_f
62
+ end
63
+ when "Integer"
64
+ if options[:multi] == true
65
+ value.map(&:to_i)
66
+ else
67
+ value.to_i
68
+ end
69
+ when "DateTime"
70
+ if options[:multi] == true
71
+ value.map{|item| Time.parse(item.to_s) }
72
+ else
73
+ Time.parse(value.to_s)
74
+ end
75
+ else
76
+ if Object.const_defined?(options[:type].to_s) && Object.const_get(options[:type].to_s).ancestors.include?(Bodhi::Properties)
77
+ klass = Object.const_get(options[:type].to_s)
78
+ if options[:multi] == true
79
+ value.map{|item| klass.new(item) }
80
+ else
81
+ klass.new(value)
82
+ end
83
+ else
84
+ value
85
+ end
86
+ end
87
+ end
22
88
  end
23
89
  end
@@ -6,10 +6,23 @@ module Bodhi
6
6
 
7
7
  attr_accessor :bodhi_context
8
8
 
9
- property :name, :storage_name, :namespace,
10
- :package, :embedded, :properties, :version,
11
- :extends, :indexes, :hidden, :events, :documentation,
12
- :metadata, :encapsulated
9
+ property :properties, type: "Object"
10
+ property :indexes, type: "Bodhi::TypeIndex", multi: true
11
+ property :events, type: "Object"
12
+
13
+ property :name, type: "String"
14
+ property :storage_name, type: "String"
15
+ property :namespace, type: "String"
16
+ property :package, type: "String"
17
+ property :version, type: "String"
18
+ property :extends, type: "String"
19
+
20
+ property :hidden, type: "Boolean"
21
+ property :embedded, type: "Boolean"
22
+ property :metadata, type: "Boolean"
23
+ property :encapsulated, type: "Boolean"
24
+
25
+ property :documentation, type: "Link"
13
26
 
14
27
  validates :name, required: true, is_not_blank: true
15
28
  validates :properties, required: true
@@ -23,28 +36,6 @@ module Bodhi
23
36
  generates :embedded, type: "Boolean"
24
37
  generates :version, type: "String"
25
38
 
26
- def initialize(params={})
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
40
- end
41
-
42
- # Format type name to be compatible with Ruby Constants
43
- if !name.nil? && name[0] == name[0].downcase
44
- name.capitalize!
45
- end
46
- end
47
-
48
39
  # Saves the resource to the Bodhi Cloud. Raises ArgumentError if record could not be saved.
49
40
  #
50
41
  # obj = Resouce.new
@@ -161,10 +152,7 @@ module Bodhi
161
152
  klass = Object.const_set(type.name, Class.new { include Bodhi::Resource })
162
153
 
163
154
  type.properties.each_pair do |attr_name, attr_properties|
164
- # symbolize the attr_properties keys
165
- attr_properties = attr_properties.reduce({}) do |memo, (k, v)|
166
- memo.merge({ k.to_sym => v})
167
- end
155
+ attr_properties = Bodhi::Support.symbolize_keys(attr_properties)
168
156
 
169
157
  # remove Sanitizers
170
158
  attr_properties.delete_if{ |key, value| [:trim, :unique, :default, :isCurrentUser, :toLower].include?(key) }
@@ -181,12 +169,7 @@ module Bodhi
181
169
  if index.is_a? Bodhi::TypeIndex
182
170
  klass.index index.keys, index.options
183
171
  else
184
- # index is a raw json object
185
- # symbolize the index option keys
186
- index = index.reduce({}) do |memo, (k, v)|
187
- memo.merge({ k.to_sym => v})
188
- end
189
-
172
+ index = Bodhi::Support.symbolize_keys(index)
190
173
  klass.index index[:keys], index[:options]
191
174
  end
192
175
  end
@@ -1,11 +1,17 @@
1
1
  module Bodhi
2
2
  class TypeIndex
3
+ include Enumerable
3
4
  include Bodhi::Properties
4
5
  include Bodhi::Validations
5
6
 
6
- property :keys, :options
7
+ property :keys, type: "String", multi: true, required: true
8
+ property :options, type: "Object"
7
9
 
8
10
  validates :keys, required: true, multi: true, type: "String"
9
11
  validates :options, type: "Object"
12
+
13
+ def each
14
+ self.attributes.each{ |k, v| yield(k, v) }
15
+ end
10
16
  end
11
17
  end
@@ -6,7 +6,16 @@ module Bodhi
6
6
 
7
7
  attr_accessor :bodhi_context
8
8
 
9
- property :username, :password, :profiles, :authorizations, :email, :firstName, :lastName, :phone, :usertype, :namespace
9
+ property :username, type: "String"
10
+ property :password, type: "String"
11
+ property :profiles, type: "String", multi: true
12
+ property :authorizations, type: "BodhiAuthorization", multi: true
13
+ property :email, type: "String"
14
+ property :firstName, type: "String"
15
+ property :lastName, type: "String"
16
+ property :phone, type: "String"
17
+ property :usertype, type: "String"
18
+ property :namespace, type: "String"
10
19
 
11
20
  validates :username, type: "String", required: true, is_not_blank: true
12
21
  validates :password, type: "String", required: true, is_not_blank: true
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.4
4
+ version: 0.6.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-12-10 00:00:00.000000000 Z
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday