Class: GraphQL::Upgrader::Transform
- Inherits:
-
Object
- Object
- GraphQL::Upgrader::Transform
- Defined in:
- lib/graphql/upgrader/member.rb
Direct Known Subclasses
ConfigurationToKwargTransform, InterfacesToImplementsTransform, MutationDefineToClassTransform, MutationResolveProcToMethodTransform, NameTransform, PositionalTypeArgTransform, PossibleTypesTransform, ProcToClassMethodTransform, PropertyToMethodTransform, RemoveEmptyBlocksTransform, RemoveExcessWhitespaceTransform, RemoveMethodParensTransform, RemoveRedundantKwargTransform, ResolveProcToMethodTransform, TypeDefineToClassTransform, UnderscoreizeFieldNameTransform, UnderscorizeMutationHashTransform, UpdateMethodSignatureTransform
Instance Method Summary collapse
-
#apply(input_text) ⇒ String
The input text, with a transformation applied if necessary.
-
#apply_processor(input_text, processor) ⇒ Object
-
#normalize_type_expression(type_expr, preserve_bang: false) ⇒ String
Recursively transform a
.define
-DSL-based type expression into a class-ready expression, for example:. -
#reindent_lines(input_text, from_indent:, to_indent:) ⇒ Object
-
#trim_lines(input_text) ⇒ Object
Remove trailing whitespace.
-
#underscorize(str) ⇒ Object
Instance Method Details
#apply(input_text) ⇒ String
Returns The input text, with a transformation applied if necessary.
15 16 17 |
# File 'lib/graphql/upgrader/member.rb', line 15 def apply(input_text) raise GraphQL::RequiredImplementationMissingError, "Return transformed text here" end |
#apply_processor(input_text, processor) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/graphql/upgrader/member.rb', line 74 def apply_processor(input_text, processor) ruby_ast = Parser::CurrentRuby.parse(input_text) processor.process(ruby_ast) processor rescue Parser::SyntaxError puts "Error text:" puts input_text raise end |
#normalize_type_expression(type_expr, preserve_bang: false) ⇒ String
Recursively transform a .define
-DSL-based type expression into a class-ready expression, for example:
types[X]
->[X, null: true]
types[X.to_non_null_type]
->[X]
Int
->Integer
X!
->X
Notice that !
is removed sometimes, because it doesn’t exist in the class API.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/graphql/upgrader/member.rb', line 30 def normalize_type_expression(type_expr, preserve_bang: false) case type_expr when /\A!/ # Handle the bang, normalize the inside "#{preserve_bang ? "!" : ""}#{normalize_type_expression(type_expr[1..-1], preserve_bang: preserve_bang)}" when /\Atypes\[.*\]\Z/ # Unwrap the brackets, normalize, then re-wrap inner_type = type_expr[6..-2] if inner_type.start_with?("!") nullable = false inner_type = inner_type[1..-1] elsif inner_type.end_with?(".to_non_null_type") nullable = false inner_type = inner_type[0...-17] else nullable = true end "[#{normalize_type_expression(inner_type, preserve_bang: preserve_bang)}#{nullable ? ", null: true" : ""}]" when /\Atypes\./ # Remove the prefix normalize_type_expression(type_expr[6..-1], preserve_bang: preserve_bang) when /\A->/ # Remove the proc wrapper, don't re-apply it # because stabby is not supported in class-based definition # (and shouldn't ever be necessary) unwrapped = type_expr .sub(/\A->\s?\{\s*/, "") .sub(/\s*\}/, "") normalize_type_expression(unwrapped, preserve_bang: preserve_bang) when "Int" "Integer" else type_expr end end |
#reindent_lines(input_text, from_indent:, to_indent:) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/graphql/upgrader/member.rb', line 84 def reindent_lines(input_text, from_indent:, to_indent:) prev_indent = " " * from_indent next_indent = " " * to_indent # For each line, remove the previous indent, then add the new indent lines = input_text.split("\n").map do |line| line = line.sub(prev_indent, "") "#{next_indent}#{line}".rstrip end lines.join("\n") end |
#trim_lines(input_text) ⇒ Object
Remove trailing whitespace
96 97 98 |
# File 'lib/graphql/upgrader/member.rb', line 96 def trim_lines(input_text) input_text.gsub(/ +$/, "") end |
#underscorize(str) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/graphql/upgrader/member.rb', line 67 def underscorize(str) str .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') # URLDecoder -> URL_Decoder .gsub(/([a-z\d])([A-Z])/,'\1_\2') # someThing -> some_Thing .downcase end |