lib/scope.rb in expressive-0.0.18 vs lib/scope.rb in expressive-0.0.20

- old
+ new

@@ -86,24 +86,14 @@ else scope[cells.first.text_value] << addition end end - syntax('lookup') do |scope, cells| - lookup_result = scope.lookups[cells.first.text_value] - if lookup_result - key = cells[1] ? cells[1].text_value[1..-2] : nil - if lookup_result.is_a?(Hash) - lookup_result[key] - elsif lookup_result.is_a?(Array) - options = lookup_result.first - the_proc = lookup_result.last - the_proc.call(options, key, *Array(cells[2..-1]).map {|cell| cell.eval(scope)}) - end - end - 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| @@ -136,14 +126,20 @@ 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('if') { |*args| args.compact!; args[0] ? args[1] : args[2] } - define('round'){|*args| args[0].to_f.round(args[1]) } 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 def to_hash h = self.retrieve_scope.dup h.delete_if{|k, v| v.kind_of?(Expressive::Function) || v.kind_of?(Expressive::ExtendedValue)} @@ -151,9 +147,27 @@ private #(post "http://example.com" "*" (headers "AUTH-TOKEN=13415") ) #(post "http://example.com" name email john smith (headers "AUTH-TOKEN=13415") ) + def perform_round(*args) + tuple = args.first + tuple[0].to_f.round(tuple[1]) + end + + def perform_lookup(scope, cells) + lookup_result = scope.lookups[cells.first.text_value] + if lookup_result + key = cells[1] ? cells[1].text_value[1..-2] : nil + if lookup_result.is_a?(Hash) + lookup_result[key] + elsif lookup_result.is_a?(Array) + options = lookup_result.first + the_proc = lookup_result.last + the_proc.call(options, key, *Array(cells[2..-1]).map {|cell| cell.eval(scope)}) + end + end + end def perform_webhook(verb, scope, cells) #convert cells to array so it can manipulated easily cells_array = cells.map{|c| c.text_value.gsub('"', '')} url = cells_array.shift