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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc_name) ⇒ NamedProcProcessor

Returns a new instance of NamedProcProcessor.



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/graphql/upgrader/member.rb', line 300

def initialize(proc_name)
  @proc_name_sym = proc_name.to_sym
  @proc_arg_names = nil
  # Beginning of the `#{proc_name} -> {...}` call
  @proc_defn_start = nil
  # End of the last `end/}`
  @proc_defn_end = nil
  # Amount of whitespace to insert to the rewritten body
  @proc_defn_indent = nil
  # First statement of the proc
  @proc_body_start = nil
  # End of last statement in the proc
  @proc_body_end = nil
  # Used for identifying the proper block
  @inside_proc = false
end

Instance Attribute Details

#proc_arg_namesObject (readonly)

Returns the value of attribute proc_arg_names.



299
300
301
# File 'lib/graphql/upgrader/member.rb', line 299

def proc_arg_names
  @proc_arg_names
end

#proc_body_endObject (readonly)

Returns the value of attribute proc_body_end.



299
300
301
# File 'lib/graphql/upgrader/member.rb', line 299

def proc_body_end
  @proc_body_end
end

#proc_body_startObject (readonly)

Returns the value of attribute proc_body_start.



299
300
301
# File 'lib/graphql/upgrader/member.rb', line 299

def proc_body_start
  @proc_body_start
end

#proc_defn_endObject (readonly)

Returns the value of attribute proc_defn_end.



299
300
301
# File 'lib/graphql/upgrader/member.rb', line 299

def proc_defn_end
  @proc_defn_end
end

#proc_defn_indentObject (readonly)

Returns the value of attribute proc_defn_indent.



299
300
301
# File 'lib/graphql/upgrader/member.rb', line 299

def proc_defn_indent
  @proc_defn_indent
end

#proc_defn_startObject (readonly)

Returns the value of attribute proc_defn_start.



299
300
301
# File 'lib/graphql/upgrader/member.rb', line 299

def proc_defn_start
  @proc_defn_start
end

Instance Method Details

#on_block(node) ⇒ Object



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

def on_block(node)
  send_node, args_node, body_node = node.children
  _receiver, method_name, _send_args_node = *send_node
  if method_name == :lambda && @inside_proc
    source_exp = body_node.loc.expression
    @proc_arg_names = args_node.children.map { |arg_node| arg_node.children[0].to_s }
    @proc_body_start = source_exp.begin.begin_pos
    @proc_body_end = source_exp.end.end_pos
  end
  super(node)
end

#on_send(node) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/graphql/upgrader/member.rb', line 317

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