Module: Mongoid::DSL::Document::Extend

Defined in:
lib/mongoid-dsl/monkey.rb

Instance Method Summary collapse

Instance Method Details

#__query_wrapper(*args) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/mongoid-dsl/monkey.rb', line 267

def __query_wrapper(*args)

  # defaults
  begin
    return_data= nil
    # params field
    field_hash= Hash.new
    # models
    models_container= Array.new
    # mother model
    mother_model= nil
    # method_to_use
    method_to_use= Hash.new

    args.each do |one_argument|
      case true

        when one_argument.class <= String
          begin
            field_hash['_id']= Moped::BSON::ObjectId.from_string(one_argument)
          rescue
            begin
              models_container.push one_argument.constantize
            rescue NameError
              #method_to_use= one_argument
            end
          end

        when one_argument.class <= Hash
          begin
            one_argument.each do |key,value|
              if key.to_s == '_id'
                field_hash['_id']= Moped::BSON::ObjectId.from_string(value.to_s)
              else
                field_hash[key]=value
              end
            end
          end

        when one_argument.class <= Class
          models_container.push one_argument

        when one_argument.class <= Array
          method_to_use= one_argument

      end
    end

  end

  # mother model find, and path generate
  begin

    full_path   = self._get_class_path(*models_container)  || Array.new.push(self)
    mother_model= full_path.shift

    full_path.count.times do |index|
      full_path[index]= full_path[index].convert_model_name
    end

  end

  # path trim
  begin

    deep_level = (full_path.count) || 0
    chains = full_path

    if !full_path.nil? && !full_path.empty?
      full_path= full_path.join('.')+'.'
    else
      full_path= String.new
    end

  end

  # start query
  begin

    # build app query args
    begin

      gen_field_hash= lambda { |query_hash,*pre_path_elements|
        query_fields= Hash.new()

        full_path_str= nil
        if !pre_path_elements.empty? && !pre_path_elements.nil?
          full_path_str= pre_path_elements.join('.')+'.'
        else
          full_path_str= ""
        end

        return query_hash.map_hash { |key,value|
          { "#{full_path_str}#{key}" => value }
        }


      }

      query_fields= gen_field_hash.call field_hash, *chains

    end

    # do query ask from db
    begin
      if method_to_use[1][:arguments?]
        query_data = mother_model.__send__(
            method_to_use[0].to_s,
            query_fields)
      else
        query_data = mother_model.__send__(
            method_to_use[0].to_s)
      end
    end

    # return data level
    begin

      if deep_level == 0
        return_data=query_data
      else

        # go down for embeds docs
        begin

          last_round   = (chains.count-1)
          map_object   = query_data
          return_array = [] #> Mongoid::Criteria.new
          chains.count.times do |deepness|
            begin
              children = Array.new
              if map_object.class == Array || map_object.class == Mongoid::Criteria

                map_object.each do |one_element_of_the_map_array|
                  subclass_data = one_element_of_the_map_array.__send__(chains[deepness])
                  if subclass_data.class <= Array || subclass_data.class <= Mongoid::Criteria
                    subclass_data.each do |one_element_of_tmp_children|
                      if deepness < last_round
                        children.push one_element_of_tmp_children
                      else
                        return_array.push one_element_of_tmp_children
                      end
                    end
                  else
                    if deepness < last_round
                      children.push subclass_data
                    else
                      return_array.push subclass_data
                    end
                  end
                end

              else
                subclass_data = map_object.__send__(chains[deepness])
                if subclass_data.class == Array || subclass_data.class == Mongoid::Criteria
                  subclass_data.each do |one_element_of_tmp_children|
                    if deepness < last_round
                      children.push one_element_of_tmp_children
                    else
                      return_array.push one_element_of_tmp_children
                    end
                  end
                else
                  if deepness < last_round
                    children.push subclass_data
                  else
                    return_array.push subclass_data
                  end
                end
              end
              map_object=children.uniq
            rescue NoMethodError => ex
              STDERR.puts(ex)
            end
          end

          return_data= return_array

        end
      end
    end

    # trim not used class
    if method_to_use[1][:arguments?]

      tmp_array= []
      return_data.map! { |one_element|

        tmp_hash= one_element.convert_to_hash#instance_variables.each{ |var| tmp_hash[var.to_s.delete("@")] = one_element.instance_variable_get(var) }

        if tmp_hash["attributes"].deep_include?(field_hash) || tmp_hash["attributes"].deep_include?(field_hash.map_hash{|k,v|[k.to_s,v]})
          one_element
        else
          nil
        end

      }.compact!

    end


  end

  return return_data

