Class: Lutaml::Jsonschema::SchemaStore

Inherits:
SchemaSet
  • Object
show all
Defined in:
lib/lutaml/jsonschema/schema_store.rb

Overview

SchemaStore extends SchemaSet with on-disk persistence via lutaml-store.

Inherited from SchemaSet (zero duplication):

resolve_ref, validate!, valid?, validation_errors,
all_definitions, all_properties, collect_refs

Added by SchemaStore:

load_from_directory, save_to_directory, configuration access,
remove_schema, convenience query methods

Instance Attribute Summary collapse

Attributes inherited from SchemaSet

#base_dir, #schemas

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SchemaSet

#all_definitions, #all_properties, infer_base_dir, #resolve_ref, #source_json, #valid?, #validate!, #validation_errors

Constructor Details

#initialize(package: nil) ⇒ SchemaStore

Returns a new instance of SchemaStore.



17
18
19
20
# File 'lib/lutaml/jsonschema/schema_store.rb', line 17

def initialize(package: nil)
  super()
  @package = package || Lutaml::Store::PackageStore.new(LjrPackageDefinition.definition)
end

Instance Attribute Details

#packageObject (readonly)

Returns the value of attribute package.



15
16
17
# File 'lib/lutaml/jsonschema/schema_store.rb', line 15

def package
  @package
end

Class Method Details

.load_from_directory(dir, format: nil) ⇒ Object

── Class-level loaders ──



24
25
26
27
28
29
30
31
32
# File 'lib/lutaml/jsonschema/schema_store.rb', line 24

def self.load_from_directory(dir, format: nil)
  pkg = Lutaml::Store::PackageStore.load(
    LjrPackageDefinition.definition, dir,
    transport: :directory, format: format
  )
  store = new(package: pkg)
  store.sync_from_package
  store
end

.load_from_files(*paths, base_dir: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lutaml/jsonschema/schema_store.rb', line 34

def self.load_from_files(*paths, base_dir: nil)
  store = new
  paths.each do |path|
    data = File.read(path, encoding: "utf-8")
    schema = Schema.from_json(data)
    name = File.basename(path, ".*")
    schema.dollar_id ||= name
    store.add(name, schema, path, data)
  end
  store.base_dir = base_dir if base_dir
  store
end

Instance Method Details

#add(name, schema, file_path = nil, source_json = nil) ⇒ Object

── Overrides ──



49
50
51
52
# File 'lib/lutaml/jsonschema/schema_store.rb', line 49

def add(name, schema, file_path = nil, source_json = nil)
  super
  @package.add_model(schema)
end

#add_schema(schema, file_path = nil, source_json = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lutaml/jsonschema/schema_store.rb', line 72

def add_schema(schema, file_path = nil, source_json = nil)
  name = schema.dollar_id || (if file_path
                                File.basename(file_path,
                                              ".*")
                              end)
  unless name
    raise Error,
          "Schema must have $id or file_path for name derivation"
  end

  schema.dollar_id ||= name
  add(name, schema, file_path, source_json)
end

#configurationObject

── Configuration ──



93
94
95
# File 'lib/lutaml/jsonschema/schema_store.rb', line 93

def configuration
  @package.models_for(Configuration).first
end

#configuration=(config) ⇒ Object



97
98
99
100
101
# File 'lib/lutaml/jsonschema/schema_store.rb', line 97

def configuration=(config)
  existing = configuration
  @package.remove_model(Configuration, existing.title) if existing&.title
  @package.add_model(config)
end

#remove_schema(name) ⇒ Object



86
87
88
89
# File 'lib/lutaml/jsonschema/schema_store.rb', line 86

def remove_schema(name)
  s = @schemas.delete(name)
  @package.remove_model(Schema, s.dollar_id) if s&.dollar_id
end

#save_to_directory(path, format: nil, formats: {}) ⇒ Object

── Persistence ──



105
106
107
108
# File 'lib/lutaml/jsonschema/schema_store.rb', line 105

def save_to_directory(path, format: nil, formats: {})
  @package.save(path, transport: :directory, format: format,
                      formats: formats)
end

#schema(name) ⇒ Object

── Convenience accessors ──



56
57
58
# File 'lib/lutaml/jsonschema/schema_store.rb', line 56

def schema(name)
  @schemas[name]
end

#schema_countObject



64
65
66
# File 'lib/lutaml/jsonschema/schema_store.rb', line 64

def schema_count
  @schemas.size
end

#schema_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/lutaml/jsonschema/schema_store.rb', line 68

def schema_exists?(name)
  @schemas.key?(name)
end

#schema_namesObject



60
61
62
# File 'lib/lutaml/jsonschema/schema_store.rb', line 60

def schema_names
  @schemas.keys
end

#sync_from_packageObject

── Sync ──



112
113
114
115
116
117
118
119
120
# File 'lib/lutaml/jsonschema/schema_store.rb', line 112

def sync_from_package
  @package.models_for(Schema).each do |s|
    next unless s.dollar_id

    name = File.basename(s.dollar_id).sub(/\..*/, "")
    name = disambiguate(name) if @schemas.key?(name)
    @schemas[name] = s
  end
end