Class: Hocho::Config

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

Constant Summary collapse

DEFAULT_INVENTORY_PROVIDERS_CONFIG =
[file: {path: './hosts.yml'}]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, base_dir: '.') ⇒ Config

Returns a new instance of Config.



15
16
17
18
# File 'lib/hocho/config.rb', line 15

def initialize(hash, base_dir: '.')
  @config = Hocho::Utils::Symbolize.keys_of(hash)
  @base_dir = Pathname(base_dir)
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



20
21
22
# File 'lib/hocho/config.rb', line 20

def base_dir
  @base_dir
end

Class Method Details

.load(path) ⇒ Object



11
12
13
# File 'lib/hocho/config.rb', line 11

def self.load(path)
  new YAML.load_file(path.to_s), base_dir: File.dirname(path.to_s)
end

Instance Method Details

#[](k) ⇒ Object



22
23
24
# File 'lib/hocho/config.rb', line 22

def [](k)
  @config[k]
end

#inventory_providersObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hocho/config.rb', line 26

def inventory_providers
  @inventory_providers ||= begin
    provider_specs = (@config[:inventory_providers] || DEFAULT_INVENTORY_PROVIDERS_CONFIG)
    if provider_specs.kind_of?(Hash)
      provider_specs = [provider_specs]
    end

    provider_specs.flat_map do |spec|
      raise TypeError, 'config inventory_providers[] should be an Hash' unless spec.kind_of?(Hash)
      spec.map do |name, options|
        InventoryProviders.find(name).new(**options)
      end
    end
  end
end

#property_providersObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hocho/config.rb', line 42

def property_providers
  @property_providers ||= begin
    provider_specs = (@config[:property_providers] || [])
    raise TypeError, 'config property_providers should be an Array' unless provider_specs.kind_of?(Array)
    provider_specs.flat_map do |spec|
      raise TypeError, 'config property_providers[] should be an Hash' unless spec.kind_of?(Hash)
      spec.map do |name, options|
        PropertyProviders.find(name).new(**options)
      end
    end
  end
end