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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_path) ⇒ GcrPackage

Returns a new instance of GcrPackage.



19
20
21
22
23
# File 'lib/glossarist/gcr_package.rb', line 19

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

Instance Attribute Details

#conceptsObject (readonly)

Returns the value of attribute concepts.



17
18
19
# File 'lib/glossarist/gcr_package.rb', line 17

def concepts
  @concepts
end

#metadataObject (readonly)

Returns the value of attribute metadata.



17
18
19
# File 'lib/glossarist/gcr_package.rb', line 17

def 
  @metadata
end

#zip_pathObject (readonly)

Returns the value of attribute zip_path.



17
18
19
# File 'lib/glossarist/gcr_package.rb', line 17

def zip_path
  @zip_path
end

Class Method Details

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



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

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/glossarist/gcr_package.rb', line 40

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

.load(zip_path) ⇒ Object



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

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

Instance Method Details

#readObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/glossarist/gcr_package.rb', line 105

def read
  @concepts = []

  Zip::File.open(@zip_path) do |zf|
    if (entry = zf.find_entry("metadata.yaml"))
      @metadata = GcrMetadata.from_yaml(entry.get_input_stream.read)
    end

    zf.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
end

#validateObject



69
70
71
# File 'lib/glossarist/gcr_package.rb', line 69

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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/glossarist/gcr_package.rb', line 73

def write(concepts, , register_data, compiled_formats: [],
          shortname: 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 compiled_formats.any?
      write_compiled(zf, concepts, compiled_formats, shortname: shortname,
                                                     **opts)
    end
  end
end

#write_compiled(zip_file, concepts, formats, shortname: nil, **opts) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/glossarist/gcr_package.rb', line 123

def write_compiled(zip_file, concepts, formats, shortname: nil, **opts)
  name = shortname || "glossary"
  transform_opts = { shortname: name }.merge(opts.slice(:site_url,
                                                        :uri_prefix, :title))

  if formats.include?("tbx")
    write_compiled_tbx(zip_file, concepts, transform_opts, name)
  end

  skos_formats = formats & %w[jsonld turtle jsonl]
  if skos_formats.any?
    write_compiled_skos(zip_file, concepts, skos_formats, transform_opts,
                        name)
  end

  (formats - KNOWN_COMPILED_FORMATS).each do |fmt|
    warn "Warning: Unknown compiled format '#{fmt}', skipping"
  end
end

#write_compiled_skos(zip_file, concepts, formats, opts, name) ⇒ Object

rubocop:disable Metrics/MethodLength



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/glossarist/gcr_package.rb', line 151

def write_compiled_skos(zip_file, concepts, formats, opts, name) # rubocop:disable Metrics/MethodLength
  require "glossarist/transforms/concept_to_skos_transform"
  vocab = Transforms::ConceptToSkosTransform.transform_document(concepts,
                                                                opts)

  if formats.include?("jsonld")
    zip_file.get_output_stream("compiled/#{name}.jsonld") do |f|
      f.write(vocab.to_jsonld)
    end
  end

  if formats.include?("turtle")
    zip_file.get_output_stream("compiled/#{name}.ttl") do |f|
      f.write(vocab.to_turtle)
    end
  end

  return unless formats.include?("jsonl")

  zip_file.get_output_stream("compiled/#{name}.jsonl") do |f|
    concepts.each do |concept|
      skos = Transforms::ConceptToSkosTransform.transform(concept, opts)
      f.write(skos.to_jsonld)
      f.write("\n")
    end
  end
end

#write_compiled_tbx(zip_file, concepts, opts, name) ⇒ Object



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

def write_compiled_tbx(zip_file, concepts, opts, name)
  require "glossarist/transforms/concept_to_tbx_transform"
  doc = Transforms::ConceptToTbxTransform.transform_document(concepts, opts)
  zip_file.get_output_stream("compiled/#{name}.tbx.xml") do |f|
    f.write(doc.to_xml)
  end
end

#write_concept(zip_file, concept) ⇒ Object



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

def write_concept(zip_file, concept)
  termid = concept.data.id.to_s
  doc = ConceptDocument.from_managed_concept(concept)
  zip_file.get_output_stream("concepts/#{termid}.yaml") do |f|
    f.write(doc.to_yamls)
  end
end