Class: Foobara::TypeDeclarations::TypeBuilder

Inherits:
Object
  • Object
show all
Includes:
Foobara::TruncatedInspect
Defined in:
foobara-0.6.1/projects/typesystem/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.



88
89
90
91
92
93
94
95
96
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 88

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.



86
87
88
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 86

def accesses
  @accesses
end

#nameObject

Returns the value of attribute name.



86
87
88
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 86

def name
  @name
end

#type_declaration_handler_registryObject

Returns the value of attribute type_declaration_handler_registry.



86
87
88
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 86

def type_declaration_handler_registry
  @type_declaration_handler_registry
end

Instance Method Details

#accesses_up_hierarchyObject



98
99
100
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 98

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

#handler_for_class(klass) ⇒ Object



125
126
127
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 125

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

#handlersObject



121
122
123
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 121

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

#register_type_declaration_handler(type_declaration_handler) ⇒ Object

declaration handlers



104
105
106
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 104

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

#type_declaration_handler_for(type_declaration) ⇒ Object



112
113
114
115
116
117
118
119
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 112

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.declaration_data}"
end

#type_declaration_handler_registriesObject



108
109
110
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 108

def type_declaration_handler_registries
  accesses_up_hierarchy.map(&:type_declaration_handler_registry)
end

#type_for_declaration(*type_declaration_bits, &block) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 145

def type_for_declaration(*type_declaration_bits, &block)
  lru_cache.cached([self, *block&.object_id, *type_declaration_bits]) do
    type_for_declaration_without_cache(*type_declaration_bits, &block)
  end
rescue NoTypeDeclarationHandlerFoundError
  raise if TypeDeclarations.strict_stringified?
  raise if TypeDeclarations.stringified?
  raise if TypeDeclarations.strict?

  TypeDeclarations.stringified do
    type_for_declaration(*type_declaration_bits, &block)
  end
end

#type_for_declaration_without_cache(*type_declaration_bits) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 159

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

  type = type_declaration.type
  return type if type

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

#type_for_strict_declaration(type_declaration) ⇒ Object



137
138
139
140
141
142
143
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 137

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

#type_for_strict_stringified_declaration(type_declaration) ⇒ Object



129
130
131
132
133
134
135
# File 'projects/typesystem/projects/type_declarations/src/type_builder.rb', line 129

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