Module: VagrantBolt::Util::Config

Defined in:
lib/vagrant-bolt/util/config.rb

Class Method Summary collapse

Class Method Details

.full_path(path, root_path) ⇒ String

Convert a path to the absolute path if it is relative

Parameters:

  • path (String)

    The path to convert

  • root_path (Object)

    The root path to append

Returns:

  • (String)

    The absolute path or nil if path is nil



40
41
42
43
44
# File 'lib/vagrant-bolt/util/config.rb', line 40

def self.full_path(path, root_path)
  return path if path.nil? || root_path.nil?

  (Pathname.new path).absolute? ? path : File.expand_path(path, root_path)
end

.merge_config(local, other) ⇒ Object

Merge config objects overriding Nil and UNSET_VALUE Since the configs have been finalized they will have nil values Arrays will be merged and override parent non arrays instead of UNSET_VALUE

Parameters:

  • local (Object)

    The local config object

  • other (Object)

    The other config object

Returns:

  • (Object)

    A merged result with local overriding other



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-bolt/util/config.rb', line 16

def self.merge_config(local, other)
  result = local.class.new
  [other, local].each do |obj|
    obj.instance_variables.each do |key|
      value = obj.instance_variable_get(key)
      case value
      when Array
        res_value = result.instance_variable_get(key)
        value = (value + res_value).uniq if res_value.is_a? Array
      when Hash
        res_value = result.instance_variable_get(key)
        value = res_value.merge(value) if res_value.is_a? Hash
      end
      result.instance_variable_set(key, value) if value != Vagrant::Plugin::V2::Config::UNSET_VALUE && !value.nil?
    end
  end
  result.finalize!
  result
end

.relative_path(path, root_path) ⇒ String

Convert a path to the relative path from the current directory

Parameters:

  • path (String)

    The path to convert

  • root_path (Object)

    The root path to append

Returns:

  • (String)

    The relative path or nil if path is nil



50
51
52
53
54
55
# File 'lib/vagrant-bolt/util/config.rb', line 50

def self.relative_path(path, root_path)
  return path if path.nil?

  absolute_path = Pathname.new full_path(path, root_path)
  absolute_path.relative_path_from(Pathname.getwd).to_s
end