Class: Expressir::SchemaManifest
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Expressir::SchemaManifest
- Defined in:
- lib/expressir/schema_manifest.rb
Instance Attribute Summary collapse
-
#output_path ⇒ Object
Returns the value of attribute output_path.
Class Method Summary collapse
Instance Method Summary collapse
- #base_path ⇒ Object
- #concat(another_config) ⇒ Object
-
#initialize(**args) ⇒ SchemaManifest
constructor
A new instance of SchemaManifest.
- #path_absolute_to_relative(absolute_path, container_path) ⇒ Object
- #path_relative_to_absolute(relative_path) ⇒ Object
- #save_to_path(filename) ⇒ Object
- #schemas_from_yaml(model, value) ⇒ Object
- #schemas_to_yaml(model, doc) ⇒ Object
- #set_initial_path(new_path) ⇒ Object
- #to_file(to_path = path) ⇒ Object
- #update_path(new_path) ⇒ Object
- #update_schema_path(old_base_path, new_base_path) ⇒ Object
Constructor Details
#initialize(**args) ⇒ SchemaManifest
Returns a new instance of SchemaManifest.
13 14 15 16 |
# File 'lib/expressir/schema_manifest.rb', line 13 def initialize(**args) @path = path_relative_to_absolute(path) if path super end |
Instance Attribute Details
#output_path ⇒ Object
Returns the value of attribute output_path.
11 12 13 |
# File 'lib/expressir/schema_manifest.rb', line 11 def output_path @output_path end |
Class Method Details
.from_file(path) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/expressir/schema_manifest.rb', line 26 def self.from_file(path) from_yaml(File.read(path)).tap do |x| x.set_initial_path(path) end rescue StandardError => e raise InvalidSchemaManifestError, "Invalid schema manifest format: #{e.}" end |
Instance Method Details
#base_path ⇒ Object
18 19 20 |
# File 'lib/expressir/schema_manifest.rb', line 18 def base_path File.dirname(@path) end |
#concat(another_config) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/expressir/schema_manifest.rb', line 110 def concat(another_config) unless another_config.is_a?(self.class) raise StandardError, "Can only concat a SchemaManifest object." end # We need to update the relative paths when paths exist if path && another_config.path && path != another_config.path new_config = another_config.dup new_config.update_path(path) end schemas.concat(another_config.schemas) end |
#path_absolute_to_relative(absolute_path, container_path) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/expressir/schema_manifest.rb', line 88 def path_absolute_to_relative(absolute_path, container_path) container_path ||= @path return absolute_path unless container_path p = Pathname.new(container_path) container = p.directory? ? p.to_s : p.dirname Pathname.new(absolute_path).relative_path_from(container).to_s end |
#path_relative_to_absolute(relative_path) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/expressir/schema_manifest.rb', line 76 def path_relative_to_absolute(relative_path) eval_path = Pathname.new(relative_path) return relative_path if eval_path.absolute? # Or based on current working directory? return relative_path unless @path # ... but if this calculates path, we end up expanding it anyway Pathname.new(File.dirname(@path)).join(eval_path)..to_s end |
#save_to_path(filename) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/expressir/schema_manifest.rb', line 125 def save_to_path(filename) new_config = dup.tap do |c| c.path = filename c.update_path(File.dirname(filename)) c.output_path = filename end File.open(filename, "w") do |f| puts "Writing #{filename}..." f.write(new_config.to_yaml) puts "Done." end end |
#schemas_from_yaml(model, value) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/expressir/schema_manifest.rb', line 47 def schemas_from_yaml(model, value) model.schemas = value.map do |k, v| SchemaManifestEntry.new(id: k, path: path_relative_to_absolute(v["path"])) end end |
#schemas_to_yaml(model, doc) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/expressir/schema_manifest.rb', line 54 def schemas_to_yaml(model, doc) doc["schemas"] = model.schemas.sort_by(&:id).to_h do |schema| # We are outputting the schemas collection file to the directory where # the collection config is at (assumed to be Dir.pwd), not to the # directory we sourced the manifest from, e.g. # documents/iso-10303-41/schemas.yaml. # So the schema.container_path = @config.path is not # in fact needed, as the files are already absolute. This notion of # using @path to create relative paths was misconceived [ schema.id, { "path" => path_absolute_to_relative( schema.path, model.output_path || Dir.pwd, ), }, ] end end |
#set_initial_path(new_path) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/expressir/schema_manifest.rb', line 39 def set_initial_path(new_path) @path = path_relative_to_absolute(new_path) schemas.each do |schema| schema.path = path_relative_to_absolute(schema.path) schema.container_path = File.(@path) end end |
#to_file(to_path = path) ⇒ Object
34 35 36 37 |
# File 'lib/expressir/schema_manifest.rb', line 34 def to_file(to_path = path) FileUtils.mkdir_p(File.dirname(to_path)) File.write(to_path, to_yaml) end |
#update_path(new_path) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/expressir/schema_manifest.rb', line 97 def update_path(new_path) if @path.nil? @path = new_path return @path end old_base_path = File.dirname(@path) new_base_path = File.dirname(new_path) update_schema_path(old_base_path, new_base_path) @path = new_path end |
#update_schema_path(old_base_path, new_base_path) ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/expressir/schema_manifest.rb', line 139 def update_schema_path(old_base_path, new_base_path) schemas.each do |schema| schema_path = Pathname.new(schema.path) next if schema_path.absolute? schema_path = (Pathname.new(old_base_path) + schema_path).cleanpath # This is the new relative schema_path schema.path = schema_path.relative_path_from(new_base_path) end end |