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



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
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
# File 'lib/scope.rb', line 74

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|
    puts "$hash..."
    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)}
      puts value.class
      hash[key] = value
    end
    hash
  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('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('$tail'){|*args| args.flatten[1..-1] }
  define('$reverse'){|*args| args.flatten.reverse }

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

end

Instance Method Details

#to_hashObject



163
164
165
166
# File 'lib/scope.rb', line 163

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