Class: Yogurt::CodeGenerator::FieldAccessMethod::FragmentBranch

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

Overview

Indicates the possible object types that could occur at runtime, and which expressions should be used if that object type appears.

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 Memoize

#freeze, #memoize_as

Instance Method Details

#<=>(other) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/yogurt/code_generator/field_access_method.rb', line 38

def <=>(other)
  return unless other.is_a?(FragmentBranch)

  comparison = sorted_typenames <=> other.sorted_typenames
  return comparison if comparison != 0

  expression <=> other.expression
end

#sorted_typenamesObject



25
26
27
# File 'lib/yogurt/code_generator/field_access_method.rb', line 25

def sorted_typenames
  memoize_as(:sorted_typenames) {typenames.to_a.sort}
end

#to_rubyObject



30
31
32
33
34
35
# File 'lib/yogurt/code_generator/field_access_method.rb', line 30

def to_ruby
  <<~STRING.strip
    when #{sorted_typenames.map(&:inspect).join(', ')}
    #{indent(expression, 1)}
  STRING
end