Class: DevScripts::Support::MethodCall
- Inherits:
-
String
- Object
- String
- DevScripts::Support::MethodCall
- Defined in:
- lib/dev_scripts/support/method_call.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#ast_node ⇒ Object
readonly
Returns the value of attribute ast_node.
-
#invocation ⇒ Object
readonly
Returns the value of attribute invocation.
-
#receiver ⇒ Object
readonly
Returns the value of attribute receiver.
Instance Method Summary collapse
- #arg_string(arg) ⇒ Object
-
#initialize(ast_node) ⇒ MethodCall
constructor
A new instance of MethodCall.
- #invocation_with_args ⇒ Object
Constructor Details
#initialize(ast_node) ⇒ MethodCall
Returns a new instance of MethodCall.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dev_scripts/support/method_call.rb', line 6 def initialize(ast_node) @ast_node = ast_node current_receiver, @invocation, *@args = ast_node.children invocation_stack = [invocation_with_args] while Parser::AST::Node === current_receiver invocation_stack.unshift MethodCall.new(current_receiver).invocation_with_args current_receiver, invocation, *args = current_receiver&.children || [] end invocation_stack.each_with_index do |invocation_string, index| self << '.' if index > 0 self << invocation_string.to_s end end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/dev_scripts/support/method_call.rb', line 4 def args @args end |
#ast_node ⇒ Object (readonly)
Returns the value of attribute ast_node.
4 5 6 |
# File 'lib/dev_scripts/support/method_call.rb', line 4 def ast_node @ast_node end |
#invocation ⇒ Object (readonly)
Returns the value of attribute invocation.
4 5 6 |
# File 'lib/dev_scripts/support/method_call.rb', line 4 def invocation @invocation end |
#receiver ⇒ Object (readonly)
Returns the value of attribute receiver.
4 5 6 |
# File 'lib/dev_scripts/support/method_call.rb', line 4 def receiver @receiver end |
Instance Method Details
#arg_string(arg) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dev_scripts/support/method_call.rb', line 37 def arg_string(arg) case arg.type when :sym ":#{arg.children[0]}" when :str "'#{arg.children[0]}'" when :int "#{arg.children[0].to_i}" when :lvar "#{arg.children[0]}" when :send "#{arg.children[1]}" when :hash arg.children.each_with_object('').with_index do |(kwarg, string), index| string << arg_string(kwarg) string << ', ' if index < arg.children.size - 1 end when :pair key, value = arg.children "#{key.children.first}: " + arg_string(value) end end |
#invocation_with_args ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dev_scripts/support/method_call.rb', line 24 def invocation_with_args if args && args.size > 0 args.each_with_object("#{invocation}(").with_index do |(arg, string), index| string << arg_string(arg) string << ', ' if index < args.size - 1 string << ')' if index == args.size - 1 end else invocation end end |