#This is a sample vagrant file
Vagrant::Config.run do |config|
config.vm.box = "precise64"
- config.ssh.forward_agent = true
-
+
config.vm.provision Vagrant::Provisioners::CopyMyConf do |copy_conf|
- copy_conf.ssh = true
- copy_conf.vim = true
- copy_conf.git = true
+ copy_conf.ssh
+ copy_conf.vim
+ copy_conf.git
copy_conf.user_home = "/home/vagrant"
end
def prepare
@to_be_copied = []
- config.all_true.each do |c|
- conf = self.class.const_get(c.capitalize).new
+ config.all_enabled_attributes.each do |conf|
@to_be_copied << conf
conf.prepare env[:vm].config.vm, tmp_root
end
def self.all_attributes
[:ssh, :vim, :git]
end
- attr_accessor *all_attributes
attr_accessor :user_home
- def all_true
- self.class.all_attributes.collect do |attr|
- self.send(attr) ? attr : nil
+ all_attributes.each do |attr|
+ define_method(attr) do
+ instance_variable_get_or_set(attr, CopyMyConf.const_get("#{attr.capitalize}").new)
+ end
+ end
+
+ def all_enabled_attributes
+ all_attributes.collect do |attr|
+ instance_variable_get "@#{attr}"
end.compact
+ [@ssh, @vim, @git].compact
+ end
+
+ private
+ def all_attributes
+ self.class.all_attributes
+ end
+
+ def instance_variable_get_or_set(attr, value)
+ instance_variable_get("@#{attr}") || instance_variable_set("@#{attr}", value)
end
end
end
it "should list all the true attributes" do
config = Config.new
- config.vim = true
- config.ssh = true
+ config.vim
+ config.ssh
- all_true_attributes = config.all_true
- all_true_attributes.should =~ [:vim, :ssh]
+ all_enabled_attributes = config.all_enabled_attributes
+ all_enabled_attributes.map(&:class) =~ [Vim, Ssh]
end
end
end
end
it "should prepare provisioning process" do
- @config.should_receive(:all_true).and_return([:vim])
+ @config.should_receive(:all_enabled_attributes).and_return([CopyMyConf::Vim.new])
CopyMyConf::Vim.any_instance.should_receive(:prepare).with(@mock_vm, anything)
CopyMyConf.new.prepare
end
it "should provision the vm" do
- @config.stub(:all_true).and_return([:vim])
+ @config.stub(:all_enabled_attributes).and_return([CopyMyConf::Vim.new])
copy_my_conf = CopyMyConf.new
CopyMyConf::Vim.any_instance.stub(:prepare)