Module: Plant::Query

Defined in:
lib/query.rb

Constant Summary collapse

@@wheres =
nil

Class Method Summary collapse

Class Method Details

.find_all(klass) ⇒ Object



14
15
16
# File 'lib/query.rb', line 14

def self.find_all klass
  Plant.all[klass] || []
end

.find_by_ids(klass, ids) ⇒ Object



18
19
20
21
22
# File 'lib/query.rb', line 18

def self.find_by_ids klass, ids
  plants = Plant.all[klass].select{|plant| ids.include?(plant.id)}
  return plants.first if plants.size == 1
  plants
end

.limit(objects, limit_value) ⇒ Object



33
34
35
36
37
# File 'lib/query.rb', line 33

def self.limit objects, limit_value
  @@objects = objects
  @@limit = limit_value
  objects.first(limit_value)
end

.offset(objects, offset_value) ⇒ Object



39
40
41
# File 'lib/query.rb', line 39

def self.offset objects, offset_value
  @@objects[offset_value..-1].first(@@limit)
end

.order(objects, args) ⇒ Object



28
29
30
31
# File 'lib/query.rb', line 28

def self.order objects, args
  attribute, order = args.split(" ")
  objects.sort {|x, y| (x,y = y,x if order == 'desc'); x.send(attribute.to_sym) <=> y.send(attribute.to_sym)}
end

.select(klass) ⇒ Object



24
25
26
# File 'lib/query.rb', line 24

def self.select klass
  Plant::Selector.new(:klass => klass, :wheres => @@wheres, :plants => Plant.all[klass].to_a).select
end

.wheresObject



10
11
12
# File 'lib/query.rb', line 10

def self.wheres
  @@wheres
end

.wheres=(wheres) ⇒ Object



6
7
8
# File 'lib/query.rb', line 6

def self.wheres= wheres
  @@wheres = wheres
end