Class: Opencdd::Model::EntityStore

Inherits:
Object
  • Object
show all
Defined in:
lib/opencdd/model/entity_store.rb

Overview

Per-entity YAML persistence using lutaml-store's DatabaseStore as the backend. Each entity is stored as a single YAML file, serialized via Lutaml::Model (Entity::Yaml — the deepened adapter inside Entity's namespace). Diff-friendly at the entity level — one git diff shows exactly which entity changed.

Uses DatabaseStore#save_all and #load_all with format: :yaml and layout: :separate (one file per entity). This is the proper lutaml-store API — not the low-level adapter.set/get bypass that the previous version used.

Directory layout (managed by DatabaseStore's separate layout):

data/
└── entities/
  ├── 0112_2___61360_4_AAA001.yaml
  ├── 0112_2___61360_4_AAA010.yaml
  └── ...

The filename is the model's key field (irdi), sanitized by the FileSystem adapter (non-alphanumeric chars → underscore).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ EntityStore

Returns a new instance of EntityStore.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/opencdd/model/entity_store.rb', line 32

def initialize(path)
  @path = File.expand_path(path.to_s)
  FileUtils.mkdir_p(@path)
  @store = Lutaml::Store.new(
    adapter: :filesystem,
    adapter_options: { path: @path, extension: ".yaml" },
    models: [
      { model: Opencdd::Entity::Yaml, key: :irdi, dir: "entities" },
    ],
  )
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/opencdd/model/entity_store.rb', line 30

def path
  @path
end

#storeObject (readonly)

Returns the value of attribute store.



30
31
32
# File 'lib/opencdd/model/entity_store.rb', line 30

def store
  @store
end

Instance Method Details

#fetch(key) ⇒ Object

Fetch a single entity's YAML model by key.



76
77
78
79
80
# File 'lib/opencdd/model/entity_store.rb', line 76

def fetch(key)
  @store.fetch(model: Opencdd::Entity::Yaml, irdi: key)
rescue Lutaml::Store::InvalidKeyError
  nil
end

#load_database(database = nil) ⇒ Object

Load all YAML files from the store into a Database. Returns a finalized Database.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/opencdd/model/entity_store.rb', line 57

def load_database(database = nil)
  database ||= Opencdd::Database.new
  yaml_entities = @store.load_all(
    Opencdd::Entity::Yaml,
    path: @path,
    format: :yaml,
    layout: :separate,
  )
  yaml_entities.each do |yaml_entity|
    entity = yaml_entity.to_entity(database)
    database.add_entity(entity)
  rescue StandardError => e
    warn "EntityStore: skipping entity: #{e.message}"
  end
  database.finalize!
  database
end

#save_database(database) ⇒ Object

Save all entities from database to individual YAML files via DatabaseStore#save_all. Returns self.



46
47
48
49
50
51
52
53
# File 'lib/opencdd/model/entity_store.rb', line 46

def save_database(database)
  yaml_entities = database.entities.filter_map do |entity|
    yaml = Opencdd::Entity::Yaml.from_entity(entity)
    yaml.irdi ? yaml : nil
  end
  @store.save_all(yaml_entities, path: @path, format: :yaml, layout: :separate)
  self
end