end

#_all(*args) ⇒ Object



477
478
479
# File 'lib/mongoid-dsl/monkey.rb', line 477

def _all(*args)
  self.__query_wrapper(['all',{:arguments? => false}],*args)
end

#_find(target_id) ⇒ Object



481
482
483
484
485
# File 'lib/mongoid-dsl/monkey.rb', line 481

def _find(target_id)
  return self._where(
      '_id' => Moped::BSON::ObjectId.from_string(target_id.to_s)
  )[0]
end

#_find_by(*args) ⇒ Object



486
487
488
# File 'lib/mongoid-dsl/monkey.rb', line 486

def _find_by(*args)
  return self._where(*args).first
end

#_get_class_path(*included) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/mongoid-dsl/monkey.rb', line 123

def _get_class_path(*included)

  # create defaults
  begin
    return_array= Array.new()
    check_list= Array.new#.push(self.to_s)
    relations_hash = Hash.new()
    #chains= Hash.new
    chains= Array.new
  end

  # get parents for every participants
  begin
    models_to_check = Array.new.push self
    loop do
      tmp_array = Array.new
      break if models_to_check.empty?
      models_to_check.each do |one_models_element|
        begin
          one_models_element.parents.each do |one_parent_in_underscore|
            parent_model= one_parent_in_underscore.convert_model_name
            tmp_array.push parent_model if !relations_hash.keys.include?(parent_model)
            relations_hash[one_models_element] ||= Array.new
            relations_hash[one_models_element].push parent_model
          end
        rescue NoMethodError
          #next
        end
      end
      models_to_check= tmp_array
    end
    models_to_check.clear
  end

  # make connections from relation pairs
  begin

    # generate pre path chains
    # minden egyes szulo,s szulo szulo elemen egyesevel menj vegig (csak elso elemet vizsgalva) es ahol tobb mint egy elem talalhato azt torold
    # igy legkozelebb az mar nem lesz lehetseges utvonal, ahol pedig mindenhol csak 1 elem volt  ott ellenorzid
    # 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)
    # the goal is to get every 1 element from sub arrays

    begin
      available_paths= Array.new
      return nil if relations_hash[self].nil?
      loop do

        # defaults
        begin
          next_element_to_find  = self
          delete_element        = Hash.new
          one_chain             = Array.new
        end

        # chain element builder
        begin
          loop do
            if !relations_hash[next_element_to_find].nil? && relations_hash[next_element_to_find] != Array.new

              one_chain.push(relations_hash[next_element_to_find][0])
              if relations_hash[next_element_to_find].count > 1
                delete_element       = Hash.new
                delete_element[next_element_to_find]= relations_hash[next_element_to_find][0]
              end

              next_element_to_find= relations_hash[next_element_to_find][0]
            else
              break
            end
          end
        end

        # remove already checked tree
        begin
          if delete_element != Hash.new
            relations_hash[delete_element.keys[0]].delete_at(
                relations_hash[delete_element.keys[0]].index(
                    delete_element[delete_element.keys[0]]))
            delete_element= Hash.new
          end
        end

        # add new element to chains
        begin
          unless chains.include? one_chain
            chains.push one_chain
          else
            break
          end
        end
      end

    end

  end

  # after format and check params for contains
  begin

    # pre chains trim
    begin

      tmp_array= Array.new
      chains.each do |one_element|
        if !one_element.contain?(included)
          tmp_array.push one_element
        end
      end
      tmp_array.each do |one_element_to_delete|
        chains.delete_at(chains.index(one_element_to_delete))
      end

    end

    # choose the shortest path
    begin
      chain_list_max_count= nil
      chains.each do |one_chain_list|
        counter= one_chain_list.count
        chain_list_max_count ||= (counter+1)
        if counter < chain_list_max_count
          return_array= one_chain_list
          counter= chain_list_max_count
        end
      end
    end

    # reverse array
    begin
      return_array.reverse!
    end

    # add new element as self for first
    begin
      return_array.push self
    end


  end

  return return_array
