lib/raap/symbolic_caller.rb in raap-0.2.0 vs lib/raap/symbolic_caller.rb in raap-0.3.0
- old
+ new
@@ -30,23 +30,35 @@
@name
end
end
attr_reader :symbolic_call
+ attr_reader :allow_private
- def initialize(symbolic_call)
+ def initialize(symbolic_call, allow_private: false)
@symbolic_call = symbolic_call
+ @allow_private = allow_private
end
def eval
walk do |symbolic_call|
eval_one(symbolic_call)
end
end
- def walk(&)
- _walk(@symbolic_call, &)
+ def call_str
+ symbolic_call => [:call, receiver, Symbol => method_name, Array => args, Hash => kwargs, b]
+ receiver = try_eval(receiver)
+ args, kwargs, block = try_eval([args, kwargs, block])
+
+ a = []
+ a << args.map(&:inspect).join(', ') if !args.empty?
+ a << kwargs.map { |k ,v| "#{k}: #{BindCall.inspect(v)}" }.join(', ') if !kwargs.empty?
+ argument_str = a.join(', ')
+ block_str = block ? "{ }" : nil
+
+ "#{BindCall.inspect(receiver)}.#{method_name}(#{argument_str})#{block_str}"
end
def to_lines
[].tap do |lines|
walk do |symbolic_call|
@@ -67,17 +79,11 @@
var = Var.new(var_name(receiver_value))
var_eq = "#{var} = "
receiver = receiver_value.name + '.'
else
var_eq = ""
- receiver = if printable?(receiver_value)
- var = Var.new(printable(receiver_value))
- var + '.'
- else
- var = Var.new(var_name(receiver_value.class))
- var + '.'
- end
+ receiver = Var.new(printable(receiver_value)) + '.'
end
arguments = []
arguments << args.map { |a| printable(a) } if !args.empty?
arguments << kwargs.map{|k,v| "#{k}: #{printable(v)}" }.join(', ') if !kwargs.empty?
@@ -90,10 +96,20 @@
end
end
private
+ def try_eval(symbolic_call)
+ SymbolicCaller.new(symbolic_call).eval
+ rescue RuntimeError, NotImplementedError
+ symbolic_call
+ end
+
+ def walk(&)
+ _walk(@symbolic_call, &)
+ end
+
def _walk(symbolic_call, &block)
return symbolic_call if BindCall::instance_of?(symbolic_call, BasicObject)
return symbolic_call if !BindCall.respond_to?(symbolic_call, :deconstruct) && !BindCall.respond_to?(symbolic_call, :deconstruct_keys)
case symbolic_call
@@ -113,34 +129,42 @@
end
end
def eval_one(symbolic_call)
symbolic_call => [:call, receiver_value, method_name, args, kwargs, block]
- BindCall.public_send(receiver_value, method_name, *args, **kwargs, &block)
+ if @allow_private
+ receiver_value.__send__(method_name, *args, **kwargs, &block)
+ else
+ BindCall.public_send(receiver_value, method_name, *args, **kwargs, &block)
+ end
end
def var_name(mod)
printable(mod).gsub('::', '_').downcase
end
- def printable?(obj)
- case obj
- when Symbol, Integer, Float, Regexp, nil, true, false, String, Module, Var
- true
- else
- false
+ def printable(obj)
+ if obj in [:call, _, Symbol, Array, Hash, _]
+ return _walk(obj)
end
- end
- def printable(obj)
case obj
when Var
obj.name
# Object from which it can get strings that can be eval with `#inspect`
when Symbol, Integer, Float, Regexp, nil, true, false
obj.inspect
when String
obj.inspect.gsub('"', "'") or raise
+ when Hash
+ hash_body = obj.map do |k, v|
+ ks = printable(k)
+ vs = printable(v)
+ ks.start_with?(':') ? "#{ks[1..-1]}: #{vs}" : "#{ks} => #{vs}"
+ end
+ "{#{hash_body.join(', ')}}"
+ when Array
+ "[#{obj.map { |o| printable(o) }.join(', ')}]"
when Module
BindCall.name(obj) or raise
else
var_name(obj.class)
end