bodhi-slam 0.6.1 → 0.7.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: 2740f7bb58fef86f5fcb168c16736c0c043f7825
4
- data.tar.gz: 5211d0755728d31d4ffdf18ad60f755511b8eccc
3
+ metadata.gz: 142c61ba57d9623f1875dc7ad3ef1802a4c26c55
4
+ data.tar.gz: 4bc85952eebd17b260acab63647829f44773ec87
5
5
  SHA512:
6
- metadata.gz: 7a650291911ad699c807cf808b4dde162146cf395ab18e2c47057400956743cc0f8091a7ced958b048ffc55b9e3b2e8aa88977e1142c64c12cb9d0549d2dead5
7
- data.tar.gz: 089278eff165608ff8f4bc45858aa678f22cf32ce9eda6d68828a6595dd32496fe66a3a61c4898b6630738732ebbd31bb9ba8b726d0eb212e766a06879f5917c
6
+ metadata.gz: 6144923d080241629cf3c137b89094bc5402df06289ec93ebe01ceba94059af8f8dd8e4d3745a4759500f4da9a57f7a2a3a2119c90ca1a65572da89aabe7f869
7
+ data.tar.gz: 6a71e114c72b1b31bae451a26e1c797cdb70e175c5cb91432c3531fcf69c5ac185aa981ff1cf6b93b0027f8e976e00c9b2dd91689b008d5885438ce229d9e5ad
@@ -0,0 +1,157 @@
1
+ module Bodhi
2
+ module Associations
3
+ module ClassMethods
4
+ def associations; @associations; end
5
+
6
+ def has_one(association_name, options={})
7
+ options = Bodhi::Support.symbolize_keys(options)
8
+ define_association(:has_one, association_name, options)
9
+
10
+ # Define a new helper method to get the association
11
+ define_method(association_name) do
12
+ association = self.class.associations[association_name.to_sym]
13
+ query = Bodhi::Query.new(association[:class_name]).from(self.bodhi_context)
14
+
15
+ if association[:through]
16
+ through_query = Bodhi::Query.new(association[:through][:class_name]).from(self.bodhi_context)
17
+ through_query.where(association[:through][:foreign_key].to_sym => self.send(association[:primary_key]))
18
+ through_query.select(association[:through][:primary_key])
19
+
20
+ puts through_query.url
21
+
22
+ instance_id = through_query.first.send(association[:through][:primary_key])
23
+ query.where(association[:foreign_key].to_sym => instance_id)
24
+ else
25
+ instance_id = self.send(association[:primary_key])
26
+ query.where(association[:foreign_key].to_sym => instance_id)
27
+ end
28
+
29
+ query.and(association[:query])
30
+
31
+ puts query.url
32
+
33
+ query.first
34
+ end
35
+ end
36
+
37
+ def has_many(association_name, options={})
38
+ options = Bodhi::Support.symbolize_keys(options)
39
+ define_association(:has_many, association_name, options)
40
+
41
+ # Define a new helper method to get the association
42
+ define_method(association_name) do
43
+
44
+ # Get the value from the instance object's source_key. Default is :sys_id
45
+ association = self.class.associations[association_name.to_sym]
46
+ query = Bodhi::Query.new(association[:class_name]).from(self.bodhi_context)
47
+
48
+ if association[:through]
49
+ through_query = Bodhi::Query.new(association[:through][:class_name]).from(self.bodhi_context)
50
+ through_query.where(association[:through][:foreign_key].to_sym => self.send(association[:primary_key]))
51
+ through_query.select(association[:through][:primary_key])
52
+
53
+ puts through_query.url
54
+
55
+ method_chain = association[:through][:primary_key].split('.')
56
+ if method_chain.size == 1
57
+ instance_ids = through_query.all.map{ |item| item.send(association[:through][:primary_key]) }
58
+ else
59
+ instance_ids = through_query.all.map do |item|
60
+ method_chain.reduce(item){ |memo, method| memo.send(method) }
61
+ end
62
+ end
63
+
64
+ query.where(association[:foreign_key].to_sym => { "$in" => instance_ids })
65
+ else
66
+ instance_id = self.send(association[:primary_key])
67
+ if instance_id.is_a?(Array)
68
+ query.where(association[:foreign_key].to_sym => { "$in" => instance_id })
69
+ else
70
+ query.where(association[:foreign_key].to_sym => instance_id)
71
+ end
72
+ end
73
+
74
+ query.and(association[:query])
75
+ puts query.url
76
+ query.all
77
+ end
78
+ end
79
+
80
+ def belongs_to(association_name, options={})
81
+ options = Bodhi::Support.symbolize_keys(options)
82
+ define_association(:belongs_to, association_name, options)
83
+
84
+ # Define a new helper method to get the association
85
+ define_method(association_name) do
86
+
87
+ # Get the value from the instance object's source_key. Default is :sys_id
88
+ association = self.class.associations[association_name.to_sym]
89
+ instance_id = self.send(association[:primary_key])
90
+
91
+ # Define & call the query. Returns a single Object or nil
92
+ query = Bodhi::Query.new(association[:class_name]).from(self.bodhi_context)
93
+ query.where(association[:foreign_key].to_sym => instance_id)
94
+ query.and(association[:query])
95
+
96
+ puts query.url
97
+ query.first
98
+ end
99
+ end
100
+
101
+ private
102
+ def define_association(type, name, options)
103
+ options.merge!(association_type: type)
104
+
105
+ if options[:class_name].nil?
106
+ options[:class_name] = Bodhi::Support.camelize(name.to_s)
107
+ end
108
+
109
+ if options[:through].is_a?(String)
110
+ case options[:association_type]
111
+ when :has_one
112
+ options[:through] = {
113
+ class_name: Bodhi::Support.camelize(options[:through]),
114
+ foreign_key: Bodhi::Support.underscore(self.name)+"_id",
115
+ primary_key: "sys_id"
116
+ }
117
+ options[:foreign_key] = Bodhi::Support.underscore(options[:through][:class_name])+"_id"
118
+ when :has_many
119
+ options[:through] = {
120
+ class_name: Bodhi::Support.camelize(options[:through]),
121
+ foreign_key: Bodhi::Support.underscore(self.name)+"_id",
122
+ primary_key: Bodhi::Support.underscore(options[:class_name])+"_id"
123
+ }
124
+ options[:foreign_key] = "sys_id"
125
+ end
126
+ end
127
+
128
+ case type
129
+ when :belongs_to
130
+ if options[:foreign_key].nil?
131
+ options[:foreign_key] = "sys_id"
132
+ end
133
+
134
+ if options[:primary_key].nil?
135
+ options[:primary_key] = Bodhi::Support.underscore(options[:class_name])+"_id"
136
+ end
137
+ else
138
+ if options[:foreign_key].nil?
139
+ options[:foreign_key] = Bodhi::Support.underscore(self.name)+"_id"
140
+ end
141
+
142
+ if options[:primary_key].nil?
143
+ options[:primary_key] = "sys_id"
144
+ end
145
+ end
146
+
147
+ options[:query].nil? ? options[:query] = Hash.new : options[:query]
148
+ @associations[name.to_sym] = options
149
+ end
150
+ end
151
+
152
+ def self.included(base)
153
+ base.extend(ClassMethods)
154
+ base.instance_variable_set(:@associations, Hash.new)
155
+ end
156
+ end
157
+ end
@@ -76,8 +76,17 @@ module Bodhi
76
76
  # Resource.factory.add_generator("name", type: "String")