end

#_where(*args) ⇒ Object



474
475
476
# File 'lib/mongoid-dsl/monkey.rb', line 474

def _where(*args)
  self.__query_wrapper(['where',{:arguments? => true}],*args)
end

#documentsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongoid-dsl/monkey.rb', line 8

def documents

      exceptions= %w[
    Mongoid::Relations::Embedded::In
    Mongoid::Relations::Referenced::In
  ]

  return_array = Array.new
  self.relations.each do |model_name,module_propertys|
    if !exceptions.include?(module_propertys[:relation].to_s)
      return_array.push model_name
    end
  end

  return return_array
rescue Exception
  return []

end

#parentsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid-dsl/monkey.rb', line 87

def parents
  begin

    exceptions= %w[
  Mongoid::Relations::Embedded::In
]

    return_array = Array.new
    self.relations.each do |model_name,module_propertys|
      if exceptions.include?(module_propertys[:relation].to_s)
        return_array.push model_name
      end
    end
    return return_array

  end
end

#propertiesObject



77
78
79
80
81
82
83
84
85
# File 'lib/mongoid-dsl/monkey.rb', line 77

def properties

  hash_data = Hash.new
  self.fields.each do |key,value|
    hash_data[value.name]=value.options[:type]
  end
  return hash_data

end

#referencesObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mongoid-dsl/monkey.rb', line 104

def references
  begin

    exceptions= %w[
  Mongoid::Relations::Referenced::In
  Mongoid::Relations::Referenced::ManyToMany
]

    return_array = Array.new
    self.relations.each do |model_name,module_propertys|
      if exceptions.include?(module_propertys[:relation].to_s)
        return_array.push model_name
      end
    end
    return return_array

  end
end

#relation_connection_type(to_model) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongoid-dsl/monkey.rb', line 28

def relation_connection_type(to_model)
  begin

    return_none= "Mongoid::Relations::None"
    return_self= "Mongoid::Relations::Self"

    if to_model.nil?
      return return_none
    end

    if self.to_s == to_model.to_s
      return return_self
    end

    relation_type_data= self.reflect_on_association(to_model.convert_model_name)

    if relation_type_data.nil?
      return return_none
    else
      return relation_type_data[:relation].to_s
    end

  end
end

#relation_from?(klass) ⇒ Boolean

> [:none,:self,:one,:many]

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


497
498
499
500
# File 'lib/mongoid-dsl/monkey.rb', line 497

def relation_from?(klass)
  raise(ArgumentError,"obj must be class: #{klass}") unless klass <= Class
  return self.reverse_relation_conn_type(klass).to_s.downcase.split('::').last.to_sym
end

#relation_to?(klass) ⇒ Boolean

> [:none,:self,:one,:many]

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


491
492
493
494
# File 'lib/mongoid-dsl/monkey.rb', line 491

def relation_to?(klass)
  raise(ArgumentError,"obj must be class: #{klass}") unless klass <= Class
  return self.relation_connection_type(klass).to_s.downcase.split('::').last.to_sym
end

#reverse_relation_conn_type(from_model) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mongoid-dsl/monkey.rb', line 52

def reverse_relation_conn_type(from_model)
  begin

    return_none= "Mongoid::Relations::None"
    return_self= "Mongoid::Relations::Self"

    if from_model.nil?
      return return_none
    end

    if self.to_s == from_model.to_s
      return return_self
    end

    relation_type_data= from_model.reflect_on_association(self.convert_model_name)

    if relation_type_data.nil?
      return return_none
    else
      return relation_type_data[:relation].to_s
    end

  end
end