Class: Mortar::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mortar/config.rb

Defined Under Namespace

Classes: ConfigError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables: {}, overlays: [], labels: {}) ⇒ Config

Returns a new instance of Config.



25
26
27
28
29
# File 'lib/mortar/config.rb', line 25

def initialize(variables: {}, overlays: [], labels: {})
  @variables = variables
  @overlays = overlays
  @labels = labels
end

Class Method Details

.load(path) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mortar/config.rb', line 7

def self.load(path)
  cfg = YAML.safe_load(File.read(path))

  raise ConfigError, "Failed to load config, check config file syntax" unless cfg.is_a? Hash
  raise ConfigError, "Failed to load config, overlays needs to be an array" if cfg.key?('overlays') && !cfg['overlays'].is_a?(Array)

  if cfg.key?('labels')
    raise ConfigError, "Failed to load config, labels needs to be a hash" if !cfg['labels'].is_a?(Hash)
    raise ConfigError, "Failed to load config, label values need to be strings" if cfg['labels'].values.any? { |value| !value.is_a?(String) }
  end

  new(
    variables: cfg['variables'] || {},
    overlays: cfg['overlays'] || [],
    labels: cfg['labels'] || {}
  )
end

Instance Method Details

#labels(other = {}) ⇒ RecursiveOpenStruct

Parameters:

  • other (Hash) (defaults to: {})

Returns:

  • (RecursiveOpenStruct)


49
50
51
52
53
# File 'lib/mortar/config.rb', line 49

def labels(other = {})
  hash = @labels.dup
  hash.merge!(other)
  RecursiveOpenStruct.new(hash, preserve_original_keys: true)
end

#overlays(other = []) ⇒ Array<Object>

Parameters:

  • other (Array<Object>) (defaults to: [])

Returns:

  • (Array<Object>)


41
42
43
44
45
# File 'lib/mortar/config.rb', line 41

def overlays(other = [])
  return @overlays unless other

  (@overlays + other).uniq.compact
end

#variables(other = {}) ⇒ RecursiveOpenStruct

Parameters:

  • other (Hash) (defaults to: {})

Returns:

  • (RecursiveOpenStruct)


33
34
35
36
37
# File 'lib/mortar/config.rb', line 33

def variables(other = {})
  hash = @variables.dup
  hash.deep_merge!(other)
  RecursiveOpenStruct.new(hash, recurse_over_arrays: true)
end