77
77
  # Resource.factory.add_generator("test", type: "Integer", multi: true, required: true)
78
78
  def add_generator(name, options)
79
+ if options.is_a?(Proc)
80
+ @generators[name.to_sym] = options
81
+ else
82
+ @generators[name.to_sym] = build_default_generator(options)
83
+ end
84
+ end
85
+
86
+ private
87
+ def build_default_generator(options)
79
88
  options = options.reduce({}) do |memo, (k, v)|
80
- 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})
89
+ memo.merge({ Bodhi::Support.underscore(k.to_s).to_sym => v})
81
90
  end
82
91
 
83
92
  case options[:type]
@@ -129,9 +138,9 @@ module Bodhi
129
138
 
130
139
  when "DateTime"
131
140
  if options[:multi]
132
- generator = lambda { [*0..5].sample.times.collect{ Time.at(rand * Time.now.to_i).iso8601 } }
141
+ generator = lambda { [*0..5].sample.times.collect{ Time.at(rand * Time.now.to_i) } }
133
142
  else
134
- generator = lambda { Time.at(rand * Time.now.to_i).iso8601 }
143
+ generator = lambda { Time.at(rand * Time.now.to_i) }
135
144
  end
136
145
 
137
146
  when "Integer"
@@ -230,8 +239,6 @@ module Bodhi
230
239
  end
231
240
  end
232
241
  end
233
-
234
- @generators[name.to_sym] = generator
235
242
  end
236
243
 
237
244
  end
@@ -62,6 +62,8 @@ module Bodhi
62
62
  attributes[property] = value.attributes.delete_if { |k, v| v.nil? }
63
63
  elsif value.is_a?(Array) && value.first.respond_to?(:attributes)
64
64
  attributes[property] = value.map(&:attributes).collect{ |item| item.delete_if { |k, v| v.nil? } }
65
+ elsif value.is_a?(Time)
66
+ attributes[property] = value.iso8601
65
67
  else
