Move Config::V1::Base to Vagrant::Plugin::V1::Config
This commit is contained in:
parent
ffab8cab68
commit
41bc8e7454
|
@ -1,82 +0,0 @@
|
|||
module Vagrant
|
||||
module Config
|
||||
# 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 Base
|
||||
# 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.
|
||||
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
|
||||
|
||||
# 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.
|
||||
def set_options(options)
|
||||
options.each do |key, value|
|
||||
send("#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
# Merge another configuration object into this one.
|
||||
#
|
||||
# @param [Object] other The other configuration object to merge from,
|
||||
# this must be the same type of object as this one.
|
||||
# @return [Object] The merged object.
|
||||
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
|
||||
|
||||
# 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.
|
||||
#
|
||||
# @param [ErrorRecorder] errors
|
||||
def validate(env, errors); end
|
||||
|
||||
# Converts the configuration to a raw hash by calling `#to_hash`
|
||||
# on all instance variables (if it can) and putting them into
|
||||
# a hash.
|
||||
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
|
||||
|
||||
# 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}.
|
||||
def to_json(*a)
|
||||
instance_variables_hash.to_json(*a)
|
||||
end
|
||||
|
||||
# Returns the instance variables as a hash of key-value pairs.
|
||||
def instance_variables_hash
|
||||
instance_variables.inject({}) do |acc, iv|
|
||||
acc[iv.to_s[1..-1]] = instance_variable_get(iv)
|
||||
acc
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,4 @@
|
|||
require "vagrant/config/version_base"
|
||||
require "vagrant/config/v1/base"
|
||||
require "vagrant/config/v1/root"
|
||||
|
||||
module Vagrant
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
module Vagrant
|
||||
module Config
|
||||
class V1 < VersionBase
|
||||
# Base class for configuration keys. It is not required to inherit
|
||||
# from this class but this class provides useful helpers that config
|
||||
# classes may wish to use.
|
||||
class Base
|
||||
# This is called as a last-minute hook that allows the configuration
|
||||
# object to finalize itself before it will be put into use. This is
|
||||
# a useful place to do some defaults in the case the user didn't
|
||||
# configure something or so on.
|
||||
#
|
||||
# The configuration object is expected to mutate itself.
|
||||
def finalize!
|
||||
end
|
||||
|
||||
# Merge another configuration object into this one. This assumes that
|
||||
# the other object is the same class as this one.
|
||||
#
|
||||
# @param [Object] other The other configuration object to merge from,
|
||||
# this must be the same type of object as this one.
|
||||
# @return [Object] The merged object.
|
||||
def merge(other)
|
||||
result = self.class.new
|
||||
|
||||
# Set all of our instance variables on the new class
|
||||
[self, other].each do |obj|
|
||||
obj.instance_variables.each do |key|
|
||||
# Ignore keys that start with a double underscore. This allows
|
||||
# configuration classes to still hold around internal state
|
||||
# that isn't propagated.
|
||||
if !key.to_s.start_with?("__")
|
||||
result.instance_variable_set(key, obj.instance_variable_get(key))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
# Called by {Root} after the configuration is loaded to validate
|
||||
# the configuaration objects. Subclasses should implement this
|
||||
# method and add any errors to the `errors` object given.
|
||||
#
|
||||
# @param [ErrorRecorder] errors
|
||||
def validate(env, errors); end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,6 +5,7 @@ require "vagrant/plugin/v1/errors"
|
|||
module Vagrant
|
||||
module Plugin
|
||||
module V1
|
||||
autoload :Config, "vagrant/plugin/v1/config"
|
||||
autoload :Plugin, "vagrant/plugin/v1/plugin"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
module Vagrant
|
||||
module Plugin
|
||||
module V1
|
||||
# This is the base class for a configuration key defined for
|
||||
# V1. Any configuration key plugins for V1 should inherit from this
|
||||
# class.
|
||||
class Config
|
||||
# This is called as a last-minute hook that allows the configuration
|
||||
# object to finalize itself before it will be put into use. This is
|
||||
# a useful place to do some defaults in the case the user didn't
|
||||
# configure something or so on.
|
||||
#
|
||||
# An example of where this sort of thing is used or has been used:
|
||||
# the "vm" configuration key uses this to make sure that at least
|
||||
# one sub-VM has been defined: the default VM.
|
||||
#
|
||||
# The configuration object is expected to mutate itself.
|
||||
def finalize!
|
||||
# Default implementation is to do nothing.
|
||||
end
|
||||
|
||||
# Merge another configuration object into this one. This assumes that
|
||||
# the other object is the same class as this one. This should not
|
||||
# mutate this object, but instead should return a new, merged object.
|
||||
#
|
||||
# The default implementation will simply iterate over the instance
|
||||
# variables and merge them together, with this object overriding
|
||||
# any conflicting instance variables of the older object. Instance
|
||||
# variables starting with "__" (double underscores) will be ignored.
|
||||
# This lets you set some sort of instance-specific state on your
|
||||
# configuration keys without them being merged together later.
|
||||
#
|
||||
# @param [Object] other The other configuration object to merge from,
|
||||
# this must be the same type of object as this one.
|
||||
# @return [Object] The merged object.
|
||||
def merge(other)
|
||||
result = self.class.new
|
||||
|
||||
# Set all of our instance variables on the new class
|
||||
[self, other].each do |obj|
|
||||
obj.instance_variables.each do |key|
|
||||
# Ignore keys that start with a double underscore. This allows
|
||||
# configuration classes to still hold around internal state
|
||||
# that isn't propagated.
|
||||
if !key.to_s.start_with?("@__")
|
||||
result.instance_variable_set(key, obj.instance_variable_get(key))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
# 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.
|
||||
#
|
||||
# This is expected to mutate itself.
|
||||
#
|
||||
# @param [Hash] options A hash of options to set on this configuration
|
||||
# key.
|
||||
def set_options(options)
|
||||
options.each do |key, value|
|
||||
send("#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
# Converts this configuration object to JSON.
|
||||
def to_json(*a)
|
||||
instance_variables_hash.to_json(*a)
|
||||
end
|
||||
|
||||
# Returns the instance variables as a hash of key-value pairs.
|
||||
def instance_variables_hash
|
||||
instance_variables.inject({}) do |acc, iv|
|
||||
acc[iv.to_s[1..-1]] = instance_variable_get(iv)
|
||||
acc
|
||||
end
|
||||
end
|
||||
|
||||
# Called after the configuration is finalized and loaded to validate
|
||||
# this object.
|
||||
#
|
||||
# @param [Environment] env Vagrant::Environment object of the
|
||||
# environment that this configuration has been loaded into. This
|
||||
# gives you convenient access to things like the the root path
|
||||
# and so on.
|
||||
# @param [ErrorRecorder] errors
|
||||
def validate(env, errors)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
module VagrantPlugins
|
||||
module GuestFreeBSD
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :halt_timeout
|
||||
attr_accessor :halt_check_interval
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module VagrantPlugins
|
||||
module GuestLinux
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :halt_timeout
|
||||
attr_accessor :halt_check_interval
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module VagrantPlugins
|
||||
module GuestSolaris
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :halt_timeout
|
||||
attr_accessor :halt_check_interval
|
||||
# This sets the command to use to execute items as a superuser. sudo is default
|
||||
|
|
|
@ -2,7 +2,7 @@ require "vagrant"
|
|||
|
||||
module VagrantPlugins
|
||||
module Kernel_V1
|
||||
class NFSConfig < Vagrant::Config::V1::Base
|
||||
class NFSConfig < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :map_uid
|
||||
attr_accessor :map_gid
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ require "vagrant"
|
|||
|
||||
module VagrantPlugins
|
||||
module Kernel_V1
|
||||
class PackageConfig < Vagrant::Config::V1::Base
|
||||
class PackageConfig < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :name
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ require "vagrant"
|
|||
|
||||
module VagrantPlugins
|
||||
module Kernel_V1
|
||||
class SSHConfig < Vagrant::Config::V1::Base
|
||||
class SSHConfig < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :username
|
||||
attr_accessor :password
|
||||
attr_accessor :host
|
||||
|
|
|
@ -2,7 +2,7 @@ require "vagrant"
|
|||
|
||||
module VagrantPlugins
|
||||
module Kernel_V1
|
||||
class VagrantConfig < Vagrant::Config::V1::Base
|
||||
class VagrantConfig < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :dotfile_name
|
||||
attr_accessor :host
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ require File.expand_path("../vm_subvm", __FILE__)
|
|||
|
||||
module VagrantPlugins
|
||||
module Kernel_V1
|
||||
class VMConfig < Vagrant::Config::V1::Base
|
||||
class VMConfig < Vagrant::Plugin::V1::Config
|
||||
DEFAULT_VM_NAME = :default
|
||||
|
||||
attr_accessor :name
|
||||
|
|
|
@ -83,7 +83,7 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
# This is the configuration which is available through `config.chef`
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
# Shared config
|
||||
attr_accessor :node_name
|
||||
attr_accessor :provisioning_path
|
||||
|
|
|
@ -8,7 +8,7 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
class Puppet < Vagrant::Provisioners::Base
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :manifest_file
|
||||
attr_accessor :manifests_path
|
||||
attr_accessor :module_path
|
||||
|
@ -120,7 +120,7 @@ module VagrantPlugins
|
|||
def set_module_paths
|
||||
@module_paths = []
|
||||
@expanded_module_paths.each_with_index do |path, i|
|
||||
@module_paths << [path, File.join(config.pp_path, "modules-#{i}"]
|
||||
@module_paths << [path, File.join(config.pp_path, "modules-#{i}")]
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ module VagrantPlugins
|
|||
end
|
||||
|
||||
class PuppetServer < Base
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :puppet_server
|
||||
attr_accessor :puppet_node
|
||||
attr_accessor :options
|
||||
|
|
|
@ -4,7 +4,7 @@ require "tempfile"
|
|||
module VagrantPlugins
|
||||
module Shell
|
||||
class Provisioner < Vagrant::Provisioners::Base
|
||||
class Config < Vagrant::Config::Base
|
||||
class Config < Vagrant::Plugin::V1::Config
|
||||
attr_accessor :inline
|
||||
attr_accessor :path
|
||||
attr_accessor :upload_path
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
require File.expand_path("../../../base", __FILE__)
|
||||
|
||||
describe Vagrant::Config::Base do
|
||||
include_context "unit"
|
||||
|
||||
let(:foo_class) do
|
||||
Class.new(described_class) do
|
||||
attr_accessor :one
|
||||
attr_accessor :two
|
||||
end
|
||||
end
|
||||
|
||||
it "should merge by default by simply copying each instance variable" do
|
||||
one = foo_class.new
|
||||
one.one = 2
|
||||
one.two = 1
|
||||
|
||||
two = foo_class.new
|
||||
two.two = 5
|
||||
|
||||
result = one.merge(two)
|
||||
result.one.should == 2
|
||||
result.two.should == 5
|
||||
end
|
||||
|
||||
it "doesn't merge values that start with a double underscore" do
|
||||
bar_class = Class.new(foo_class) do
|
||||
class_variable_set(:@@counter, 0)
|
||||
|
||||
def initialize
|
||||
@__test = self.class.send(:class_variable_get, :@@counter)
|
||||
self.class.send(:class_variable_set, :@@counter, @__test + 1)
|
||||
end
|
||||
end
|
||||
|
||||
one = bar_class.new
|
||||
one.one = 2
|
||||
one.two = 1
|
||||
|
||||
two = bar_class.new
|
||||
two.two = 5
|
||||
|
||||
# Verify the counters
|
||||
one.instance_variable_get(:@__test).should == 0
|
||||
two.instance_variable_get(:@__test).should == 1
|
||||
one.merge(two).instance_variable_get(:@__test).should == 2
|
||||
end
|
||||
end
|
|
@ -0,0 +1,44 @@
|
|||
require File.expand_path("../../../../base", __FILE__)
|
||||
|
||||
describe Vagrant::Plugin::V1::Config do
|
||||
include_context "unit"
|
||||
|
||||
let(:foo_class) do
|
||||
Class.new(described_class) do
|
||||
attr_accessor :one
|
||||
attr_accessor :two
|
||||
end
|
||||
end
|
||||
|
||||
describe "merging" do
|
||||
it "should merge by default by simply copying each instance variable" do
|
||||
one = foo_class.new
|
||||
one.one = 2
|
||||
one.two = 1
|
||||
|
||||
two = foo_class.new
|
||||
two.two = 5
|
||||
|
||||
result = one.merge(two)
|
||||
result.one.should == 2
|
||||
result.two.should == 5
|
||||
end
|
||||
|
||||
it "doesn't merge values that start with a double underscore" do
|
||||
one = foo_class.new
|
||||
one.one = 1
|
||||
one.two = 1
|
||||
one.instance_variable_set(:@__bar, "one")
|
||||
|
||||
two = foo_class.new
|
||||
two.two = 2
|
||||
two.instance_variable_set(:@__bar, "two")
|
||||
|
||||
# Merge and verify
|
||||
result = one.merge(two)
|
||||
result.one.should == 1
|
||||
result.two.should == 2
|
||||
result.instance_variable_get(:@__bar).should be_nil
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue