lib/delorean/engine.rb in delorean_lang-0.0.34 vs lib/delorean/engine.rb in delorean_lang-0.0.38
- old
+ new
@@ -1,12 +1,9 @@
+require 'delorean/const'
require 'delorean/base'
module Delorean
- SIG = "_SIG"
- MOD = "DELOREAN__"
- POST = "__D"
-
class Engine
attr_reader :last_node, :module_name, :line_no, :comp_set, :pm, :m, :imports
def initialize(module_name)
# name of current module
@@ -279,28 +276,40 @@
# Script development/testing
######################################################################
# enumerate all nodes
def enumerate_nodes
- n = SortedSet.new
- @node_attrs.keys.each {|k| n.add(k) }
- n
+ SortedSet[* @node_attrs.keys]
end
# enumerate qualified list of all attrs
def enumerate_attrs
- enumerate_attrs_by_node(nil)
+ @node_attrs.keys.inject({}) { |h, node|
+ h[node] = enumerate_attrs_by_node(node)
+ h
+ }
end
- # enumerate qualified list of attrs by node (or all if nil is passed in)
+ # enumerate qualified list of attrs by node
def enumerate_attrs_by_node(node)
- @node_attrs.keys.inject({}) { |h, n|
- klass = @m.module_eval(n)
- h[n] = klass.methods.map(&:to_s).select {|x| x.end_with?(POST)}.map {|x|
- x.sub(/#{POST}$/, '')
- } if node == n || node.nil?
- h
+ raise "bad node" unless node
+
+ # FIXME: for some reason, in rspec we get nodes which are String
+ # but .is_a?(String) fails.
+ begin
+ klass = node.class.name=="String" ? @m.module_eval(node) : node
+ rescue NameError
+ # FIXME: a little hacky. Should raise an exception.
+ return []
+ end
+
+ raise "bad node class #{klass}" unless klass.is_a?(Class)
+
+ klass.methods.map(&:to_s).select { |x|
+ x.end_with?(POST)
+ }.map { |x|
+ x.sub(/#{POST}$/, '')
}
end
# enumerate all params
def enumerate_params
@@ -308,35 +317,44 @@
end
# enumerate params by a single node
def enumerate_params_by_node(node)
attrs = enumerate_attrs_by_node(node)
- ps = Set.new
- attrs.each_value {|v| v.map {|p| ps.add(p) if @param_set.include?(p.to_s)}}
- ps
+ Set.new( attrs.select {|a| @param_set.include?(a)} )
end
######################################################################
# Runtime
######################################################################
def evaluate(node, attr, params={})
evaluate_attrs(node, [attr], params)[0]
end
+ def evaluate_attrs_hash(node, attrs, params={})
+ res = evaluate_attrs(node, attrs, params)
+ Hash[* attrs.zip(res).flatten(1)]
+ end
+
def evaluate_attrs(node, attrs, params={})
- raise "bad node '#{node}'" unless node =~ /^[A-Z][a-zA-Z0-9_]*$/
+ raise "bad params" unless params.is_a?(Hash)
- begin
- klass = @m.module_eval(node)
- rescue NameError
- err(UndefinedNodeError, "node #{node} is undefined")
+ if node.is_a?(Class)
+ klass = node
+ else
+ raise "bad node '#{node}'" unless node =~ /^[A-Z][a-zA-Z0-9_]*$/
+
+ begin
+ klass = @m.module_eval(node)
+ rescue NameError
+ err(UndefinedNodeError, "node #{node} is undefined")
+ end
end
params[:_engine] = self
- attrs.map {|attr|
+ attrs.map { |attr|
raise "bad attribute '#{attr}'" unless attr =~ /^[a-z][A-Za-z0-9_]*$/
klass.send("#{attr}#{POST}".to_sym, params)
}
end
@@ -353,7 +371,6 @@
end
######################################################################
end
-
end