lib/delorean/base.rb in delorean_lang-0.5.1 vs lib/delorean/base.rb in delorean_lang-0.5.2
- old
+ new
@@ -1,14 +1,15 @@
+# frozen_string_literal: true
+
require 'active_support/time'
require 'active_record'
require 'bigdecimal'
require 'delorean/ruby'
require 'delorean/ruby/whitelists/default'
require 'delorean/cache'
module Delorean
-
::Delorean::Ruby.whitelist = ::Delorean::Ruby::Whitelists::Default.new
::Delorean::Cache.adapter = ::Delorean::Cache::Adapters::RubyCache.new(
size_per_class: 1000
)
@@ -20,42 +21,40 @@
def cloned_params
# FIXME: evaluate() modifies params! => need to clone it.
# This is pretty awful. NOTE: can't sanitize params as Marty
# patches NodeCall and modifies params to send _parent_id.
# This whole thing needs to be redone.
- @cp ||= Hash[params]
+ @cloned_params ||= Hash[params]
end
def evaluate(attr)
engine.evaluate(node, attr, cloned_params)
end
def /(args)
- begin
- case args
- when Array
- engine.eval_to_hash(node, args, cloned_params)
- when String
- self.evaluate(args)
- else
- raise "non-array/string arg to /"
- end
- rescue => exc
- Delorean::Engine.grok_runtime_exception(exc)
+ case args
+ when Array
+ engine.eval_to_hash(node, args, cloned_params)
+ when String
+ evaluate(args)
+ else
+ raise 'non-array/string arg to /'
end
+ rescue StandardError => exc
+ Delorean::Engine.grok_runtime_exception(exc)
end
# FIXME: % should also support string as args
def %(args)
- raise "non-array arg to %" unless args.is_a?(Array)
+ raise 'non-array arg to %' unless args.is_a?(Array)
engine.eval_to_hash(node, args, cloned_params)
end
# add new arguments, results in a new NodeCall
def +(args)
- raise "bad arg to %" unless args.is_a?(Hash)
+ raise 'bad arg to %' unless args.is_a?(Hash)
NodeCall.new(_e, engine, node, params.merge(args))
end
end
@@ -70,10 +69,11 @@
if obj.instance_of?(Hash)
# FIXME: this implementation doesn't handle something like
# {}.length. i.e. length is a whitelisted function, but not
# an attr. This implementation returns nil instead of 0.
return obj[attr] if obj.member?(attr)
+
return attr.is_a?(String) ? obj[attr.to_sym] : nil
end
# NOTE: should keep this function consistent with _index
case obj
@@ -89,13 +89,15 @@
return obj.send((attr + POST).to_sym, _e) if obj < BaseClass
end
begin
return _instance_call(obj, attr, [], _e)
- rescue => exc
- raise InvalidGetAttribute,
- "attr lookup failed: '#{attr}' on <#{obj.class}> #{obj} - #{exc}"
+ rescue StandardError => exc
+ raise(
+ InvalidGetAttribute,
+ "attr lookup failed: '#{attr}' on <#{obj.class}> #{obj} - #{exc}"
+ )
end
end
######################################################################
@@ -103,37 +105,38 @@
# NOTE: should keep this function consistent with _get_attr
case obj
when nil
# FIXME: even Javascript which is superpermissive raises an
# exception on null getattr.
- return nil
+ nil
when Hash, NodeCall, Class, OpenStruct
raise InvalidIndex unless args.length == 1
+
_get_attr(obj, args[0], _e)
when Array, String, MatchData
raise InvalidIndex unless args.length <= 2 &&
args[0].is_a?(Integer) &&
(args[1].nil? || args[1].is_a?(Integer))
+
obj[*args]
else
raise InvalidIndex
end
end
######################################################################
def self._sanitize_hash(_e)
- _e.each_with_object({}) do
- |(k,v), h|
+ _e.each_with_object({}) do |(k, v), h|
h[k] = v if k.is_a?(Integer) || k =~ /\A[a-z][A-Za-z0-9_]*\z/
end
end
######################################################################
def self._err(*args)
- str = args.map(&:to_s).join(", ")
+ str = args.map(&:to_s).join(', ')
raise str
end
def self._node_call(node, _e, params)
context = _e[:_engine]
@@ -162,18 +165,18 @@
if obj.is_a?(Class)
_e[:_engine].parse_check_call_fn(method, args.count, obj)
return obj.send(msg, *args)
end
- cls = obj.class
-
matcher = ::Delorean::Ruby.whitelist.matcher(method_name: msg)
raise "no such method #{method}" unless matcher
- return(
- _instance_call(obj, matcher.match_to, args, _e)
- ) if matcher.match_to?
+ if matcher.match_to?
+ return(
+ _instance_call(obj, matcher.match_to, args, _e)
+ )
+ end
matcher.match!(klass: obj.class, args: args)
obj.send(msg, *args)
end