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) (defaults to: "Types::Base\\3")

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



104
105
106
107
108
# File 'lib/graphql/upgrader/member.rb', line 104

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



110
111
112
113
114
115
116
# File 'lib/graphql/upgrader/member.rb', line 110

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