Class: Vagrant::Config::Base
- Inherits:
-
Object
- Object
- Vagrant::Config::Base
- 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.
Direct Known Subclasses
NFSConfig, PackageConfig, SSHConfig, Top, VMConfig, VagrantConfig, Provisioners::Chef::Config, Provisioners::Puppet::Config, Provisioners::PuppetServer::Config, Provisioners::Shell::Config, Systems::FreeBSD::FreeBSDConfig, Systems::Linux::LinuxConfig, Systems::Solaris::SolarisConfig
Instance Attribute Summary collapse
-
#top ⇒ Object
Top of this configuration stack.
Class Method Summary collapse
-
.configures(accessor, klass = self) ⇒ Object
Registers a subclass with the Vagrant configuration system so that it can then be used in Vagrantfiles.
-
.json_create(data) ⇒ Object
Loads configuration values from JSON back into the proper configuration classes.
Instance Method Summary collapse
-
#env ⇒ Vagrant::Envrionment
A helper to access the environment that this configuration is for.
-
#instance_variables_hash ⇒ Object
Returns the instance variables as a hash of key-value pairs.
-
#set_options(options) ⇒ Object
Allows setting options from a hash.
-
#to_hash ⇒ Object
Converts the configuration to a raw hash by calling
#to_hash
on all instance variables (if it can) and putting them into a hash. -
#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. -
#validate(errors) ⇒ Object
Called by Top after the configuration is loaded to validate the configuaration objects.
Instance Attribute Details
#top ⇒ Object
Top of this configuration stack
8 9 10 |
# File 'lib/vagrant/config/base.rb', line 8 def top @top end |
Class Method Details
.configures(accessor, klass = self) ⇒ Object
Registers a subclass with the Vagrant configuration system so that it can then be used in Vagrantfiles.
16 17 18 |
# File 'lib/vagrant/config/base.rb', line 16 def self.configures(accessor, klass=self) Top.configures(accessor, klass) end |
.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.
24 25 26 27 28 29 30 |
# File 'lib/vagrant/config/base.rb', line 24 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
#env ⇒ Vagrant::Envrionment
A helper to access the environment that this configuration is for. This is obtained by getting the env from the Top.
36 37 38 |
# File 'lib/vagrant/config/base.rb', line 36 def env top.env end |
#instance_variables_hash ⇒ Object
Returns the instance variables as a hash of key-value pairs.
77 78 79 80 81 82 |
# File 'lib/vagrant/config/base.rb', line 77 def instance_variables_hash instance_variables.inject({}) do |acc, iv| acc[iv.to_s[1..-1]] = instance_variable_get(iv) unless [:@env, :@top].include?(iv.to_sym) acc end 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.
43 44 45 46 47 |
# File 'lib/vagrant/config/base.rb', line 43 def () .each do |key, value| send("#{key}=", value) end end |
#to_hash ⇒ Object
Converts the configuration to a raw hash by calling #to_hash
on all instance variables (if it can) and putting them into
a hash.
59 60 61 62 63 64 65 66 |
# File 'lib/vagrant/config/base.rb', line 59 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.
71 72 73 74 |
# File 'lib/vagrant/config/base.rb', line 71 def to_json(*a) result = { 'json_class' => self.class.name } result.merge(instance_variables_hash).to_json(*a) end |
#validate(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.
54 |
# File 'lib/vagrant/config/base.rb', line 54 def validate(errors); end |