mongoid-dsl 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4fa301c9617088b3bf2b0f82609924c9a7aea0af
4
+ data.tar.gz: ddb141cc05b803af124b4b0a0dbd9c7f4ff747cc
5
+ SHA512:
6
+ metadata.gz: 62be09d282612a3248c3d210bec2c13641c99f8ff38f69db607c288db2de7f5eb6def7c9c4544c89e844aee999a02e14ec887e7df809e59e9a2152c360a42f37
7
+ data.tar.gz: 215497d4ee504fd75c1f326098eed9aed04bf34e9e8ab10c61eb870635cccbb322dd9fa196f40aabb1ad1ceb85ac511c44eeaaf2b8bc97786fd7b482d648cb7f
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Adam Luzsi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ Mongoid-DSL
2
+ ==============
3
+
4
+ Ruby Module for ease of use with mongoid based models
5
+
6
+ with this module, you can do:
7
+ * recursive methods by calling [:_find,:_find_by,:_where,:_all] on the model (even on deeply embedded ones)
8
+ * check connection relation between two model
9
+ * get :parents, :references or :documents
10
+
11
+
12
+ ### Examples for use
13
+
14
+ recursive find
15
+ If the model path is not clear by default the shortest way will be chosen,
16
+ You can modify this behavior by adding model names to the args, before or after the query hash
17
+ for example: TestD._where( TestA, TestB, TestC, test: "world" ).inspect
18
+
19
+ The return object will be array if the target model is not a main one but an embedded
20
+
21
+ ```ruby
22
+
23
+ require 'mongoid-dsl'
24
+ require_relative "helper/con"
25
+
26
+ :TestA.mongoid_name
27
+ #> nil
28
+
29
+ class TestA
30
+
31
+ include Mongoid::Document
32
+ include Mongoid::Timestamps
33
+
34
+ store_in :collection => self.mongoid_name
35
+
36
+ embeds_many :TestB.mongoid_name
37
+
38
+ field :test,
39
+ :type => String,
40
+ :presence => true,
41
+ :desc => "description for this field",
42
+ :accept_only => %W[ hello world hello\ world ]
43
+
44
+ end
45
+
46
+ :TestA.mongoid_name
47
+ #> "test_a"
48
+
49
+ class TestB
50
+
51
+ include Mongoid::Document
52
+ include Mongoid::Timestamps
53
+
54
+ embedded_in :TestA.mongoid_name
55
+ embeds_many :TestC.mongoid_name
56
+
57
+ field :test,
58
+ :type => String,
59
+ :presence => true,
60
+ :desc => "description for this field",
61
+ :uniq => true
62
+
63
+ end
64
+
65
+ class TestC
66
+
67
+ include Mongoid::Document
68
+ include Mongoid::Timestamps
69
+
70
+ embedded_in :TestB.mongoid_name
71
+ embeds_many :TestD.mongoid_name
72
+
73
+ field :test,
74
+ :type => String,
75
+ :presence => true,
76
+ :desc => "description for this field"
77
+
78
+ end
79
+
80
+ class TestD
81
+
82
+ include Mongoid::Document
83
+ include Mongoid::Timestamps
84
+
85
+ embedded_in :TestC.mongoid_name
86
+
87
+ field :test,
88
+ :type => String,
89
+ :presence => true,
90
+ :desc => "description for this field"
91
+
92
+ end
93
+
94
+ test_a= TestA.create! test: "hello"
95
+ test_a.test_b.create(test: "world")
96
+ test_b= test_a.test_b.last
97
+ test_b.test_c.create(test: "world")
98
+ test_c= test_b.test_c.last
99
+ test_c.test_d.create(test: "world")
100
+ test_d= test_c.test_d.last
101
+
102
+ # puts TestD._find(test_d['_id']).inspect
103
+ #<TestD _id: 536a10f6241548c811000004, created_at: 2014-05-07 10:54:46 UTC, updated_at: 2014-05-07 10:54:46 UTC, test: "world">
104
+
105
+ puts TestD._all.inspect
106
+ #> return every embedded TestD obj
107
+
108
+ puts TestD._where( test: "world" ).inspect
109
+ #> return by criteria the embedded TestD objects
110
+
111
+ Mongoid.purge!
112
+
113
+
114
+ ```
115
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require File.join"bundler","gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1 @@
1
+ Mongoid.load!(File.join(File.dirname(__FILE__),"mongoid.yml"), :development)
@@ -0,0 +1,6 @@
1
+ development:
2
+ sessions:
3
+ default:
4
+ database: mongoid
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,87 @@
1
+ require_relative "../lib/mongoid-dsl"
2
+ require_relative "helper/con"
3
+
4
+ :TestA.mongoid_name #> nil
5
+
6
+ class TestA
7
+
8
+ include Mongoid::Document
9
+ include Mongoid::Timestamps
10
+
11
+ store_in :collection => self.mongoid_name
12
+
13
+ embeds_many :TestB.mongoid_name
14
+
15
+ field :test,
16
+ :type => String,
17
+ :presence => true,
18
+ :desc => "description for this field",
19
+ :accept_only => %W[ hello world hello\ world ]
20
+
21
+ end
22
+
23
+ :TestA.mongoid_name #> "test_a"
24
+
25
+ class TestB
26
+
27
+ include Mongoid::Document
28
+ include Mongoid::Timestamps
29
+
30
+ embedded_in :TestA.mongoid_name
31
+ embeds_many :TestC.mongoid_name
32
+
33
+ field :test,
34
+ :type => String,
35
+ :presence => true,
36
+ :desc => "description for this field",
37
+ :uniq => true
38
+
39
+ end
40
+
41
+ class TestC
42
+
43
+ include Mongoid::Document
44
+ include Mongoid::Timestamps
45
+
46
+ embedded_in :TestB.mongoid_name
47
+ embeds_many :TestD.mongoid_name
48
+
49
+ field :test,
50
+ :type => String,
51
+ :presence => true,
52
+ :desc => "description for this field"
53
+
54
+ end
55
+
56
+ class TestD
57
+
58
+ include Mongoid::Document
59
+ include Mongoid::Timestamps
60
+
61
+ embedded_in :TestC.mongoid_name
62
+
63
+ field :test,
64
+ :type => String,
65
+ :presence => true,
66
+ :desc => "description for this field"
67
+
68
+ end
69
+
70
+ test_a= TestA.create! test: "hello"
71
+ test_a.test_b.create(test: "world")
72
+ test_b= test_a.test_b.last
73
+ test_b.test_c.create(test: "world")
74
+ test_c= test_b.test_c.last
75
+ test_c.test_d.create(test: "world")
76
+ test_d= test_c.test_d.last
77
+
78
+ # puts TestD._find(test_d['_id']).inspect
79
+ #<TestD _id: 536a10f6241548c811000004, created_at: 2014-05-07 10:54:46 UTC, updated_at: 2014-05-07 10:54:46 UTC, test: "world">
80
+
81
+ puts TestD._all.inspect
82
+ #> return every embedded TestD obj
83
+
84
+ puts TestD._where( TestA, test: "world" ).inspect
85
+ #> return by criteria the embedded TestD objects
86
+
87
+ Mongoid.purge!
@@ -0,0 +1,183 @@
1
+ # Validates whether the value of the specified attribute is available in a
2
+ # particular enumerable object.
3
+ #
4
+ # class Person < ActiveRecord::Base
5
+ # validates_inclusion_of :gender, :in => %w( m f )
6
+ # validates_inclusion_of :age, :in => 0..99
7
+ # validates_inclusion_of :format, :in => %w( jpg gif png ), :message => "extension %{value} is not included in the list"
8
+ # validates_inclusion_of :states, :in => lambda{ |person| STATES[person.country] }
9
+ # end
10
+ #
11
+ # Configuration options:
12
+ # * <tt>:in</tt> - An enumerable object of available items. This can be
13
+ # supplied as a proc or lambda which returns an enumerable. If the enumerable
14
+ # is a range the test is performed with <tt>Range#cover?</tt>
15
+ # (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
16
+ # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
17
+ # * <tt>:message</tt> - Specifies a custom error message (default is: "is not
18
+ # included in the list").
19
+ # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute
20
+ # is +nil+ (default is +false+).
21
+ # * <tt>:allow_blank</tt> - If set to true, skips this validation if the
22
+ # attribute is blank (default is +false+).
23
+ # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
24
+ # validation contexts by default (+nil+), other options are <tt>:create</tt>
25
+ # and <tt>:update</tt>.
26
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
27
+ # the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
28
+ # <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
29
+ # or string should
30
+ # return or evaluate to a true or false value.
31
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
32
+ # if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
33
+ # or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
34
+ # proc or string should return or evaluate to a true or false value.
35
+ # * <tt>:strict</tt> - Specifies whether validation should be strict.
36
+ # See <tt>ActiveModel::Validation#validates!</tt> for more information.
37
+
38
+ [ :desc, :description, :comment ].each do |name_to_use|
39
+ Mongoid::Fields.option name_to_use do |model, field, value|
40
+
41
+ if field.instance_variable_get('@options')[name_to_use].class <= Array
42
+ field.instance_variable_get('@options')[name_to_use]= field.instance_variable_get('@options')[name_to_use].join
43
+ end
44
+
45
+ field.instance_variable_get('@options')[:desc]= field.instance_variable_get('@options').delete(name_to_use)
46
+
47
+ end
48
+ end
49
+
50
+ [ :required, :present, :presence, :need ].each do |name_to_use|
51
+ Mongoid::Fields.option name_to_use do |model, field, value|
52
+
53
+ field.instance_variable_get('@options')[:presence]= field.instance_variable_get('@options').delete(name_to_use)
54
+ if value == true
55
+ model.instance_eval do
56
+ validates_presence_of field.instance_variable_get('@name')
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+
63
+ [ :uniq, :unique, :uniqueness ].each do |name_to_use|
64
+ Mongoid::Fields.option name_to_use do |model, field, value|
65
+
66
+ field.instance_variable_get('@options')[:uniqueness]= field.instance_variable_get('@options').delete(name_to_use)
67
+ if value == true
68
+ model.instance_eval do
69
+ validates_uniqueness_of field.instance_variable_get('@name')
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ [ :accept, :accept_only, :can, :can_be, :only, :inclusion ].each do |name_to_use|
77
+ Mongoid::Fields.option name_to_use do |model, field, value|
78
+
79
+ field.instance_variable_get('@options')[:inclusion]= field.instance_variable_get('@options').delete(name_to_use)
80
+ model.instance_eval do
81
+
82
+ validates_inclusion_of field.instance_variable_get('@name'),
83
+ in: [*value]
84
+
85
+ end
86
+
87
+ end
88
+ end
89
+
90
+ [ :deny, :denied, :denied_only, :not, :exclude, :exclusion ].each do |name_to_use|
91
+ Mongoid::Fields.option name_to_use do |model, field, value|
92
+
93
+ field.instance_variable_get('@options')[:exclusion]= field.instance_variable_get('@options').delete(name_to_use)
94
+ model.instance_eval do
95
+
96
+ validates_exclusion_of field.instance_variable_get('@name'),
97
+ in: [*value]
98
+
99
+ end
100
+
101
+ end
102
+ end
103
+
104
+
105
+ [ :with, :match, :regex, :regexp ].each do |name_to_use|
106
+ Mongoid::Fields.option name_to_use do |model, field, value|
107
+
108
+ field.instance_variable_get('@options')[:format]= field.instance_variable_get('@options').delete(name_to_use)
109
+ model.instance_eval do
110
+
111
+ if value.class <= Regexp
112
+
113
+ validates_format_of field.instance_variable_get('@name'),
114
+ with: value
115
+
116
+ elsif value.class <= Array
117
+
118
+ validates_format_of field.instance_variable_get('@name'),
119
+ in: value
120
+
121
+ else
122
+ raise ArgumentError,"invalid regexp: #{value}"
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+ end
129
+
130
+
131
+ [ :without, :not_match, :cant_be_regex, :cant_be_regexp ].each do |name_to_use|
132
+ Mongoid::Fields.option name_to_use do |model, field, value|
133
+
134
+ field.instance_variable_get('@options')[:format]= field.instance_variable_get('@options').delete(name_to_use)
135
+ model.instance_eval do
136
+
137
+ if value.class <= Regexp
138
+
139
+ validates_format_of field.instance_variable_get('@name'),
140
+ without: value
141
+
142
+ else
143
+ raise ArgumentError,"invalid regexp: #{value}"
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+ end
150
+
151
+ [ :length ].each do |name_to_use|
152
+ Mongoid::Fields.option name_to_use do |model, field, value|
153
+
154
+ field.instance_variable_get('@options')[:format]= field.instance_variable_get('@options').delete(name_to_use)
155
+ model.instance_eval do
156
+
157
+ if value.class <= Hash
158
+
159
+ options_to_use= {}
160
+
161
+ [:max,:maximum,'max','maximum'].each do |key_name|
162
+ options_to_use[:maximum] ||= value[key_name]
163
+ end
164
+
165
+ [:min,:minimum,'min','minimum'].each do |key_name|
166
+ options_to_use[:minimum] ||= value[key_name]
167
+ end
168
+
169
+ [:within,:range,'within','range'].each do |key_name|
170
+ options_to_use[:within] ||= value[key_name]
171
+ end
172
+
173
+ validates_length_of field.instance_variable_get('@name'),
174
+ options_to_use
175
+
176
+ else
177
+ raise ArgumentError,"invalid Hash obj: #{value}"
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+ end
@@ -0,0 +1,645 @@
1
+ module Mongoid
2
+ module DSL
3
+
4
+ module Document
5
+
6
+ module Extend
7
+
8
+ def documents
9
+
10
+ exceptions= %w[
11
+ Mongoid::Relations::Embedded::In
12
+ Mongoid::Relations::Referenced::In
13
+ ]
14
+
15
+ return_array = Array.new
16
+ self.relations.each do |model_name,module_propertys|
17
+ if !exceptions.include?(module_propertys[:relation].to_s)
18
+ return_array.push model_name
19
+ end
20
+ end
21
+
22
+ return return_array
23
+ rescue Exception
24
+ return []
25
+
26
+ end
27
+
28
+ def relation_connection_type(to_model)
29
+ begin
30
+
31
+ return_none= "Mongoid::Relations::None"
32
+ return_self= "Mongoid::Relations::Self"
33
+
34
+ if to_model.nil?
35
+ return return_none
36
+ end
37
+
38
+ if self.to_s == to_model.to_s
39
+ return return_self
40
+ end
41
+
42
+ relation_type_data= self.reflect_on_association(to_model.convert_model_name)
43
+
44
+ if relation_type_data.nil?
45
+ return return_none
46
+ else
47
+ return relation_type_data[:relation].to_s
48
+ end
49
+
50
+ end
51
+ end
52
+ def reverse_relation_conn_type(from_model)
53
+ begin
54
+
55
+ return_none= "Mongoid::Relations::None"
56
+ return_self= "Mongoid::Relations::Self"
57
+
58
+ if from_model.nil?
59
+ return return_none
60
+ end
61
+
62
+ if self.to_s == from_model.to_s
63
+ return return_self
64
+ end
65
+
66
+ relation_type_data= from_model.reflect_on_association(self.convert_model_name)
67
+
68
+ if relation_type_data.nil?
69
+ return return_none
70
+ else
71
+ return relation_type_data[:relation].to_s
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ #> not yet finished
78
+ def available_fields
79
+ Mongoid::Document.available_fields(self)
80
+ end
81
+
82
+ def properties
83
+
84
+ hash_data = Hash.new
85
+ self.fields.each do |key,value|
86
+ hash_data[value.name]=value.options[:type]
87
+ end
88
+ return hash_data
89
+
90
+ end
91
+
92
+ def parents
93
+ begin
94
+
95
+ exceptions= %w[
96
+ Mongoid::Relations::Embedded::In
97
+ ]
98
+
99
+ return_array = Array.new
100
+ self.relations.each do |model_name,module_propertys|
101
+ if exceptions.include?(module_propertys[:relation].to_s)
102
+ return_array.push model_name
103
+ end
104
+ end
105
+ return return_array
106
+
107
+ end
108
+ end
109
+ def references
110
+ begin
111
+
112
+ exceptions= %w[
113
+ Mongoid::Relations::Referenced::In
114
+ Mongoid::Relations::Referenced::ManyToMany
115
+ ]
116
+
117
+ return_array = Array.new
118
+ self.relations.each do |model_name,module_propertys|
119
+ if exceptions.include?(module_propertys[:relation].to_s)
120
+ return_array.push model_name
121
+ end
122
+ end
123
+ return return_array
124
+
125
+ end
126
+ end
127
+
128
+ def get_path(*included)
129
+
130
+ # create defaults
131
+ begin
132
+ return_array= Array.new()
133
+ check_list= Array.new#.push(self.to_s)
134
+ relations_hash = Hash.new()
135
+ #chains= Hash.new
136
+ chains= Array.new
137
+ end
138
+
139
+ # get parents for every participants
140
+ begin
141
+ models_to_check = Array.new.push self
142
+ loop do
143
+ tmp_array = Array.new
144
+ break if models_to_check.empty?
145
+ models_to_check.each do |one_models_element|
146
+ begin
147
+ one_models_element.parents.each do |one_parent_in_underscore|
148
+ parent_model= one_parent_in_underscore.convert_model_name
149
+ tmp_array.push parent_model if !relations_hash.keys.include?(parent_model)
150
+ relations_hash[one_models_element] ||= Array.new
151
+ relations_hash[one_models_element].push parent_model
152
+ end
153
+ rescue NoMethodError
154
+ #next
155
+ end
156
+ end
157
+ models_to_check= tmp_array
158
+ end
159
+ models_to_check.clear
160
+ end
161
+
162
+ # make connections from relation pairs
163
+ begin
164
+
165
+ # generate pre path chains
166
+ # minden egyes szulo,s szulo szulo elemen egyesevel menj vegig (csak elso elemet vizsgalva) es ahol tobb mint egy elem talalhato azt torold
167
+ # igy legkozelebb az mar nem lesz lehetseges utvonal, ahol pedig mindenhol csak 1 elem volt ott ellenorzid
168
+ # hogy mar megtalalt sorrol van e szo, s ha igen torold a relations_hash[self] ből ezt a szulot (2. elem a path ban)
169
+ # the goal is to get every 1 element from sub arrays
170
+
171
+ begin
172
+ available_paths= Array.new
173
+ return nil if relations_hash[self].nil?
174
+ loop do
175
+
176
+ # defaults
177
+ begin
178
+ next_element_to_find = self
179
+ delete_element = Hash.new
180
+ one_chain = Array.new
181
+ end
182
+
183
+ # chain element builder
184
+ begin
185
+ loop do
186
+ if !relations_hash[next_element_to_find].nil? && relations_hash[next_element_to_find] != Array.new
187
+
188
+ one_chain.push(relations_hash[next_element_to_find][0])
189
+ if relations_hash[next_element_to_find].count > 1
190
+ delete_element = Hash.new
191
+ delete_element[next_element_to_find]= relations_hash[next_element_to_find][0]
192
+ end
193
+
194
+ next_element_to_find= relations_hash[next_element_to_find][0]
195
+ else
196
+ break
197
+ end
198
+ end
199
+ end
200
+
201
+ # remove already checked tree
202
+ begin
203
+ if delete_element != Hash.new
204
+ relations_hash[delete_element.keys[0]].delete_at(
205
+ relations_hash[delete_element.keys[0]].index(
206
+ delete_element[delete_element.keys[0]]))
207
+ delete_element= Hash.new
208
+ end
209
+ end
210
+
211
+ # add new element to chains
212
+ begin
213
+ unless chains.include? one_chain
214
+ chains.push one_chain
215
+ else
216
+ break
217
+ end
218
+ end
219
+ end
220
+
221
+ end
222
+
223
+ end
224
+
225
+ # after format and check params for contains
226
+ begin
227
+
228
+ # pre chains trim
229
+ begin
230
+
231
+ tmp_array= Array.new
232
+ chains.each do |one_element|
233
+ if !one_element.contain?(included)
234
+ tmp_array.push one_element
235
+ end
236
+ end
237
+ tmp_array.each do |one_element_to_delete|
238
+ chains.delete_at(chains.index(one_element_to_delete))
239
+ end
240
+
241
+ end
242
+
243
+ # choose the shortest path
244
+ begin
245
+ chain_list_max_count= nil
246
+ chains.each do |one_chain_list|
247
+ counter= one_chain_list.count
248
+ chain_list_max_count ||= (counter+1)
249
+ if counter < chain_list_max_count
250
+ return_array= one_chain_list
251
+ counter= chain_list_max_count
252
+ end
253
+ end
254
+ end
255
+
256
+ # reverse array
257
+ begin
258
+ return_array.reverse!
259
+ end
260
+
261
+ # add new element as self for first
262
+ begin
263
+ return_array.push self
264
+ end
265
+
266
+
267
+ end
268
+
269
+ return return_array
270
+ end
271
+
272
+ def __query_wrapper(*args)
273
+
274
+ # defaults
275
+ begin
276
+ return_data= nil
277
+ # params field
278
+ field_hash= Hash.new
279
+ # models
280
+ models_container= Array.new
281
+ # mother model
282
+ mother_model= nil
283
+ # method_to_use
284
+ method_to_use= Hash.new
285
+
286
+ args.each do |one_argument|
287
+ case true
288
+
289
+ when one_argument.class <= String
290
+ begin
291
+ field_hash['_id']= Moped::BSON::ObjectId.from_string(one_argument)
292
+ rescue
293
+ begin
294
+ models_container.push one_argument.constantize
295
+ rescue NameError
296
+ #method_to_use= one_argument
297
+ end
298
+ end
299
+
300
+ when one_argument.class <= Hash
301
+ begin
302
+ one_argument.each do |key,value|
303
+ if key.to_s == '_id'
304
+ field_hash['_id']= Moped::BSON::ObjectId.from_string(value.to_s)
305
+ else
306
+ field_hash[key]=value
307
+ end
308
+ end
309
+ end
310
+
311
+ when one_argument.class <= Class
312
+ models_container.push one_argument
313
+
314
+ when one_argument.class <= Array
315
+ method_to_use= one_argument
316
+
317
+ end
318
+ end
319
+
320
+ end
321
+
322
+ # mother model find, and path generate
323
+ begin
324
+
325
+ full_path = self.get_path(*models_container) || Array.new.push(self)
326
+ mother_model= full_path.shift
327
+
328
+ full_path.count.times do |index|
329
+ full_path[index]= full_path[index].convert_model_name
330
+ end
331
+
332
+ end
333
+
334
+ # path trim
335
+ begin
336
+
337
+ deep_level = (full_path.count) || 0
338
+ chains= full_path
339
+
340
+ if !full_path.empty? && !full_path.nil?
341
+ full_path= full_path.join('.')+'.'
342
+ else
343
+ full_path= String.new
344
+ end
345
+
346
+ end
347
+
348
+ # start query
349
+ begin
350
+
351
+ # build app query args
352
+ begin
353
+
354
+ gen_field_hash= lambda { |query_hash,*pre_path_elements|
355
+ query_fields= Hash.new()
356
+
357
+ full_path_str= nil
358
+ if !pre_path_elements.empty? && !pre_path_elements.nil?
359
+ full_path_str= pre_path_elements.join('.')+'.'
360
+ else
361
+ full_path_str= ""
362
+ end
363
+
364
+ return query_hash.map_hash { |key,value|
365
+ { "#{full_path_str}#{key}" => value }
366
+ }
367
+
368
+
369
+ }
370
+
371
+ query_fields= gen_field_hash.call field_hash, *chains
372
+
373
+ end
374
+
375
+ # do query ask from db
376
+ begin
377
+ if method_to_use[1][:arguments?]
378
+ query_data = mother_model.__send__(
379
+ method_to_use[0].to_s,
380
+ query_fields)
381
+ else
382
+ query_data = mother_model.__send__(
383
+ method_to_use[0].to_s)
384
+ end
385
+ end
386
+
387
+ # return data level
388
+ begin
389
+
390
+ if deep_level == 0
391
+ return_data=query_data
392
+ else
393
+
394
+ # go down for embeds docs
395
+ begin
396
+
397
+ last_round = (chains.count-1)
398
+ map_object = query_data
399
+ return_array = [] #> Mongoid::Criteria.new
400
+ chains.count.times do |deepness|
401
+ begin
402
+ children = Array.new
403
+ if map_object.class == Array || map_object.class == Mongoid::Criteria
404
+
405
+ map_object.each do |one_element_of_the_map_array|
406
+ subclass_data = one_element_of_the_map_array.__send__(chains[deepness])
407
+ if subclass_data.class <= Array || subclass_data.class <= Mongoid::Criteria
408
+ subclass_data.each do |one_element_of_tmp_children|
409
+ if deepness < last_round
410
+ children.push one_element_of_tmp_children
411
+ else
412
+ return_array.push one_element_of_tmp_children
413
+ end
414
+ end
415
+ else
416
+ if deepness < last_round
417
+ children.push subclass_data
418
+ else
419
+ return_array.push subclass_data
420
+ end
421
+ end
422
+ end
423
+
424
+ else
425
+ subclass_data = map_object.__send__(chains[deepness])
426
+ if subclass_data.class == Array || subclass_data.class == Mongoid::Criteria
427
+ subclass_data.each do |one_element_of_tmp_children|
428
+ if deepness < last_round
429
+ children.push one_element_of_tmp_children
430
+ else
431
+ return_array.push one_element_of_tmp_children
432
+ end
433
+ end
434
+ else
435
+ if deepness < last_round
436
+ children.push subclass_data
437
+ else
438
+ return_array.push subclass_data
439
+ end
440
+ end
441
+ end
442
+ map_object=children.uniq
443
+ rescue NoMethodError => ex
444
+ STDERR.puts(ex)
445
+ end
446
+ end
447
+
448
+ return_data= return_array
449
+
450
+ end
451
+ end
452
+ end
453
+
454
+ # trim not used class
455
+ if method_to_use[1][:arguments?]
456
+
457
+ tmp_array= []
458
+ return_data.map! { |one_element|
459
+
460
+ tmp_hash= one_element.convert_to_hash#instance_variables.each{ |var| tmp_hash[var.to_s.delete("@")] = one_element.instance_variable_get(var) }
461
+
462
+ if tmp_hash["attributes"].deep_include?(field_hash) || tmp_hash["attributes"].deep_include?(field_hash.map_hash{|k,v|[k.to_s,v]})
463
+ one_element
464
+ else
465
+ nil
466
+ end
467
+
468
+ }.compact!
469
+
470
+ end
471
+
472
+
473
+ end
474
+
475
+ # TODO find out how to add elements to a criteria obj
476
+ # # convert to mongid criteria
477
+ # begin
478
+ #
479
+ # mongoid_crit= Mongoid::Criteria.new(self)
480
+ #
481
+ # puts mongoid_crit.methods - Object.methods
482
+ #
483
+ #
484
+ # mongoid_crit.add_to_set return_data[0]
485
+ #
486
+ # return mongoid_crit
487
+ #
488
+ # rescue Exception
489
+ # end
490
+
491
+ return return_data
492
+
493
+ end
494
+
495
+ def _where(*args)
496
+ self.__query_wrapper(['where',{:arguments? => true}],*args)
497
+ end
498
+ def _all(*args)
499
+ self.__query_wrapper(['all',{:arguments? => false}],*args)
500
+ end
501
+
502
+ def _find(target_id)
503
+
504
+ # pre validation
505
+ begin
506
+ if target_id.class != Moped::BSON::ObjectId && target_id.class != String
507
+ raise ArgumentError, "id parameter must be id or ObjectId"
508
+ end
509
+ end
510
+
511
+ # Do the Gangnam style
512
+ begin
513
+ return_doc = self._where( '_id' => target_id )[0]
514
+ end
515
+
516
+ return return_doc
517
+ end
518
+ def _find_by(*args)
519
+
520
+ # Do the Gangnam style
521
+ begin
522
+ return_array = self._where(*args).first
523
+ end
524
+
525
+ return return_array
526
+ end
527
+
528
+ alias :this_to_me :relation_connection_type
529
+ alias :me_to_this :reverse_relation_conn_type
530
+
531
+ end
532
+
533
+ module Include
534
+
535
+ def _parent
536
+
537
+ self.class.parents.each do |parent_underscore_name|
538
+ if self.__send__("has_#{parent_underscore_name}?") == true
539
+ return self.__send__(parent_underscore_name)
540
+ end
541
+ end
542
+ return nil
543
+
544
+ end
545
+
546
+ def _reference
547
+
548
+ self.class.references.each do |parent_underscore_name|
549
+ if self.__send__("has_#{parent_underscore_name}?") == true
550
+ return self.__send__(parent_underscore_name)
551
+ end
552
+ end
553
+ return nil
554
+
555
+ end
556
+
557
+ alias :get_parent_doc :_parent
558
+ alias :get_reference_doc :_reference
559
+
560
+ end
561
+
562
+ end
563
+
564
+ module StringExt
565
+ module Include
566
+ def to_ObjectId
567
+ begin
568
+ Moped::BSON::ObjectId.from_string(self.to_s)
569
+ rescue Exception => ex
570
+ ex.logger
571
+ nil
572
+ end
573
+ end
574
+ end
575
+ end
576
+
577
+ module CoreExt
578
+
579
+ module Include
580
+
581
+ # convert mongoid name
582
+ def convert_model_name
583
+
584
+ unless self.class <= Class || self.class <= String || self.class <= Symbol || self.class <= NilClass
585
+ raise ArgumentError, "invalid input, must be Class or String: => #{self.class} (#{self})"
586
+ end
587
+
588
+ if self.class <= NilClass
589
+ return nil
590
+ end
591
+
592
+ case self.class.to_s
593
+
594
+ when "Class"
595
+ return self.to_s.split('::').last.underscore
596
+
597
+ when "String","Symbol"
598
+ Mongoid.models.each do |one_model_name|
599
+ if self.to_s == one_model_name.to_s.split('::').last.underscore || \
600
+ self.to_s == one_model_name.to_s.split('::').last.underscore+('s')
601
+
602
+ return one_model_name.to_s.constantize
603
+
604
+ end
605
+ end
606
+
607
+ return self.to_s.split('::').last.underscore
608
+
609
+
610
+ end
611
+
612
+ return nil
613
+ end
614
+
615
+ alias :mongoise_name :convert_model_name
616
+ alias :mongoize_name :convert_model_name
617
+ alias :mongoid_name :convert_model_name
618
+
619
+ alias :mongoid_name_convert :convert_model_name
620
+
621
+ end
622
+
623
+ end
624
+
625
+ end
626
+ end
627
+
628
+ begin
629
+ Mongoid::Config.inject_singleton_method :register_model, add: :before do |klass|
630
+ klass.__send__ :extend, Mongoid::DSL::Document::Extend
631
+ klass.__send__ :include, Mongoid::DSL::Document::Include
632
+ end
633
+ rescue NoMethodError
634
+ end
635
+
636
+ [
637
+ Class,
638
+ Symbol,
639
+ NilClass,
640
+ String
641
+ ].each do |class_name|
642
+ class_name.__send__ :include, Mongoid::DSL::CoreExt::Include
643
+ end
644
+
645
+ String.__send__ :include, Mongoid::DSL::StringExt::Include
@@ -0,0 +1,6 @@
1
+
2
+ require 'mpatch'
3
+ require 'mongoid'
4
+ require 'procemon'
5
+
6
+ Dir.glob( File.join( File.dirname(__FILE__),'**','*.{ru,rb}') ).each{|p|require(p)}
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |spec|
4
+
5
+ spec.name = "mongoid-dsl"
6
+ spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
7
+ spec.authors = ["Adam Luzsi"]
8
+ spec.email = ["adamluzsi@gmail.com"]
9
+ spec.description = "Ruby Module for ease of use with mongoid based models"
10
+ spec.summary = "Ruby Module for ease of use mongoid models"
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_development_dependency "bundler"
18
+ spec.add_development_dependency "rake"
19
+
20
+ spec.add_dependency "procemon", ">= 2.0.0"
21
+ spec.add_dependency "mongoid", ">= 3.1.0"
22
+ spec.add_dependency "mpatch", ">= 2.9.0"
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Luzsi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: procemon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: mpatch
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.9.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.9.0
83
+ description: Ruby Module for ease of use with mongoid based models
84
+ email:
85
+ - adamluzsi@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - VERSION
95
+ - examples/helper/con.rb
96
+ - examples/helper/mongoid.yml
97
+ - examples/recursive_find.rb
98
+ - lib/mongoid-dsl.rb
99
+ - lib/mongoid-dsl/fields-ext.rb
100
+ - lib/mongoid-dsl/monkey.rb
101
+ - mongoid-dsl.gemspec
102
+ homepage:
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.2.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Ruby Module for ease of use mongoid models
125
+ test_files: []