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



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/graphql/upgrader/member.rb', line 296

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



295
296
297
# File 'lib/graphql/upgrader/member.rb', line 295

def proc_arg_names
  @proc_arg_names
end

#proc_body_endObject (readonly)

Returns the value of attribute proc_body_end



295
296
297
# File 'lib/graphql/upgrader/member.rb', line 295

def proc_body_end
  @proc_body_end
end

#proc_body_startObject (readonly)

Returns the value of attribute proc_body_start



295
296
297
# File 'lib/graphql/upgrader/member.rb', line 295

def proc_body_start
  @proc_body_start
end

#proc_defn_endObject (readonly)

Returns the value of attribute proc_defn_end



295
296
297
# File 'lib/graphql/upgrader/member.rb', line 295

def proc_defn_end
  @proc_defn_end
end

#proc_defn_indentObject (readonly)

Returns the value of attribute proc_defn_indent



295
296
297
# File 'lib/graphql/upgrader/member.rb', line 295

def proc_defn_indent
  @proc_defn_indent
end

#proc_defn_startObject (readonly)

Returns the value of attribute proc_defn_start



295
296
297
# File 'lib/graphql/upgrader/member.rb', line 295

def proc_defn_start
  @proc_defn_start
end

Instance Method Details

#on_block(node) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
# File 'lib/graphql/upgrader/member.rb', line 327

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



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/graphql/upgrader/member.rb', line 313

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