Class: GraphQL::Upgrader::ProcToClassMethodTransform::NamedProcProcessor

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
lib/graphql/upgrader/member.rb

Defined Under Namespace

Classes: ProcToMethodSection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc_name) ⇒ NamedProcProcessor

Returns a new instance of NamedProcProcessor



302
303
304
305
# File 'lib/graphql/upgrader/member.rb', line 302

def initialize(proc_name)
  @proc_name_sym = proc_name.to_sym
  @proc_to_method_sections = []
end

Instance Attribute Details

#proc_to_method_sectionsObject (readonly)

Returns the value of attribute proc_to_method_sections



301
302
303
# File 'lib/graphql/upgrader/member.rb', line 301

def proc_to_method_sections
  @proc_to_method_sections
end

Instance Method Details

#on_block(node) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/graphql/upgrader/member.rb', line 345

def on_block(node)
  send_node, args_node, body_node = node.children
  _receiver, method_name, _send_args_node = *send_node
  if method_name == :lambda && !@proc_to_method_sections.empty? && @proc_to_method_sections[-1].inside_proc
    proc_to_method_section = @proc_to_method_sections[-1]

    source_exp = body_node.loc.expression
    proc_to_method_section.proc_arg_names = args_node.children.map { |arg_node| arg_node.children[0].to_s }
    proc_to_method_section.proc_body_start = source_exp.begin.begin_pos
    proc_to_method_section.proc_body_end = source_exp.end.end_pos
    proc_to_method_section.inside_proc = false
  end
  super(node)
end

#on_send(node) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/graphql/upgrader/member.rb', line 328

def on_send(node)
  receiver, method_name, _args = *node
  if method_name == @proc_name_sym && receiver.nil?
    proc_section = ProcToMethodSection.new
    source_exp = node.loc.expression
    proc_section.proc_defn_start = source_exp.begin.begin_pos
    proc_section.proc_defn_end = source_exp.end.end_pos
    proc_section.proc_defn_indent = source_exp.column
    proc_section.inside_proc = true

    @proc_to_method_sections << proc_section
  end
  res = super(node)
  @inside_proc = false
  res
end