Class: GraphQL::Upgrader::TypeDefineToClassTransform

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

Overview

Turns {X} = GraphQL::{Y}Type.define do into class {X} < Types::Base{Y}.

Instance Method Summary collapse

Methods inherited from Transform

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

Constructor Details

#initialize(base_class_pattern: "Types::Base\\3") ⇒ TypeDefineToClassTransform

Returns a new instance of TypeDefineToClassTransform

Parameters:

  • base_class_pattern (String)

    Replacement pattern for the base class name. Use this if your base classes have nonstandard names.



100
101
102
103
104
# File 'lib/graphql/upgrader/member.rb', line 100

def initialize(base_class_pattern: "Types::Base\\3")
  @find_pattern = /( *)([a-zA-Z_0-9:]*) = GraphQL::#{GRAPHQL_TYPES}Type\.define do/
  @replace_pattern = "\\1class \\2 < #{base_class_pattern}"
  @interface_replace_pattern = "\\1module \\2\n\\1  include #{base_class_pattern}"
end

Instance Method Details

#apply(input_text) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/graphql/upgrader/member.rb', line 106

def apply(input_text)
  if input_text.include?("GraphQL::InterfaceType.define")
    input_text.sub(@find_pattern, @interface_replace_pattern)
  else
    input_text.sub(@find_pattern, @replace_pattern)
  end
end