Class: GraphQL::Upgrader::PossibleTypesTransform

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

Overview

Transform possible_types [A, B, C] to possible_types(A, B, C)

Constant Summary

PATTERN =
/(?<indent>\s*)(?:possible_types) \[\s*(?<possible_types>(?:[a-zA-Z_0-9:\.,\s]+))\]/m

Instance Method Summary collapse

Methods inherited from Transform

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

Instance Method Details

#apply(input_text) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
# File 'lib/graphql/upgrader/member.rb', line 624

def apply(input_text)
  input_text.gsub(PATTERN) do
    indent = $~[:indent]
    possible_types = $~[:possible_types].split(',').map(&:strip).reject(&:empty?)
    extra_leading_newlines = indent[/^\n*/]
    method_indent = indent.sub(/^\n*/m, "")
    type_indent = "  " + method_indent
    possible_types_call = "#{method_indent}possible_types(\n#{possible_types.map { |t| "#{type_indent}#{t},"}.join("\n")}\n#{method_indent})"
    extra_leading_newlines + trim_lines(possible_types_call)
  end
end