Class: Vagrant::Config::Base

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

Overview

The base class for all configuration classes. This implements basic things such as the environment instance variable which all config classes need as well as a basic to_json implementation.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.json_create(data) ⇒ Object

Loads configuration values from JSON back into the proper configuration classes. By default, this is done by simply iterating over all values in the JSON hash and assigning them to instance variables on the class.



11
12
13
14
15
16
17
# File 'lib/vagrant/config/base.rb', line 11

def self.json_create(data)
  data.inject(new) do |result, data|
    key, value = data
    result.instance_variable_set("@#{key}".to_sym, value) if key != "json_class"
    result
  end
end

Instance Method Details

#instance_variables_hashObject

Returns the instance variables as a hash of key-value pairs.



74
75
76
77
78
79
# File 'lib/vagrant/config/base.rb', line 74

def instance_variables_hash
  instance_variables.inject({}) do |acc, iv|
    acc[iv.to_s[1..-1]] = instance_variable_get(iv)
    acc
  end
end

#merge(other) ⇒ Object

Merge another configuration object into this one.

Parameters:

  • other (Object)

    The other configuration object to merge from, this must be the same type of object as this one.

Returns:

  • (Object)

    The merged object.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant/config/base.rb', line 33

def merge(other)
  result = self.class.new
  instance_variables_hash.merge(other.instance_variables_hash).each do |key, value|
    # Ignore keys that start with a double underscore. This allows
    # configuration classes to still hold around internal state
    # that isn't propagated.
    if !key.start_with?("__")
      result.instance_variable_set("@#{key}".to_sym, value)
    end
  end

  result
end

#set_options(options) ⇒ Object

Allows setting options from a hash. By default this simply calls the #{key}= method on the config class with the value, which is the expected behavior most of the time.



22
23
24
25
26
# File 'lib/vagrant/config/base.rb', line 22

def set_options(options)
  options.each do |key, value|
    send("#{key}=", value)
  end
end

#to_hashObject

Converts the configuration to a raw hash by calling #to_hash on all instance variables (if it can) and putting them into a hash.



57
58
59
60
61
62
63
64
# File 'lib/vagrant/config/base.rb', line 57

def to_hash
  instance_variables_hash.inject({}) do |acc, data|
    k,v = data
    v = v.to_hash if v.respond_to?(:to_hash)
    acc[k] = v
    acc
  end
end

#to_json(*a) ⇒ Object

Converts to JSON, with the json_class field set so that when the JSON is parsed back, it can be loaded back into the proper class. See json_create.



69
70
71
# File 'lib/vagrant/config/base.rb', line 69

def to_json(*a)
  instance_variables_hash.to_json(*a)
end

#validate(env, errors) ⇒ Object

Called by Top after the configuration is loaded to validate the configuaration objects. Subclasses should implement this method and add any errors to the errors object given.

Parameters:



52
# File 'lib/vagrant/config/base.rb', line 52

def validate(env, errors); end