Class: VagrantPlugins::Dotvm::Config::AbstractConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dotvm/config/abstractconfig.rb

Instance Method Summary collapse

Instance Method Details

#convert_array(data, type) ⇒ Array

Converts array of hashes into array of specialized objects.

Parameters:

  • data (Array)

    Array of hashes

  • type (String)

    Specialized class name

Returns:

  • (Array)

    Array of specialized objects.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-dotvm/config/abstractconfig.rb', line 31

def convert_array(data, type)
  result = []

  data.to_a.each do |item|
    object = Object.const_get(type).new
    object.populate item
    result << object
  end

  result
end

#ensure_type(value, type, name = '') ⇒ Object



21
22
23
24
# File 'lib/vagrant-dotvm/config/abstractconfig.rb', line 21

def ensure_type(value, type, name = '')
  return true if value.is_a?(type) || value.is_a?(NilClass)
  fail InvalidConfigError.new, "'#{name}' must be #{type.name}."
end

#populate(data) ⇒ Object

Populates current object with provided data.

Parameters:

  • data (Hash)

    Hash with data



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vagrant-dotvm/config/abstractconfig.rb', line 8

def populate(data)
  data.each do |key, value|
    unless respond_to? "#{key}="
      fail(
        InvalidConfigError.new,
        "Invalid configuration option: #{key}."
      )
    end

    send("#{key}=", value)
  end
end