66
68
  attributes[property] = value
67
69
  end
@@ -5,7 +5,7 @@ module Bodhi
5
5
  def initialize(klass, controller="resources")
6
6
  @controller = controller
7
7
  @klass = Object.const_get(klass.to_s)
8
- @criteria = []
8
+ @criteria = {}
9
9
  @fields = []
10
10
  @paging = {}
11
11
  @sorting = {}
@@ -13,7 +13,7 @@ module Bodhi
13
13
 
14
14
  def clear!
15
15
  @context = nil
16
- @criteria.clear
16
+ @criteria = {}
17
17
  @fields.clear
18
18
  @paging.clear
19
19
  @sorting.clear
@@ -31,14 +31,8 @@ module Bodhi
31
31
  end
32
32
  params = []
33
33
 
34
- unless criteria.empty?
35
- if criteria.size > 1
36
- where_string = "{$and:[#{criteria.join(',')}]}"
37
- else
38
- where_string = criteria.join
39
- end
40
-
41
- params << "where=#{where_string}"
34
+ unless criteria.keys.empty?
35
+ params << "where=#{criteria.to_json}"
42
36
  end
43
37
 
44
38
  unless fields.empty?
@@ -150,12 +144,14 @@ module Bodhi
150
144
  end
151
145
 
152
146
  def where(query)
153
- unless query.is_a? String
154
- raise ArgumentError.new("Expected String but received #{query.class}")
147
+ if query.is_a?(String)
148
+ json = JSON.parse(query)
149
+ query = Bodhi::Support.symbolize_keys(json)
150
+ else
151
+ query = Bodhi::Support.symbolize_keys(query)
155
152
  end
156
153
 
157
- @criteria << query
158
- @criteria.uniq!
154
+ @criteria.merge!(query)
159
155
  self
160
156
  end
161
157
  alias :and :where
@@ -339,7 +339,7 @@ module Bodhi
339
339
 
340
340
  def self.included(base)
341
341
  base.extend(ClassMethods)
342
- base.include(InstanceMethods, Bodhi::Validations, Bodhi::Properties, Bodhi::Indexes, Bodhi::Factories)
342
+ base.include(InstanceMethods, Bodhi::Properties, Bodhi::Associations, Bodhi::Validations, Bodhi::Indexes, Bodhi::Factories)
343
343
  base.instance_variable_set(:@embedded, false)
344
344
  end
345
345
  end
@@ -28,20 +28,7 @@ module Bodhi
28
28
  when "Link"
29
29
  klass = Hash
30
30
  when "DateTime"
31
- single_comparator = lambda do |item|
32
- begin
33
- DateTime.iso8601(item)
34
- rescue
35
- false
36
- end
37
- end
38
- array_comparator = lambda do |items|
39
- begin
40
- items.collect{ |item| DateTime.iso8601(item) }
41
- rescue
42
- false
43
- end
44
- end
31
+ klass = Time
45
32
  when "Object"
46
33
  klass = Hash
47
34
  single_message = "must be a JSON object"
data/lib/bodhi-slam.rb CHANGED
@@ -13,6 +13,7 @@ require 'bodhi-slam/context'
13
13
 
14
14
  require 'bodhi-slam/properties'
15
15
  require 'bodhi-slam/indexes'
16
+ require 'bodhi-slam/associations'
16
17
 
17
18
  require 'bodhi-slam/batches'
18
19
  require 'bodhi-slam/enumerations'
@@ -49,9 +50,9 @@ class BodhiSlam
49
50
  enumerations = Bodhi::Enumeration.find_all(context)
50
51
 
51
52
  if options[:include].is_a? Array
52
- types = Bodhi::Type.where("{name: { $in: #{options[:include].map(&:to_s)} }}").from(context).all
53
+ types = Bodhi::Type.where(name: { "$in" => options[:include].map(&:to_s) }).from(context).all
53
54
  elsif options[:except].is_a? Array
54
- types = Bodhi::Type.where("{name: { $nin: #{options[:except].map(&:to_s)} }}").from(context).all
55
+ types = Bodhi::Type.where(name: { "$nin" => options[:except].map(&:to_s) }).from(context).all
55
56
  else
56
57
  types = Bodhi::Type.find_all(context)
57
58
  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.6.1
4
+ version: 0.7.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-12-16 00:00:00.000000000 Z
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -143,6 +143,7 @@ extensions: []
143
143
  extra_rdoc_files: []
144
144
  files:
145
145
  - lib/bodhi-slam.rb
146
+ - lib/bodhi-slam/associations.rb
146
147
  - lib/bodhi-slam/batches.rb
147
148
  - lib/bodhi-slam/batches/resource.rb
148
149
  - lib/bodhi-slam/context.rb