Class: MendixBridge::Config
- Inherits:
-
Object
- Object
- MendixBridge::Config
- Defined in:
- lib/mendix_bridge/config.rb
Constant Summary collapse
- FILE_NAME =
".mendix-ruby.yml"- KEYS =
%w[project inventory model].freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .find(start_dir = Dir.pwd) ⇒ Object
- .load(path) ⇒ Object
- .write(path, project:, inventory:, model:, force: false) ⇒ Object
Instance Method Summary collapse
-
#initialize(path, data) ⇒ Config
constructor
A new instance of Config.
- #to_h ⇒ Object
Constructor Details
#initialize(path, data) ⇒ Config
Returns a new instance of Config.
59 60 61 62 |
# File 'lib/mendix_bridge/config.rb', line 59 def initialize(path, data) @path = path @data = data end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/mendix_bridge/config.rb', line 13 def path @path end |
Class Method Details
.find(start_dir = Dir.pwd) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mendix_bridge/config.rb', line 15 def self.find(start_dir = Dir.pwd) directory = Pathname.new(File.(start_dir)) loop do candidate = directory.join(FILE_NAME) return load(candidate.to_s) if candidate.file? break if directory.root? directory = directory.parent end nil end |
.load(path) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mendix_bridge/config.rb', line 27 def self.load(path) path = File.(path) data = YAML.safe_load_file(path, permitted_classes: [], aliases: false) raise ConfigError, "configuration root must be a mapping: #{path}" unless data.is_a?(Hash) raise ConfigError, "unsupported configuration version" unless data["version"] == 1 unknown = data.keys - ["version", *KEYS] raise ConfigError, "unknown configuration keys: #{unknown.join(', ')}" unless unknown.empty? new(path, data) rescue Psych::Exception => error raise ConfigError, "invalid configuration #{path}: #{error.}" end |
.write(path, project:, inventory:, model:, force: false) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mendix_bridge/config.rb', line 43 def self.write(path, project:, inventory:, model:, force: false) path = File.(path) raise ConfigError, "configuration already exists: #{path}" if File.exist?(path) && !force base = File.dirname(path) data = { "version" => 1, "project" => relative_path(project, base), "inventory" => relative_path(inventory, base), "model" => relative_path(model, base) } File.write(path, YAML.dump(data)) load(path) end |
Instance Method Details
#to_h ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/mendix_bridge/config.rb', line 71 def to_h { "path" => path, "project" => project, "inventory" => inventory, "model" => model } end |