From 2254852606b5104c60f643724a827701ae0c9c81 Mon Sep 17 00:00:00 2001 From: Akshay Mankar Date: Sat, 12 Jan 2013 12:53:44 +0530 Subject: [PATCH] Provisioner objects created by config --- example/Vagrantfile | 9 ++++----- lib/copy_my_conf.rb | 3 +-- lib/copy_my_conf/config.rb | 23 +++++++++++++++++++---- spec/copy_my_conf/config_spec.rb | 8 ++++---- spec/copy_my_conf_spec.rb | 4 ++-- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/example/Vagrantfile b/example/Vagrantfile index 8aa5f3f..07b89ee 100644 --- a/example/Vagrantfile +++ b/example/Vagrantfile @@ -5,12 +5,11 @@ require 'copy_my_conf' #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 diff --git a/lib/copy_my_conf.rb b/lib/copy_my_conf.rb index 8f2b889..7cde153 100644 --- a/lib/copy_my_conf.rb +++ b/lib/copy_my_conf.rb @@ -9,8 +9,7 @@ module Vagrant 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 diff --git a/lib/copy_my_conf/config.rb b/lib/copy_my_conf/config.rb index b077607..23f150e 100644 --- a/lib/copy_my_conf/config.rb +++ b/lib/copy_my_conf/config.rb @@ -5,13 +5,28 @@ module Vagrant 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 diff --git a/spec/copy_my_conf/config_spec.rb b/spec/copy_my_conf/config_spec.rb index 4a2ffdc..5e1334d 100644 --- a/spec/copy_my_conf/config_spec.rb +++ b/spec/copy_my_conf/config_spec.rb @@ -5,11 +5,11 @@ module Vagrant::Provisioners 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 diff --git a/spec/copy_my_conf_spec.rb b/spec/copy_my_conf_spec.rb index b471ff3..10b0247 100644 --- a/spec/copy_my_conf_spec.rb +++ b/spec/copy_my_conf_spec.rb @@ -18,14 +18,14 @@ module Vagrant 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) -- 2.39.2