Class: GraphQL::Upgrader::InterfacesToImplementsTransform

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

Overview

Transform interfaces [A, B, C] to implements A\nimplements B\nimplements C\n

Constant Summary collapse

PATTERN =
/(?<indent>\s*)(?:interfaces) \[\s*(?<interfaces>(?:[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



642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/graphql/upgrader/member.rb', line 642

def apply(input_text)
  input_text.gsub(PATTERN) do
    indent = $~[:indent]
    interfaces = $~[:interfaces].split(',').map(&:strip).reject(&:empty?)
    # Preserve leading newlines before the `interfaces ...`
    # call, but don't re-insert them between `implements` calls.
    extra_leading_newlines = "\n" * (indent[/^\n*/].length - 1)
    indent = indent.sub(/^\n*/m, "")
    interfaces_calls = interfaces
      .map { |interface| "\n#{indent}implements #{interface}" }
      .join
    extra_leading_newlines + interfaces_calls
  end
end