Class: MendixBridge::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (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.expand_path(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.expand_path(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.message}"
end

.write(path, project:, inventory:, model:, force: false) ⇒ Object

Raises:



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.expand_path(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_hObject



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