Class: GraphQL::Upgrader::ProcToClassMethodTransform

Inherits:
Transform
  • Object
show all
Defined in:
lib/graphql/upgrader/member.rb

Defined Under Namespace

Classes: NamedProcProcessor

Instance Method Summary collapse

Methods inherited from Transform

#apply_processor, #normalize_type_expression, #reindent_lines, #trim_lines, #underscorize

Constructor Details

#initialize(proc_name) ⇒ ProcToClassMethodTransform

Returns a new instance of ProcToClassMethodTransform.

Parameters:

  • proc_name (String)

    The name of the proc to be moved to def self.#{proc_name}



279
280
281
282
283
# File 'lib/graphql/upgrader/member.rb', line 279

def initialize(proc_name)
  @proc_name = proc_name
  # This will tell us whether to operate on the input or not
  @proc_check_pattern = /#{proc_name}\s?->/
end

Instance Method Details

#apply(input_text) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/graphql/upgrader/member.rb', line 285

def apply(input_text)
  if input_text =~ @proc_check_pattern
    processor = apply_processor(input_text, NamedProcProcessor.new(@proc_name))
    processor.proc_to_method_sections.reverse.each do |proc_to_method_section|
      proc_body = input_text[proc_to_method_section.proc_body_start..proc_to_method_section.proc_body_end]
      method_defn_indent = " " * proc_to_method_section.proc_defn_indent
      method_defn = "def self.#{@proc_name}(#{proc_to_method_section.proc_arg_names.join(", ")})\n#{method_defn_indent}  #{proc_body}\n#{method_defn_indent}end\n"
      method_defn = trim_lines(method_defn)
      # replace the proc with the new method
      input_text[proc_to_method_section.proc_defn_start..proc_to_method_section.proc_defn_end] = method_defn
    end
  end
  input_text
end