lib/delorean/base.rb in delorean_lang-0.0.34 vs lib/delorean/base.rb in delorean_lang-0.0.38

- old
+ new

@@ -9,11 +9,11 @@ extend Delorean::Functions include Delorean::Functions ###################################################################### - def self._get_attr(obj, attr) + def self._get_attr(obj, attr, _e) return nil if obj.nil? if obj.kind_of? ActiveRecord::Base klass = obj.class @@ -24,28 +24,40 @@ klass.reflect_on_all_associations.map(&:name).member? attr.to_sym raise InvalidGetAttribute, "ActiveRecord lookup '#{attr}' on #{obj}" elsif obj.instance_of?(Hash) return obj.member?(attr) ? obj[attr] : obj[attr.to_sym] + elsif obj.instance_of?(Class) && (obj < BaseClass) + return obj.send((attr + POST).to_sym, _e) end raise InvalidGetAttribute, "bad attribute lookup '#{attr}' on #{obj}" end ###################################################################### - def self._script_call(node_name, mname, _e, attrs, params) - context = _e[:_engine] - node_name ||= self.name.split('::')[-1] + def self._index(obj, args, _e) + if obj.instance_of?(Hash) + raise InvalidIndex unless args.length == 1 + obj[args[0]] + elsif obj.instance_of?(Array) + raise InvalidIndex unless args.length < 2 + raise InvalidIndex unless + args[0].is_a?(Fixnum) && (!args[1] || args[1].is_a?(Fixnum)) + obj[*args] + else + raise InvalidIndex + end + end - engine = mname ? context.get_import_engine(mname) : context + ###################################################################### - res = engine.evaluate_attrs(node_name, attrs, params) + def self._script_call(node, mname, _e, attrs, params) + context = _e[:_engine] + node ||= self - return res[0] if attrs.length == 1 - - # There are more than one attrs, return hash result - Hash[* attrs.zip(res).flatten] + engine = mname ? context.get_import_engine(mname) : context + engine.evaluate_attrs_hash(node, attrs, params) end ###################################################################### end end