Class: Glossarist::GcrPackage
- Inherits:
-
Object
- Object
- Glossarist::GcrPackage
- 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
-
#bibliography ⇒ Object
readonly
Returns the value of attribute bibliography.
-
#concepts ⇒ Object
readonly
Returns the value of attribute concepts.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#zip_path ⇒ Object
readonly
Returns the value of attribute zip_path.
Class Method Summary collapse
- .create(concepts:, metadata:, output_path:, register_data: nil, compiled_formats: [], **opts) ⇒ Object
-
.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.
- .each_dataset_asset(source_dir) ⇒ Object
- .load(zip_path) ⇒ Object
- .yield_directory_assets(dir_path, base_path) ⇒ Object
- .yield_file_asset(path, entry_name) {|entry_name, File.binread(path)| ... } ⇒ Object
Instance Method Summary collapse
-
#initialize(zip_path) ⇒ GcrPackage
constructor
A new instance of GcrPackage.
- #read ⇒ Object
- #read_concepts(zip_file) ⇒ Object
- #read_file_assets(zip_file) ⇒ Object
- #read_metadata(zip_file) ⇒ Object
- #validate ⇒ Object
-
#write(concepts, metadata, register_data, compiled_formats: [], shortname: nil, source_dir: nil, **opts) ⇒ Object
rubocop:disable Metrics/ParameterLists.
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
#bibliography ⇒ Object (readonly)
Returns the value of attribute bibliography.
23 24 25 |
# File 'lib/glossarist/gcr_package.rb', line 23 def bibliography @bibliography end |
#concepts ⇒ Object (readonly)
Returns the value of attribute concepts.
23 24 25 |
# File 'lib/glossarist/gcr_package.rb', line 23 def concepts @concepts end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
23 24 25 |
# File 'lib/glossarist/gcr_package.rb', line 23 def @metadata end |
#zip_path ⇒ Object (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.(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: , 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: , 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
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
#read ⇒ Object
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
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/glossarist/gcr_package.rb', line 163 def read_concepts(zip_file) doc_class = concept_document_class_for_read 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 = doc_class.from_yamls(raw) @concepts << doc.to_managed_concept end end |
#read_file_assets(zip_file) ⇒ Object
158 159 160 161 |
# File 'lib/glossarist/gcr_package.rb', line 158 def read_file_assets(zip_file) entry = zip_file.find_entry("bibliography.yaml") @bibliography = entry.get_input_stream.read if entry 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 |
#validate ⇒ Object
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 |