Class: Foobara::TypeDeclarations::TypeBuilder

Inherits:
Object
  • Object
show all
Includes:
Foobara::TruncatedInspect
Defined in:
foobara-0.0.110/projects/type_declarations/src/type_builder.rb

Defined Under Namespace

Classes: NoTypeDeclarationHandlerFoundError

Constant Summary

Constants included from Foobara::TruncatedInspect

Foobara::TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foobara::TruncatedInspect

#inspect, truncating

Constructor Details

#initialize(name, accesses: GlobalDomain.foobara_type_builder, type_declaration_handler_registry: TypeDeclarations::TypeDeclarationHandlerRegistry.new) ⇒ TypeBuilder

Returns a new instance of TypeBuilder.



52
53
54
55
56
57
58
59
60
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 52

def initialize(
  name,
  accesses: GlobalDomain.foobara_type_builder,
  type_declaration_handler_registry: TypeDeclarations::TypeDeclarationHandlerRegistry.new
)
  self.name = name
  self.type_declaration_handler_registry = type_declaration_handler_registry
  self.accesses = Util.array(accesses).to_set
end

Instance Attribute Details

#accessesObject

Returns the value of attribute accesses.



50
51
52
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 50

def accesses
  @accesses
end

#nameObject

Returns the value of attribute name.



50
51
52
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 50

def name
  @name
end

#type_declaration_handler_registryObject

Returns the value of attribute type_declaration_handler_registry.



50
51
52
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 50

def type_declaration_handler_registry
  @type_declaration_handler_registry
end

Instance Method Details

#accesses_up_hierarchyObject



62
63
64
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 62

def accesses_up_hierarchy
  [self, *accesses, *accesses.map(&:accesses_up_hierarchy).flatten].uniq
end

#clear_cacheObject



120
121
122
123
124
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 120

def clear_cache
  if @lru_cache
    lru_cache.reset!
  end
end

#handler_for_class(klass) ⇒ Object



89
90
91
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 89

def handler_for_class(klass)
  handlers.find { |handler| handler.instance_of?(klass) }
end

#handlersObject



85
86
87
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 85

def handlers
  type_declaration_handler_registries.map(&:handlers).flatten.sort_by(&:priority)
end

#register_type_declaration_handler(type_declaration_handler) ⇒ Object

declaration handlers



68
69
70
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 68

def register_type_declaration_handler(type_declaration_handler)
  type_declaration_handler_registry.register(type_declaration_handler)
end

#type_declaration_handler_for(type_declaration) ⇒ Object



76
77
78
79
80
81
82
83
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 76

def type_declaration_handler_for(type_declaration)
  handlers.each do |handler|
    return handler if handler.applicable?(type_declaration)
  end

  raise NoTypeDeclarationHandlerFoundError,
        "No type declaration handler found for #{type_declaration}"
end

#type_declaration_handler_registriesObject



72
73
74
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 72

def type_declaration_handler_registries
  accesses_up_hierarchy.map(&:type_declaration_handler_registry)
end

#type_for_declaration(*type_declaration_bits, &block) ⇒ Object



107
108
109
110
111
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 107

def type_for_declaration(*type_declaration_bits, &block)
  lru_cache.cached([type_declaration_bits, block]) do
    type_for_declaration_without_cache(*type_declaration_bits, &block)
  end
end

#type_for_declaration_without_cache(*type_declaration_bits) ⇒ Object



113
114
115
116
117
118
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 113

def type_for_declaration_without_cache(*type_declaration_bits, &)
  type_declaration = TypeDeclarations.args_to_type_declaration(*type_declaration_bits, &)

  handler = type_declaration_handler_for(type_declaration)
  handler.process_value!(type_declaration)
end

#type_for_strict_declaration(type_declaration) ⇒ Object



100
101
102
103
104
105
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 100

def type_for_strict_declaration(type_declaration)
  TypeDeclarations.strict do
    handler = type_declaration_handler_for(type_declaration)
    handler.process_value!(type_declaration)
  end
end

#type_for_strict_stringified_declaration(type_declaration) ⇒ Object



93
94
95
96
97
98
# File 'foobara-0.0.110/projects/type_declarations/src/type_builder.rb', line 93

def type_for_strict_stringified_declaration(type_declaration)
  TypeDeclarations.strict_stringified do
    handler = type_declaration_handler_for(type_declaration)
    handler.process_value!(type_declaration)
  end
end