Provisioner objects created by config
authorAkshay Mankar <akshaym@thoughtworks.com>
Sat, 12 Jan 2013 07:23:44 +0000 (12:53 +0530)
committerAkshay Mankar <akshaym@thoughtworks.com>
Sat, 12 Jan 2013 07:23:44 +0000 (12:53 +0530)
example/Vagrantfile
lib/copy_my_conf.rb
lib/copy_my_conf/config.rb
spec/copy_my_conf/config_spec.rb
spec/copy_my_conf_spec.rb

index 8aa5f3f..07b89ee 100644 (file)
@@ -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
 
index 8f2b889..7cde153 100644 (file)
@@ -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
index b077607..23f150e 100644 (file)
@@ -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
index 4a2ffdc..5e1334d 100644 (file)
@@ -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
index b471ff3..10b0247 100644 (file)
@@ -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)