Class: FunctionExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/expressions/function_expression.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function, arguments) ⇒ FunctionExpression

Returns a new instance of FunctionExpression.



7
8
9
10
# File 'lib/expressions/function_expression.rb', line 7

def initialize(function, arguments)
  @function = function
  @arguments = arguments.map { |each| PredicateLiteral.parse(each) }
end

Class Method Details

.parse(array) ⇒ Object



3
4
5
# File 'lib/expressions/function_expression.rb', line 3

def self.parse(array)
  self.new(array[1], array[2..-1])
end

Instance Method Details

#build_arel(arel_table) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/expressions/function_expression.rb', line 12

def build_arel(arel_table)
  arguments = @arguments.map { |each| each.build_arel(arel_table) }
  case @function
    # when "sum:"
    when "count:"
      return arguments.first.count()
    # when "min:"
    # when "max:"
    # when "average:"
    when "first:"
      object = @arguments.first.build_arel(arel_table)
      raise "first:(#{object}) not supported by Arel"
      return object

    when "last:"
      object = @arguments.first.build_arel(arel_table)
      raise "last::(#{object}) not supported by Arel"
      return object

    when "fromObject:index:"
      object = @arguments.first.build_arel(arel_table)
      index = @arguments.second.build_arel (arel_table)
      raise "fromObject:(#{object})index:(#{index}) not supported by Arel"
      return object[index]

    when "add:to:"
      return arguments.reduce(:+)

    when "from:substract:"
      return arguments.reduce(:-)

    when "multiply:by:"
      return arguments.reduce(:*)

    when "divide:by:"
      return arguments.reduce(:/)

    # when "sqrt:"

    when "raise:to:"
      return arguments.reduce(:**)

    # when "abs:"
    # when "now:"
    # when "ln:"
    # when "exp:"
    # when "ceiling:"
    # when "random:"
    # when "modulus:by:"
    # when "chs:"

    else
      raise "Unknown function #{@function} #{arguments}"
  end
end