Class: Foobara::TypeDeclarations::TypeBuilder

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



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

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.



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

def accesses
  @accesses
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#type_declaration_handler_registryObject

Returns the value of attribute type_declaration_handler_registry.



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

def type_declaration_handler_registry
  @type_declaration_handler_registry
end

Instance Method Details

#accesses_up_hierarchyObject



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

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

#clear_cacheObject



155
156
157
158
159
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 155

def clear_cache
  if @lru_cache
    lru_cache.reset!
  end
end

#handler_for_class(klass) ⇒ Object



111
112
113
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 111

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

#handlersObject



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

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

#register_type_declaration_handler(type_declaration_handler) ⇒ Object

declaration handlers



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

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

#type_declaration_handler_for(type_declaration) ⇒ Object



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

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



94
95
96
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 94

def type_declaration_handler_registries
  accesses_up_hierarchy.map(&:type_declaration_handler_registry)
end

#type_for_declaration(*type_declaration_bits, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 131

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
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



145
146
147
148
149
150
151
152
153
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 145

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



123
124
125
126
127
128
129
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 123

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



115
116
117
118
119
120
121
# File 'foobara-0.1.7/projects/type_declarations/src/type_builder.rb', line 115

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