Class: Yogurt::CodeGenerator::LeafClass

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
DefinedClass, Utils
Defined in:
lib/yogurt/code_generator/leaf_class.rb

Overview

Leaf classes are generated for the inner types of query results.

Instance Method Summary collapse

Methods included from Utils

#camelize, #generate_method_name, #generate_pretty_print, #indent, #possible_types_constant, #typename_method, #underscore

Methods included from DefinedClass

#dependencies, #name

Instance Method Details

#merge_defined_methods(extra_methods) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yogurt/code_generator/leaf_class.rb', line 20

def merge_defined_methods(extra_methods)
  own_methods = defined_methods.map {|dm| [dm.name, dm]}.to_h
  extra_methods.each do |extra|
    own = own_methods[extra.name]
    if own.nil?
      own_methods[extra.name] = extra
    elsif !own.merge?(extra)
      raise "Cannot merge method #{extra.inspect} into #{own.inspect}"
    end
  end

  self.defined_methods = own_methods.values
end

#to_rubyObject



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/yogurt/code_generator/leaf_class.rb', line 35

def to_ruby
  pretty_print = generate_pretty_print(defined_methods)

  dynamic_methods = <<~STRING.strip
    #{defined_methods.map(&:to_ruby).join("\n")}
    #{pretty_print}
  STRING

  <<~STRING
    class #{name}
      extend T::Sig
      include Yogurt::QueryResult

      #{indent(possible_types_constant(schema, graphql_type), 1).strip}

      sig {params(result: Yogurt::OBJECT_TYPE).void}
      def initialize(result)
        @result = T.let(result, Yogurt::OBJECT_TYPE)
      end

      sig {override.returns(Yogurt::OBJECT_TYPE)}
      def raw_result
        @result
      end

      #{indent(typename_method(schema, graphql_type), 1).strip}

      #{indent(dynamic_methods, 1).strip}
    end
  STRING
end