Class: Wherewolf::Order::Processor

Inherits:
Processor
  • Object
show all
Defined in:
lib/wherewolf/order/processor.rb

Instance Attribute Summary

Attributes inherited from Processor

#model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#initialize

Constructor Details

This class inherits a constructor from Wherewolf::Processor

Class Method Details

.parse(model, query) ⇒ Object



6
7
8
9
# File 'lib/wherewolf/order/processor.rb', line 6

def self.parse(model, query)
  instance = self.new(model, model.wherewolf_options)
  instance.parse(query)
end

Instance Method Details

#parse(query) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/wherewolf/order/processor.rb', line 11

def parse(query)
  begin
    ast = Wherewolf::Order::Parser.new.parse(query)
    process(ast)
  rescue Parslet::ParseFailed => error
    raise Wherewolf::ParseError, error
  end
end

#process(ast) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wherewolf/order/processor.rb', line 20

def process(ast)
  ast = [ast] unless ast.is_a?(Array)
  table = self.model.arel_table
  model = self.model
  ast.each do |order|
    check_column!(order[:column], table)
    direction = (order[:direction] || 'asc').to_sym
    model = model.order(table[order[:column].to_sym].send(direction))
  end
  model
end