Class: DevScripts::Support::ExpandedMethod

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

Direct Known Subclasses

FlattenedMethod

Defined Under Namespace

Classes: NotAMethodError

Constant Summary collapse

INDENT =
'  '.freeze

Instance Method Summary collapse

Constructor Details

#initialize(ast_node:) ⇒ ExpandedMethod

Returns a new instance of ExpandedMethod.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dev_scripts/support/expanded_method.rb', line 7

def initialize(ast_node:)
  @type = ast_node.type
  @children = ast_node.children

  raise NotAMethodError unless [:send, :block].include?(type)

  get_components

  self << string_receiver

  args.each_with_index do |arg, index|
    if index == 0
      self << '('
      self << "\n"
    end

    self << INDENT + string_arg(arg)
    self << "," unless index == args.length - 1
    self << "\n"
    self << ")" if index == args.length - 1
  end

  case type
  when :block
    self << ' do'
    self << "\n"
    self << INDENT + string_block_body
    self << "\n"
    self << 'end'
  end

  self << "\n"
end