Class: Opencdd::Model::EntityStore
- Inherits:
-
Object
- Object
- Opencdd::Model::EntityStore
- Defined in:
- lib/opencdd/model/entity_store.rb
Overview
Per-entity YAML persistence using lutaml-store as the backend. Each entity is stored as a single YAML file in a directory layout, serialized via Lutaml::Model (YamlEntity). Diff-friendly at the entity level — one git diff shows exactly which entity changed.
Directory layout (managed by lutaml-store's FileSystem adapter):
data/
└── entities/
├── AA/
│ ├── AAA001.data
│ └── AAA010.data
└── ...
The FileSystem adapter shards by first 2 chars for scalability.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#fetch(key) ⇒ Object
Fetch a single entity by key.
-
#initialize(path) ⇒ EntityStore
constructor
A new instance of EntityStore.
-
#load_database(database = nil) ⇒ Object
Load all YAML files from the store into a Database.
-
#save_database(database) ⇒ Object
Save all entities from
databaseto individual YAML files via lutaml-store's FileSystem backend.
Constructor Details
#initialize(path) ⇒ EntityStore
Returns a new instance of EntityStore.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/opencdd/model/entity_store.rb', line 26 def initialize(path) @path = File.(path.to_s) FileUtils.mkdir_p(@path) @store = Lutaml::Store.new( adapter: :filesystem, adapter_options: { path: @path }, models: [ { model: Opencdd::Model::YamlEntity, key: :irdi, dir: "entities" }, ], ) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/opencdd/model/entity_store.rb', line 24 def path @path end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
24 25 26 |
# File 'lib/opencdd/model/entity_store.rb', line 24 def store @store end |
Instance Method Details
#fetch(key) ⇒ Object
Fetch a single entity by key.
71 72 73 74 75 |
# File 'lib/opencdd/model/entity_store.rb', line 71 def fetch(key) raw = @store.store.adapter.get(safe_key(key)) return nil unless raw Opencdd::Model::YamlEntity.from_yaml(raw) end |
#load_database(database = nil) ⇒ Object
Load all YAML files from the store into a Database. Returns a finalized Database.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/opencdd/model/entity_store.rb', line 53 def load_database(database = nil) database ||= Opencdd::Database.new @store.store.adapter.keys.each do |key| raw = @store.store.adapter.get(key) next unless raw begin yaml_entity = Opencdd::Model::YamlEntity.from_yaml(raw) entity = yaml_entity.to_entity(database) database.add_entity(entity) rescue StandardError => e warn "EntityStore: skipping #{key}: #{e.}" end end database.finalize! database end |
#save_database(database) ⇒ Object
Save all entities from database to individual YAML files
via lutaml-store's FileSystem backend. Returns self.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/opencdd/model/entity_store.rb', line 40 def save_database(database) database.entities.each do |entity| yaml_entity = Opencdd::Model::YamlEntity.from_entity(entity) key = safe_key(yaml_entity.irdi || yaml_entity.code) next unless key yaml_str = yaml_entity.to_yaml @store.store.adapter.set(key, yaml_str) end self end |