Class: Expressive::TopLevel

Inherits:
Scope
  • Object
show all
Defined in:
lib/scope.rb

Instance Attribute Summary

Attributes inherited from Scope

#lookups

Instance Method Summary collapse

Methods inherited from Scope

#[], #[]=, #add_lookup_function, #add_lookup_table, #clear_lookup_tables, #define, #include?, #merge, #override_scope, #retrieve_scope, #syntax

Constructor Details

#initializeTopLevel

Returns a new instance of TopLevel.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
# File 'lib/scope.rb', line 79

def initialize
  super

  syntax('set') do |scope, cells|
    scope[cells.first.text_value] = cells[1].eval(scope)
  end

  syntax('$append') do |scope, cells|
    scope[cells.first.text_value] = [] if scope[cells.first.text_value].nil?
    addition = cells[1].eval(scope)
    if addition.is_a?(Array)
      scope[cells.first.text_value] = scope[cells.first.text_value] | addition
    else
      scope[cells.first.text_value] << addition
    end
  end

  syntax('$hash') do |scope, cells|
    hash = {}
    cells.each do |cell|
      key = cell.elements[1].elements[0].elements[1].text_value
      value = cell.elements[1].elements[1].elements[1].instance_eval{eval(scope)}
      hash[key] = value
    end
    hash
  end

  syntax('$index') do |scope, cell|
    data = cell[0].eval(scope)
    index = cell[1].eval(scope)
    data[index]
  end

  syntax('$hval') do |scope, cells|
    key = cells[0].eval(scope)
    hash = cells[1].eval(scope)
    hash[key]
  end

  syntax('$lookup') {|scope, cells| perform_lookup(scope, cells)}
  syntax('lookup') {|scope, cells| perform_lookup(scope, cells)}

  syntax('post') do |scope, cells|
    perform_webhook(:post, scope, cells)
  end

  syntax('$concat') do |scope, cells|
    perform_concat(scope, cells)
  end

  syntax('$join') do |scope, cells|
    perform_join(scope, cells)
  end

  syntax('$sms') do |scope, cells|
    perform_sms(scope, cells)
  end

  syntax('$random') do |scope, cells|
    perform_random(scope, cells)
  end

  syntax('$split') do |scope, cells|
    perform_split(scope, cells)
  end

  syntax('put') do |scope, cells|
    perform_webhook(:put, scope, cells)
  end

  syntax('get') do |scope, cells|
    perform_webhook(:get, scope, cells)
  end

  syntax('date') do |scope, cells|
    if cells.empty?
      Date.today.to_time.utc
    else
      date = cells.first.text_value.gsub(/[\)\(]/, '')
      values = date.split('/').map(&:to_i).reverse
      Date.new(values[0], values[1], values[2]).to_time.utc
    end
  end
  syntax('datetime') do |scope, cells|
    if cells.empty?
      DateTime.now
    else
      date = cells.first.text_value.gsub(/[\)\(]/, '')
      DateTime.parse(date)
    end
  end

  define('$id') {|*args| args.first.id }
  define('+') {|a,b| a.to_f + b.to_f }
  define('-') {|a,b| a.to_f - b.to_f }
  define('*') {|a,b| a.to_f * b.to_f }
  define('/') {|a,b| a.to_f / b.to_f }
  define('=') {|a,b| a == b }
  define('>') {|a,b| a.to_f > b.to_f }
  define('<') {|a,b| a.to_f < b.to_f }
  define('>=') {|a,b| a.to_f >= b.to_f }
  define('<=') {|a,b| a.to_f <= b.to_f }
  define('and') {|a,b| !!a && !!b }
  define('or') {|a,b| !!a || !!b }
  define('sum') {|*args| args.flatten.map(&:to_f).reduce(:+) || 0 }
  define('$sub') {|*args| result = args.flatten.map(&:to_f).reduce(:-) || 0 }
  define('if') {|*args| args.compact!; args[0] ? args[1] : args[2] }
  define('$days_ago'){|*args| Time.now - args[0].to_i * 86400 }
  define('$hours_ago'){|*args| Time.now - args[0].to_i * 3600 }
  define('$minutes_ago'){|*args| Time.now - args[0].to_i * 60 }
  define('$head'){|*args| args.flatten.first }
  define('$include'){|val, arr| arr.include?(val) }
  define('$tail'){|*args| args.flatten[1..-1] }
  define('$reverse'){|*args| args.flatten.reverse }
  define('$not'){|*args| not args.first }

  define('$round'){|*args| perform_round(args) }
  define('round'){|*args| perform_round(args) }

end

Instance Method Details

#to_hashObject



200
201
202
203
# File 'lib/scope.rb', line 200

def to_hash
  h = self.retrieve_scope.dup
  h.delete_if{|k, v| v.kind_of?(Expressive::Function) || v.kind_of?(Expressive::ExtendedValue)}
end