Module: Opencdd::Entity::FieldReader

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

Overview

Typed reader for field declarations. Knows how to extract a value of a given kind from an entity's properties hash. The single source of truth for "what does MDC_P004.en look like" so the Json exporter, validators, and TS codegen all agree.

Class Method Summary collapse

Class Method Details

.read(entity, name, lang: nil) ⇒ Object

Read the name field from entity. Multilingual fields accept a lang: keyword and fall back to source-language (stored under the bare MDC_P### key) when the requested language is missing.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/opencdd/entity/field_reader.rb', line 15

def read(entity, name, lang: nil)
  entry = FieldRegistry.field_for(entity.class, name)
  return nil unless entry

  # Synthetic fields defined with a block: evaluate the block
  # in the entity's context via instance_exec. This preserves
  # access to private helper methods without using send to
  # bypass privacy (encapsulation rule from CLAUDE.md).
  return entity.instance_exec(&entry.block) if entry.block?

  # Legacy form: synthetic fields name a public reader method.
  # public_send is safe here because readers are public API.
  return entity.public_send(entry.reader) if entry.synthetic? && entry.reader

  raw =
    if entry.multilingual?
      raw_multilingual(entity, entry, lang)
    else
      entity.properties[entry.property_id]
    end

  coerce(raw, entry.value_kind)
end