Class: Yequel::Model::Array

Inherits:
Array
  • Object
show all
Defined in:
lib/yequel.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



218
219
220
221
222
# File 'lib/yequel.rb', line 218

def method_missing(method, *args)
  p 'refactoring'
  puts "Method: #{method} Args: (#{args.join(', ')})"
  p self
end

Instance Method Details

#get(key) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/yequel.rb', line 224

def get(key)
  #self.each { |item| p item}
  #p 'getting'
  #p key
  #p self
  result = Array.new
  #self.each { |item| p item[:id]}
  self.each { |item| result<<item if key.to_i==item[:id]}
  result[0]
end

#order(key) ⇒ Object

Order is a query command that orders an array based on a key This command is chainable



237
238
239
240
241
242
243
244
245
246
# File 'lib/yequel.rb', line 237

def order(key)
  newkeys=Hash.new
  self.each { |item| newkeys[item[key].to_s+"|"+item[:id].to_s]=item[:id]}
  #newkeys=newkeys.sort
  #p newkeys.sort
  newarray=Array.new
  #newkeys.sort.each { |ary| p ary}
  newkeys.sort.each { |ary| newarray << self.get(ary[1])} 
  newarray   
end

#page(page, per_page = 10) ⇒ Object

Page is a query command that is used to support will_paginate This command is used at the end of query chain

Examples:

Artist.where('id > 0').page(1, 15)


351
352
353
354
# File 'lib/yequel.rb', line 351

def page(page, per_page=10)
  page = (page || 1).to_i
  self.paginate(:page => page, :per_page => per_page)   
end

#reverseObject



332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/yequel.rb', line 332

def reverse
  if self.length > 0
    result = Array.new
    i = self.length-1
    while i > -1
      result << self[i]
      i = i - 1
    end        
    result
  else
    self
  end
end

#select_map(arg) ⇒ Object

select_map is a method that extracts the values from the array The result is an array for the argument (i.e., hash key) Result is returned in the order of the source array

Examples:

Artist.where('id > 0').select_map(:name)


362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/yequel.rb', line 362

def select_map(arg)
  mapped = Array.new
  if self.length > 0  
    self.each{ |k|            # the each object changes
      if k[arg]
        mapped << k[arg]
      else
        mapped = []           # key is not valid
      end
    }
  end
  mapped    
end

#where(*args) ⇒ Object

Where is a query command that filters the array This command is chainable There are many variations in the selection criteria

Examples:

Artist.where(:name => 'AS')
Artist.where('id > 0')
Artist.where('id <= 2')
Artist.where('id >= 2')
Artist.where('id != 1')
Artist.where('id = ?', 1)
Artist.where(:id => 2).where(:name => 'AS')


260
261
262
263
264
265
266
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
# File 'lib/yequel.rb', line 260

def where(*args)
  #p 'in where'
  #p args.length
  
  flatargs = args[0] 
  if flatargs.class != String     # operations are identities
  #p 'hshing ....'
    hsh = flatargs
    #p hsh
    key = hsh.keys[0]             # required key
    val = hsh.values[0]           # required value
    atype = 1
    atype = 4 if val.class == Array       
  else
    if flatargs.index(" ")
    #p 'splitting ...'
      atype = 2
      phrase = flatargs.split(' ')
      key  = phrase[0].to_sym
      oper = phrase[1]
      val  = phrase[2]
      atype = 3 if oper == '!=' 
      if args.length == 2
        val = args[1]
        atype = 1
      end
      val = val.to_i # if val.match(/\A[+-]?\d+?(\.\d+)?\Z/)        
    end
  end
  
  #p ':::::::::::::::::'
  #p flatargs
  #p phrase
  #p key
  #p oper
  #p val
  
  selected = Array.new    # now run the query
  
  # original = self.dsarray          # this is different
  original = self
  
  original.each { |item|
    item.each {|k, v|
    #p 'querying ...'
      #item[:super_name] = self.name
      selected << item if atype == 1 and key == k and val == v
      selected << item if atype == 3 and key == k and val != v
      if atype == 2 and key == k  # handle quoted expression
        if oper == '>' and v > val
          selected << item
        end
        if oper == '<' and v < val
          selected << item
        end 
        if oper == '<=' and v <= val
          selected << item
        end  
        if oper == '>=' and v >= val
          selected << item
        end       
      end
    }
  }
  
  #p selected
  #p selected.class
  #p '+++++++++++++++++++++'
  selected
   
end