Module: Opencdd::Entity::FieldRegistry

Defined in:
lib/opencdd/entity/field_registry.rb

Overview

Registry of declared fields on Entity subclasses. Open/closed: adding a field is a single field declaration in the entity class body — no edits to switch statements elsewhere.

Defined Under Namespace

Classes: Entry

Class Method Summary collapse

Class Method Details

.field_for(entity_class, name) ⇒ Object

Lookup a single field by name on entity_class. Walks up the ancestor chain so subclasses see inherited fields.



68
69
70
71
# File 'lib/opencdd/entity/field_registry.rb', line 68

def field_for(entity_class, name)
  each_entry(entity_class) { |e| return e if e.name == name.to_sym }
  nil
end

.fields_for(entity_class) ⇒ Object

Every field visible on entity_class, including inherited fields. Order: base-class declarations first, then subclass overrides (last wins on duplicate names).



76
77
78
79
80
# File 'lib/opencdd/entity/field_registry.rb', line 76

def fields_for(entity_class)
  seen = {}
  each_entry(entity_class) { |e| seen[e.name] = e }
  seen.values
end

.register(entity_class:, name:, property_id:, value_kind:, multilingual: false, synthetic: false, reader: nil, block: nil, json_key: nil) ⇒ Object

Register a field on entity_class. The DSL in Entity.field is the only intended caller. Re-registering the same name on the same class overwrites the prior declaration (useful for subclasses overriding the value_kind).

Synthetic fields prefer the block: form: the block is evaluated via instance_exec on the entity when the field is read, so it has access to private helpers without requiring send dispatch. The legacy reader: form (a Symbol naming a public method on the entity) is accepted for back-compat but should not be used for new fields.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opencdd/entity/field_registry.rb', line 48

def register(entity_class:, name:, property_id:, value_kind:,
             multilingual: false, synthetic: false, reader: nil,
             block: nil, json_key: nil)
  entry = Entry.new(
    entity_class: entity_class,
    name: name.to_sym,
    property_id: property_id,
    value_kind: value_kind,
    multilingual: multilingual,
    synthetic: synthetic,
    reader: reader,
    block: block,
    json_key: json_key,
  )
  @by_class[entity_class][entry.name] = entry
  entry
end