Class: DevScripts::Support::MethodCall

Inherits:
String
  • Object
show all
Defined in:
lib/dev_scripts/support/method_call.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject (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_nodeObject (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

#invocationObject (readonly)

Returns the value of attribute invocation.



4
5
6
# File 'lib/dev_scripts/support/method_call.rb', line 4

def invocation
  @invocation
end

#receiverObject (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_argsObject



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