Class: LetItGo::WTFParser::MethodAdd

Inherits:
Object
  • Object
show all
Defined in:
lib/let_it_go/wtf_parser.rb

Overview

Holds the “MethodAdd” components of parsed Ripper output Capable of pulling out ‘method_name`, and argument types such as :string_literal

Direct Known Subclasses

CommandCall

Instance Method Summary collapse

Constructor Details

#initialize(ripped_code) ⇒ MethodAdd

Returns a new instance of MethodAdd.



24
25
26
27
28
# File 'lib/let_it_go/wtf_parser.rb', line 24

def initialize(ripped_code)
  ripped_code = ripped_code.dup
  raise "Wrong node" unless ripped_code.shift == :method_add_arg
  @raw = ripped_code
end

Instance Method Details

#arg_typesObject

Returns argument types as an array of symbols [:regexp_literal, :string_literal]



97
98
99
# File 'lib/let_it_go/wtf_parser.rb', line 97

def arg_types
  args.map(&:first).map {|x| x.is_a?(Array) ? x.first : x }.compact
end

#argsObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/let_it_go/wtf_parser.rb', line 79

def args
  args = (args_add_block.first(2).last || [])
  case args.first
  when :args_add_star
    args.shift
    args
  else
    args
  end
end

#args_add_blockObject

[[:regexp_literal, [], [:@regexp_end, “/”, [1, 18]]],

 [[:call,
   [:string_literal,
    [:string_content, [:@tstring_content, "bar", [1, 22]]]],
   :".",
   [:@ident, "gsub!", [1, 27]]],
  [:arg_paren,
   [:args_add_block,
    [[:regexp_literal, [], [:@regexp_end, "/", [1, 34]]],
     [:string_literal,
      [:string_content, [:@tstring_content, "zoo", [1, 38]]]]],
    false]]]],
false]]]


75
76
77
# File 'lib/let_it_go/wtf_parser.rb', line 75

def args_add_block
  args_paren.last || @raw.find {|x| x.first == :args_add_block } || []
end

#args_parenObject

[:arg_paren,

[
  :args_add_block,
  # ..
]


50
51
52
# File 'lib/let_it_go/wtf_parser.rb', line 50

def args_paren
  @raw.find {|x| x.first == :arg_paren } || []
end

#callObject

[

:call,
[:string_literal, [:string_content, [:@tstring_content, "foo", [1, 7]]]],
:".",
[:@ident, "gsub", [1, 12]]

]



36
37
38
# File 'lib/let_it_go/wtf_parser.rb', line 36

def call
  @raw.find {|x| x.first == :call || x.first == :fcall}
end

#method_nameObject

For gsub we want to pull from [:@ident, “gsub”, [1, 12]] from ‘call`



41
42
43
# File 'lib/let_it_go/wtf_parser.rb', line 41

def method_name
  call.find {|x| x.is_a?(Array) && x.first == :@ident }[1]
end