Class: TypedEAV::Registry

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/typed_eav/registry.rb

Overview

Registry of entity types (host ActiveRecord models) that have opted into typed fields via ‘has_typed_eav`. Tracks optional field-type restrictions per entity.

Populated automatically when a host model calls ‘has_typed_eav`; read by Field::Base#validate_type_allowed_for_entity to enforce restrictions on field creation.

Class Method Summary collapse

Class Method Details

.allowed_types_for(entity_type) ⇒ Object

Field-type restrictions for a given entity, or nil if unrestricted.



30
31
32
33
34
35
# File 'lib/typed_eav/registry.rb', line 30

def allowed_types_for(entity_type)
  entry = entities[entity_type]
  return nil unless entry

  entry[:types]
end

.entity_typesObject

All registered entity type names.



25
26
27
# File 'lib/typed_eav/registry.rb', line 25

def entity_types
  entities.keys
end

.register(entity_type, types: nil) ⇒ Object

Register an entity type with optional type restrictions.



20
21
22
# File 'lib/typed_eav/registry.rb', line 20

def register(entity_type, types: nil)
  entities[entity_type] = { types: types }
end

.reset!Object

Clear all registrations (test isolation).



47
48
49
# File 'lib/typed_eav/registry.rb', line 47

def reset!
  entities.clear
end

.type_allowed?(entity_type, field_type_class) ⇒ Boolean

Whether a field type class is allowed for an entity.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/typed_eav/registry.rb', line 38

def type_allowed?(entity_type, field_type_class)
  allowed = allowed_types_for(entity_type)
  return true if allowed.nil?

  type_name = field_type_class.name.demodulize.underscore.to_sym
  allowed.include?(type_name)
end