Class: Lutaml::Store::PackageStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/package_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ PackageStore

Returns a new instance of PackageStore.



9
10
11
12
13
14
15
16
17
# File 'lib/lutaml/store/package_store.rb', line 9

def initialize(definition)
  @definition = definition
  @db = DatabaseStore.new(
    adapter: :memory,
    models: definition.database_store_models
  )
  @assets = {}
  @metadata = nil
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



7
8
9
# File 'lib/lutaml/store/package_store.rb', line 7

def assets
  @assets
end

#dbObject (readonly)

Public access for PackageTransport (avoids instance_variable_get).



122
123
124
# File 'lib/lutaml/store/package_store.rb', line 122

def db
  @db
end

#definitionObject (readonly)

Returns the value of attribute definition.



7
8
9
# File 'lib/lutaml/store/package_store.rb', line 7

def definition
  @definition
end

#metadataObject

Returns the value of attribute metadata.



6
7
8
# File 'lib/lutaml/store/package_store.rb', line 6

def 
  @metadata
end

Class Method Details

.load(definition, path, transport: :directory, format: nil) ⇒ Object

── Load / Save ──



21
22
23
24
25
26
# File 'lib/lutaml/store/package_store.rb', line 21

def self.load(definition, path, transport: :directory, format: nil)
  store = new(definition)
  transporter = resolve_transport(transport)
  transporter.read(path, store, format: format)
  store
end

.resolve_transport(transport) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/lutaml/store/package_store.rb', line 126

def self.resolve_transport(transport)
  case transport
  when :directory, "directory"
    PackageTransport::DirectoryTransport.new
  when :zip, "zip"
    PackageTransport::ZipTransport.new
  else
    raise ConfigurationError, "Unknown transport: #{transport}"
  end
end

Instance Method Details

#add_asset(path, content) ⇒ Object



75
76
77
# File 'lib/lutaml/store/package_store.rb', line 75

def add_asset(path, content)
  @assets[path] = content
end

#add_model(model_instance) ⇒ Object

── Model CRUD ──



36
37
38
# File 'lib/lutaml/store/package_store.rb', line 36

def add_model(model_instance)
  @db.save(model_instance)
end

#add_models(model_instances) ⇒ Object



40
41
42
# File 'lib/lutaml/store/package_store.rb', line 40

def add_models(model_instances)
  model_instances.each { |m| add_model(m) }
end

#asset(path) ⇒ Object

── Assets ──



71
72
73
# File 'lib/lutaml/store/package_store.rb', line 71

def asset(path)
  @assets[path]
end

#asset_pathsObject



79
80
81
# File 'lib/lutaml/store/package_store.rb', line 79

def asset_paths
  @assets.keys
end

#clear_allObject



101
102
103
104
105
# File 'lib/lutaml/store/package_store.rb', line 101

def clear_all
  definition.model_classes.each { |mc| clear_models(mc) }
  clear_assets
  @metadata = nil
end

#clear_assetsObject



97
98
99
# File 'lib/lutaml/store/package_store.rb', line 97

def clear_assets
  @assets.clear
end

#clear_models(model_class) ⇒ Object

── Bulk ──



89
90
91
92
93
94
95
# File 'lib/lutaml/store/package_store.rb', line 89

def clear_models(model_class)
  @db.all(model: model_class).each do |m|
    entry = definition.entry_for(model_class)
    key = m.public_send(entry.key)
    @db.destroy(model: model_class, **{ entry.key => key })
  end
end

#fetch_model(model_class, key) ⇒ Object



44
45
46
47
# File 'lib/lutaml/store/package_store.rb', line 44

def fetch_model(model_class, key)
  entry = definition.entry_for(model_class)
  @db.fetch(model: model_class, **{ entry.key => key })
end

#model_count(model_class) ⇒ Object



53
54
55
# File 'lib/lutaml/store/package_store.rb', line 53

def model_count(model_class)
  @db.count(model: model_class)
end

#model_exists?(model_class, key) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/lutaml/store/package_store.rb', line 57

def model_exists?(model_class, key)
  entry = definition.entry_for(model_class)
  @db.exists?(model: model_class, **{ entry.key => key })
end

#models_for(model_class) ⇒ Object



49
50
51
# File 'lib/lutaml/store/package_store.rb', line 49

def models_for(model_class)
  @db.all(model: model_class)
end

#remove_asset(path) ⇒ Object



83
84
85
# File 'lib/lutaml/store/package_store.rb', line 83

def remove_asset(path)
  @assets.delete(path)
end

#remove_model(model_class, key) ⇒ Object



62
63
64
65
# File 'lib/lutaml/store/package_store.rb', line 62

def remove_model(model_class, key)
  entry = definition.entry_for(model_class)
  @db.destroy(model: model_class, **{ entry.key => key })
end

#save(path, transport: :directory, format: nil, formats: {}) ⇒ Object



28
29
30
31
32
# File 'lib/lutaml/store/package_store.rb', line 28

def save(path, transport: :directory, format: nil, formats: {})
  resolved_formats = resolve_formats(format, formats)
  transporter = self.class.resolve_transport(transport)
  transporter.write(path, self, formats: resolved_formats)
end

#statsObject

── Stats ──



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lutaml/store/package_store.rb', line 109

def stats
  model_stats = definition.model_classes
                          .map { |mc| [mc.name, model_count(mc)] }
                          .to_h
  {
    package: definition.name,
    models: model_stats,
    assets: @assets.size,
    metadata: @metadata ? true : false
  }
end