lib/scope.rb in expressive-0.0.8 vs lib/scope.rb in expressive-0.0.9
- old
+ new
@@ -1,19 +1,48 @@
module Expressive
require 'json'
+
class Scope
+ attr_reader :lookups
def initialize(parent = {})
@parent = parent
@symbols = {}
+ @lookups = {}
end
+ def add_lookup_table(lookup_table_name, lookups)
+ @lookups[lookup_table_name] = {} unless @lookups.include?(lookup_table_name)
+ @lookups[lookup_table_name].merge!(lookups)
+ end
+
+ def add_lookup_function(lookup_function_name, passed_in_options, &func_proc)
+ @lookups[lookup_function_name] = [passed_in_options, func_proc]
+ end
+
+ def clear_lookup_tables
+ @lookups.clear
+ end
+
def [](name)
@symbols[name] || @parent[name]
end
def []=(name, value)
- @symbols[name] = value
+ if value
+ if value.is_a?(ExtendedValue)
+ @symbols[name] = value unless @symbols.include?(name)
+ else
+ current_value = @symbols[name]
+ if current_value and current_value.is_a?(ExtendedValue)
+ current_value.set(value)
+ else
+ @symbols[name] = value
+ end
+ end
+ else
+ @symbols[name] = value
+ end
end
def merge(scope)
@symbols.merge!(scope)
end
@@ -34,46 +63,30 @@
def syntax(name, &block)
self[name] = Syntax.new(&block)
end
end
- class Webhook
-
- def initialize(verb, url, params, headers)
- @verb, @url, @params, @headers = verb, url, params, headers
- end
-
- def execute
- self.send(@verb)
- end
-
- private
-
- def post
- headers = {:content_type => :json, :accept => :json}
- headers = headers.merge(@headers) if @headers
- RestClient.post(@url, @params.to_json, headers)
- end
-
- def get
- headers = {:params => @params}
- headers = headers.merge(@headers) if @headers
- RestClient.get(@url, headers)
- end
-
- def put
- RestClient.put(@url, @params)
- end
-
- end
-
class TopLevel < Scope
def initialize
super
syntax('set') do |scope, cells|
scope[cells.first.text_value] = cells[1].eval(scope)
+ end
+
+ syntax('lookup') do |scope, cells|
+ lookup_result = scope.lookups[cells.first.text_value]
+ if lookup_result
+ key = cells[1].text_value[1..-2]
+ 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)
+ end
+ end
end
syntax('post') do |scope, cells|
perform_webhook(:post, scope, cells)
end