Class: Suma::SchemaConfig::Config
- Inherits:
-
Shale::Mapper
- Object
- Shale::Mapper
- Suma::SchemaConfig::Config
- Defined in:
- lib/suma/schema_config/config.rb
Class Method Summary collapse
Instance Method Summary collapse
- #base_path ⇒ Object
- #concat(another_config) ⇒ Object
-
#initialize(**args) ⇒ Config
constructor
A new instance of Config.
- #path_absolute_to_relative(absolute_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
TODO: I can’t get the relative path working.
- #set_initial_path(new_path) ⇒ Object
- #to_file(to_path = path) ⇒ Object
- #update_path(new_path) ⇒ Object
Constructor Details
#initialize(**args) ⇒ Config
Returns a new instance of Config.
13 14 15 16 |
# File 'lib/suma/schema_config/config.rb', line 13 def initialize(**args) @path = path_relative_to_absolute(path) if path super(**args) end |
Class Method Details
.from_file(path) ⇒ Object
26 27 28 29 30 |
# File 'lib/suma/schema_config/config.rb', line 26 def self.from_file(path) from_yaml(File.read(path)).tap do |x| x.set_initial_path(path) end end |
Instance Method Details
#base_path ⇒ Object
18 19 20 |
# File 'lib/suma/schema_config/config.rb', line 18 def base_path File.dirname(@path) end |
#concat(another_config) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/suma/schema_config/config.rb', line 108 def concat(another_config) unless another_config.is_a?(self.class) raise StandardError, "Can only concatenate a non SchemaConfig::Config 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) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/suma/schema_config/config.rb', line 75 def path_absolute_to_relative(absolute_path) # puts "path_absolute_to_relative 1 #{absolute_path}" # pp self # pp path # pp @hello return absolute_path unless @path relative_path = Pathname.new(absolute_path).relative_path_from(Pathname.new(@path).dirname).to_s # puts "path_absolute_to_relative x #{relative_path}" relative_path end |
#path_relative_to_absolute(relative_path) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/suma/schema_config/config.rb', line 65 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 Pathname.new(File.dirname(@path)).join(eval_path)..to_s end |
#save_to_path(filename) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/suma/schema_config/config.rb', line 122 def save_to_path(filename) new_config = dup new_config.path = filename new_config.update_base_path(File.dirname(filename)) File.open(filename, "w") do |f| Utils.log "Writing #{filename}..." f.write(to_yaml) Utils.log "Done." end end |
#schemas_from_yaml(model, value) ⇒ Object
45 46 47 48 49 |
# File 'lib/suma/schema_config/config.rb', line 45 def schemas_from_yaml(model, value) model.schemas = value.map do |k, v| Schema.new(id: k, path: path_relative_to_absolute(v["path"])) end end |
#schemas_to_yaml(model, doc) ⇒ Object
TODO: I can’t get the relative path working. The schemas-*.yaml file is meant to contain the “path” key, which is a relative path to its location, then sets the base path to each schema path, which is supposed to be relative to “path” key. Somehow, the @path variable is always missing in to_yaml…
56 57 58 59 60 61 62 63 |
# File 'lib/suma/schema_config/config.rb', line 56 def schemas_to_yaml(model, doc) # puts "^"*30 # pp self # pp @path doc["schemas"] = model.schemas.sort_by(&:id).to_h do |schema| [schema.id, { "path" => path_absolute_to_relative(schema.path) }] end end |
#set_initial_path(new_path) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/suma/schema_config/config.rb', line 38 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) end end |
#to_file(to_path = path) ⇒ Object
32 33 34 35 36 |
# File 'lib/suma/schema_config/config.rb', line 32 def to_file(to_path = path) File.open(to_path, "w") do |f| f.write(to_yaml) end end |
#update_path(new_path) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/suma/schema_config/config.rb', line 87 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) 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 new_relative_schema_path = schema_path.relative_path_from(new_base_path) schema.path = new_relative_schema_path end @path = new_path end |