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 collapse

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



661
662
663
664
665
666
667
668
669
670
671
# File 'lib/graphql/upgrader/member.rb', line 661

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