Class: Glossarist::GcrPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/glossarist/gcr_package.rb

Constant Summary collapse

COMPILED_EXTENSIONS =
{
  "tbx" => "tbx.xml",
  "jsonld" => "jsonld",
  "turtle" => "ttl",
  "jsonl" => "jsonl",
}.freeze
KNOWN_COMPILED_FORMATS =
COMPILED_EXTENSIONS.keys.freeze
DATASET_ASSETS =
[
  { path: "bibliography.yaml", type: :file, attr: :bibliography },
  { path: "images", type: :directory },
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_path) ⇒ GcrPackage

Returns a new instance of GcrPackage.



25
26
27
28
29
30
# File 'lib/glossarist/gcr_package.rb', line 25

def initialize(zip_path)
  @zip_path = zip_path
  @metadata = nil
  @concepts = []
  @bibliography = nil
end

Instance Attribute Details

#bibliographyObject (readonly)

Returns the value of attribute bibliography.



23
24
25
# File 'lib/glossarist/gcr_package.rb', line 23

def bibliography
  @bibliography
end

#conceptsObject (readonly)

Returns the value of attribute concepts.



23
24
25
# File 'lib/glossarist/gcr_package.rb', line 23

def concepts
  @concepts
end

#metadataObject (readonly)

Returns the value of attribute metadata.



23
24
25
# File 'lib/glossarist/gcr_package.rb', line 23

def 
  @metadata
end

#zip_pathObject (readonly)

Returns the value of attribute zip_path.



23
24
25
# File 'lib/glossarist/gcr_package.rb', line 23

def zip_path
  @zip_path
end

Class Method Details

.create(concepts:, metadata:, output_path:, register_data: nil, compiled_formats: [], **opts) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/glossarist/gcr_package.rb', line 32

def self.create(concepts:, metadata:, output_path:, register_data: nil,
                compiled_formats: [], **opts)
  FileUtils.mkdir_p(File.dirname(output_path))
  package = new(output_path)
  package.write(concepts, , register_data,
                compiled_formats: compiled_formats, **opts)
  package
end

.create_from_directory(dir, output:, shortname:, version:, title: nil, description: nil, owner: nil, tags: [], register_yaml: nil, uri_prefix: nil, concept_uri_template: nil, streaming: false, compiled_formats: []) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/ParameterLists



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/glossarist/gcr_package.rb', line 47

def self.create_from_directory(dir, output:, shortname:, version:, # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
                              title: nil, description: nil, owner: nil,
                              tags: [], register_yaml: nil,
                              uri_prefix: nil, concept_uri_template: nil,
                              streaming: false, compiled_formats: [])
  dir = File.expand_path(dir)
  formats = Array(compiled_formats).map(&:to_s)

  if streaming && formats.any?
    raise ArgumentError,
          "Compiled formats require batch mode (streaming: true is incompatible)"
  end

  if streaming
    create_streaming(dir, output: output, shortname: shortname, version: version,
                          title: title, description: description, owner: owner,
                          tags: tags, register_yaml: register_yaml,
                          uri_prefix: uri_prefix,
                          concept_uri_template: concept_uri_template)
  else
    create_batch(dir, output: output, shortname: shortname, version: version,
                      title: title, description: description, owner: owner,
                      tags: tags, register_yaml: register_yaml,
                      uri_prefix: uri_prefix,
                      concept_uri_template: concept_uri_template,
                      compiled_formats: formats)
  end
end

.each_dataset_asset(source_dir) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/glossarist/gcr_package.rb', line 76

def self.each_dataset_asset(source_dir)
  base = Pathname.new(source_dir)
  DATASET_ASSETS.each do |asset|
    path = File.join(source_dir, asset[:path])
    case asset[:type]
    when :file
      yield_file_asset(path, asset[:path]) { |*a| yield(*a) }
    when :directory
      yield_directory_assets(path, base) { |*a| yield(*a) }
    end
  end
end

.load(zip_path) ⇒ Object



41
42
43
44
45
# File 'lib/glossarist/gcr_package.rb', line 41

def self.load(zip_path)
  package = new(zip_path)
  package.read
  package
end

.yield_directory_assets(dir_path, base_path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/glossarist/gcr_package.rb', line 95

def self.yield_directory_assets(dir_path, base_path)
  return unless File.directory?(dir_path)

  Dir.glob(File.join(dir_path, "**", "*")).each do |file|
    next unless File.file?(file)

    relative = Pathname.new(file).relative_path_from(base_path).to_s
    yield relative, File.binread(file)
  end
end

.yield_file_asset(path, entry_name) {|entry_name, File.binread(path)| ... } ⇒ Object

Yields:

  • (entry_name, File.binread(path))


89
90
91
92
93
# File 'lib/glossarist/gcr_package.rb', line 89

def self.yield_file_asset(path, entry_name)
  return unless File.exist?(path)

  yield entry_name, File.binread(path)
end

Instance Method Details

#readObject



141
142
143
144
145
146
147
148
149
# File 'lib/glossarist/gcr_package.rb', line 141

def read
  @concepts = []

  Zip::File.open(@zip_path) do |zf|
    (zf)
    read_file_assets(zf)
    read_concepts(zf)
  end
end

#read_concepts(zip_file) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/glossarist/gcr_package.rb', line 169

def read_concepts(zip_file)
  zip_file.entries.each do |entry|
    next unless entry.name.start_with?("concepts/") && entry.name.end_with?(".yaml")

    raw = entry.get_input_stream.read
    doc = ConceptDocument.from_yamls(raw)
    @concepts << doc.to_managed_concept
  end
end

#read_file_assets(zip_file) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/glossarist/gcr_package.rb', line 158

def read_file_assets(zip_file)
  DATASET_ASSETS.each do |asset|
    next unless asset[:type] == :file && asset[:attr]

    entry = zip_file.find_entry(asset[:path])
    next unless entry

    instance_variable_set("@#{asset[:attr]}", entry.get_input_stream.read)
  end
end

#read_metadata(zip_file) ⇒ Object



151
152
153
154
155
156
# File 'lib/glossarist/gcr_package.rb', line 151

def (zip_file)
  entry = zip_file.find_entry("metadata.yaml")
  return unless entry

  @metadata = GcrMetadata.from_yaml(entry.get_input_stream.read)
end

#validateObject



106
107
108
# File 'lib/glossarist/gcr_package.rb', line 106

def validate
  GcrValidator.new.validate(@zip_path)
end

#write(concepts, metadata, register_data, compiled_formats: [], shortname: nil, source_dir: nil, **opts) ⇒ Object

rubocop:disable Metrics/ParameterLists



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/glossarist/gcr_package.rb', line 110

def write(concepts, , register_data, # rubocop:disable Metrics/ParameterLists
          compiled_formats: [],
          shortname: nil, source_dir: nil, **opts)
  Zip::File.open(@zip_path, create: true) do |zf|
    zf.get_output_stream("metadata.yaml") do |f|
      f.write(.to_yaml)
    end

    if register_data
      zf.get_output_stream("register.yaml") do |f|
        f.write(register_data.to_yaml)
      end
    end

    concepts.each do |mc|
      write_concept(zf, mc)
    end

    if source_dir
      self.class.each_dataset_asset(source_dir) do |name, content|
        zf.get_output_stream(name) { |f| f.write(content) }
      end
    end

    if compiled_formats.any?
      write_compiled(zf, concepts, compiled_formats, shortname: shortname,
                                                     **opts)
    end
  end
end