class Yequel::Model::Array
Public Instance Methods
get(key)
click to toggle source
# 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
method_missing(method, *args)
click to toggle source
# File lib/yequel.rb, line 218 def method_missing(method, *args) p 'refactoring' puts "Method: #{method} Args: (#{args.join(', ')})" p self end
order(key)
click to toggle source
Order is a query command that orders an array based on a key This command is chainable
# 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)
click to toggle source
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)
# 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
reverse()
click to toggle source
# 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)
click to toggle source
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)
# 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)
click to toggle source
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')
# 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