Module: Yogurt::CodeGenerator::Utils

Extended by:
T::Sig, Utils
Included in:
Yogurt::CodeGenerator, EnumClass, FieldAccessMethod, FieldAccessMethod::FragmentBranch, FieldAccessPath, FieldAccessPath, InputClass, LeafClass, RootClass, Utils
Defined in:
lib/yogurt/code_generator/utils.rb

Instance Method Summary collapse

Instance Method Details

#camelize(term) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/yogurt/code_generator/utils.rb', line 73

def camelize(term)
  string = term.to_s
  string = string.sub(/^[a-z\d]*/, &:capitalize)
  string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) {"#{Regexp.last_match(1)}#{T.must(Regexp.last_match(2)).capitalize}"}
  string.gsub!("/", "::")
  string
end

#generate_method_name(desired_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/yogurt/code_generator/utils.rb', line 97

def generate_method_name(desired_name)
  base_desired_name = desired_name
  escaping_level = 0

  while PROTECTED_NAMES.include?(desired_name)
    escaping_level += 1
    desired_name = "#{base_desired_name}#{'_' * escaping_level}"
  end

  desired_name.to_sym
end

#generate_pretty_print(methods) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/yogurt/code_generator/utils.rb', line 110

def generate_pretty_print(methods)
  inspect_lines = methods.map do |dm|
    <<~STRING
      p.comma_breakable
      p.text(#{dm.name.to_s.inspect})
      p.text(': ')
      p.pp(#{dm.name})
    STRING
  end

  object_group = <<~STRING.strip
    p.breakable
    p.text('__typename')
    p.text(': ')
    p.pp(__typename)

    #{inspect_lines.join("\n\n")}
  STRING

  <<~STRING
    sig {override.params(p: PP::PPMethods).void}
    def pretty_print(p)
      p.object_group(self) do
        #{indent(object_group, 2).strip}
      end
    end
  STRING
end

#indent(string, amount) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/yogurt/code_generator/utils.rb', line 82

def indent(string, amount)
  return string if amount.zero?

  padding = '  ' * amount

  buffer = T.unsafe(String).new("", capacity: string.size)
  string.each_line do |line|
    buffer << padding if line.size > 1 || line != "\n"
    buffer << line
  end

  buffer
end

#possible_types_constant(schema, graphql_type) ⇒ Object



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
# File 'lib/yogurt/code_generator/utils.rb', line 32

def possible_types_constant(schema, graphql_type)
  possible_types = schema
    .possible_types(graphql_type)
    .map(&:graphql_name)
    .sort
    .map(&:inspect)

  single_line = possible_types.join(', ')
  if single_line.size <= 80
    <<~STRING.strip
      POSSIBLE_TYPES = T.let(
        [#{single_line}],
        T::Array[String]
      )
    STRING
  else
    multi_line = possible_types.join(",\n")
    <<~STRING.strip
      POSSIBLE_TYPES = T.let(
        [
          #{indent(multi_line, 2).strip}
        ].freeze,
        T::Array[String]
      )
    STRING
  end
end

#typename_method(schema, graphql_type) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yogurt/code_generator/utils.rb', line 11

def typename_method(schema, graphql_type)
  possible_types = schema.possible_types(graphql_type)

  if possible_types.size == 1
    <<~STRING
      sig {override.returns(String)}
      def __typename
        #{possible_types.fetch(0).graphql_name.inspect}
      end
    STRING
  else
    <<~STRING
      sig {override.returns(String)}
      def __typename
        raw_result["__typename"]
      end
    STRING
  end
end

#underscore(camel_cased_word) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/yogurt/code_generator/utils.rb', line 61

def underscore(camel_cased_word)
  return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)

  word = camel_cased_word.to_s.gsub("::", "/")
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end