Class: Function

Inherits:
Object
  • Object
show all
Defined in:
lib/yodel/models/core/functions/function.rb

Constant Summary collapse

CALL_TOKEN =

Compilation


'.'
START_PARAMS_TOKEN =
'('
END_PARAMS_TOKEN =
')'
START_HASH_TOKEN =
'{'
END_HASH_TOKEN =
'}'
PARAM_DELIM_TOKEN =
','
HASH_DELIM_TOKEN =
':'
ENTRY_FLAG =
'!'
DOUBLE_QUOTE_TOKEN =
'"'
SINGLE_QUOTE_TOKEN =
"'"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param) ⇒ Function

Returns a new instance of Function.



4
5
6
7
8
9
10
11
# File 'lib/yodel/models/core/functions/function.rb', line 4

def initialize(param)
  if param.is_a?(String)
    @source = param
    @instructions = compile(param)
  else
    @instructions = param
  end
end

Instance Attribute Details

#instructionsObject

Returns the value of attribute instructions.



2
3
4
# File 'lib/yodel/models/core/functions/function.rb', line 2

def instructions
  @instructions
end

#sourceObject

Returns the value of attribute source.



2
3
4
# File 'lib/yodel/models/core/functions/function.rb', line 2

def source
  @source
end

Instance Method Details

#compile(source) ⇒ Object



43
44
45
46
# File 'lib/yodel/models/core/functions/function.rb', line 43

def compile(source)
  tokens = source.scan(/[\w\-\+]+|\.|\(|\)|\{|\}|:|,|"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/)
  parse(tokens)
end

#execute(context, instruction = nil, parent_context = nil) ⇒ Object


Execution




107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/yodel/models/core/functions/function.rb', line 107

def execute(context, instruction=nil, parent_context=nil)
  instruction ||= self.instructions.first
  name, *params = instruction
  parent_context ||= context

  case name
  when 'chain'
    chain(context, parent_context, params)
  when 'field'
    get_field(context, parent_context, params.first)
  when 'find'
    find_record(context, parent_context, params[0], params[1])
  when 'changed'
    changed(context, parent_context, params.first)
  when 'previous_value'
    previous_value(context, parent_context, params.first)
  when 'collect'
    collect(context, parent_context, params.first)
  when 'flatten'
    flatten(context)
  when 'majority'
    majority(context, parent_context, params.first)
  when 'count'
    count(context, parent_context, params.first)
  when 'invert'
    invert(context)
  when 'unique'
    unique(context, parent_context, params.first)
  when 'average'
    average(context, parent_context, params)
  when 'as_a_percentage_of'
    as_a_percentage_of(context, parent_context, params.first)
  when 'present'
    present(context)
  when 'blank'
    blank(context)
  when 'sum'
    sum(context, parent_context, params)
  when 'subtract'
    subtract(context, parent_context, params)
  when 'multiply'
    multiply(context, parent_context, params)
  when 'round'
    round(context)
  when 'if'
    binary_if(context, parent_context, params[0], params[1], params[2])
  when 'greater_than'
    greater_than(context, parent_context, params.first)
  when 'greater_than_or_equal_to'
    greater_than_or_equal_to(context, parent_context, params.first)
  when 'less_than'
    less_than(context, parent_context, params.first)
  when 'less_than_or_equal_to'
    less_than_or_equal_to(context, parent_context, params.first)
  when 'not_equal'
    not_equal(context, parent_context, params.first)
  when 'and'
    binary_and(context, parent_context, params[0], params[1])
  when 'or'
    binary_or(context, parent_context, params[0], params[1])
  when 'include'
    set_include(context, parent_context, params.first)
  when 'strip'
    strip(context)
  when 'format'
    format(context, parent_context, params.first)
  when 'set'
    set_field(context, parent_context, params[0], params[1])
  when 'update'
    update_field(context, parent_context, params[0], params[1])
  when 'min'
    min(context, parent_context, params[0], params[1])
  when 'max'
    max(context, parent_context, params[0], params[1])
  when 'increment'
    increment(context, parent_context, params[0], params[1])
  when 'complement'
    complement(context, parent_context, params[0], params[1])
  when 'each'
    each(context, parent_context, params.first)
  when 'deliver'
    deliver_email(context, parent_context, params[0], params[1])
  when 'call_api'
    call_api(context, parent_context, params[0], params[1])
    
  # literals
  when 'string'
    params.first
  when 'int'
    params.first.to_i
  when 'hash'
    Hash[params.collect {|entry| execute(context, entry, parent_context)}]
  when 'entry'
    [execute(context, params.first, parent_context), execute(context, params.last, parent_context)]
  end
end

#inspect(instruction = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/yodel/models/core/functions/function.rb', line 13

def inspect(instruction=nil)
  instruction ||= self.instructions
  
  name = instruction.shift
  parameters = instruction.collect do |parameter|
    if parameter.is_a?(Array)
      inspect(parameter)
    else
      parameter
    end
  end

  "#{name}(#{parameters.join(', ')})"
end

#parse(tokens) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/yodel/models/core/functions/function.rb', line 48

def parse(tokens)
  params = false
  chain  = false
  hash   = false
  instructions = []

  until tokens.empty?
    token = tokens.shift
    case token[0]
    when CALL_TOKEN
      chain = true
    when START_PARAMS_TOKEN
      params = true
      instructions += parse(tokens)
    when END_PARAMS_TOKEN
      tokens.unshift(END_PARAMS_TOKEN) unless params
      break
    when ENTRY_FLAG
      hash = true
      instructions << ['entry'] + parse(tokens)
    when START_HASH_TOKEN
      tokens.unshift(ENTRY_FLAG)
      instructions << ['hash'] + parse(tokens)
    when END_HASH_TOKEN
      tokens.unshift(END_HASH_TOKEN) if hash
      break
    when HASH_DELIM_TOKEN
      instructions += parse(tokens)
    when PARAM_DELIM_TOKEN
      if params || hash
        tokens.unshift(ENTRY_FLAG) if hash
        instructions += parse(tokens)
      else
        tokens.unshift(PARAM_DELIM_TOKEN)
        break
      end
    when DOUBLE_QUOTE_TOKEN, SINGLE_QUOTE_TOKEN
      instructions << ['string', token[1...-1]]
    else
      if tokens.first == START_PARAMS_TOKEN
        instructions << [token] + parse(tokens)
      elsif token.to_i.to_s == token
        instructions << ['int', token]
      else
        instructions << ['field', token]
      end
    end
  end

  if chain
    [['chain'] + instructions]
  else
    instructions
  end
end