From 3af8352664ba470b97728c16fe4a35809b1fa405 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Sat, 7 Jan 2006 21:02:25 +0000 Subject: [PATCH] Make UPPERCASE variables work git-svn-id: http://svn.rubyonrails.org/rails/tools/switchtower@3385 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- CHANGELOG | 6 ++++-- lib/switchtower/actor.rb | 9 +++++---- lib/switchtower/configuration.rb | 12 ++++++++++++ test/actor_test.rb | 9 +++++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index fa05319b..9e7e33bc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,11 +1,13 @@ *0.11.0* *SVN* +* Make UPPERCASE variables work + +* Added rails_env variable (defaults to production) for use by tasks that employ the RAILS_ENV environment variable + * Added Actor.default_io_proc * Set :actor key on SSH channel instances -* Added rails_env variable (defaults to production) for use by tasks that employ the RAILS_ENV environment variable - *0.10.0* (January 2nd, 2006) diff --git a/lib/switchtower/actor.rb b/lib/switchtower/actor.rb index 145145a7..12318897 100644 --- a/lib/switchtower/actor.rb +++ b/lib/switchtower/actor.rb @@ -326,11 +326,11 @@ module SwitchTower self.class.default_io_proc end - private + def metaclass + class << self; self; end + end - def metaclass - class << self; self; end - end + private def define_method(name, &block) metaclass.send(:define_method, name, &block) @@ -382,5 +382,6 @@ module SwitchTower super end end + end end diff --git a/lib/switchtower/configuration.rb b/lib/switchtower/configuration.rb index 25405d11..65637df3 100644 --- a/lib/switchtower/configuration.rb +++ b/lib/switchtower/configuration.rb @@ -29,6 +29,9 @@ module SwitchTower # determining the release path. attr_reader :now + # The has of variables currently known by the configuration + attr_reader :variables + def initialize(actor_class=Actor) #:nodoc: @roles = Hash.new { |h,k| h[k] = [] } @actor = actor_class.new(self) @@ -60,6 +63,15 @@ module SwitchTower # Set a variable to the given value. def set(variable, value) + # if the variable is uppercase, then we add it as a constant to the + # actor. This is to allow uppercase "variables" to be set and referenced + # in recipes. + if variable.to_s[0].between?(?A, ?Z) + klass = @actor.metaclass + klass.send(:remove_const, variable) if klass.const_defined?(variable) + klass.const_set(variable, value) + end + @variables[variable] = value end diff --git a/test/actor_test.rb b/test/actor_test.rb index ed657ee2..b8bbc0f7 100644 --- a/test/actor_test.rb +++ b/test/actor_test.rb @@ -4,6 +4,7 @@ require 'stringio' require 'test/unit' require 'switchtower/actor' require 'switchtower/logger' +require 'switchtower/configuration' class ActorTest < Test::Unit::TestCase @@ -258,4 +259,12 @@ class ActorTest < Test::Unit::TestCase @actor.foo assert_equal %w(foo after_foo), history end + + def test_uppercase_variables + config = SwitchTower::Configuration.new(TestActor) + config.set :HELLO, "world" + assert_equal "world", config.actor.instance_eval("HELLO") + config.set :HELLO, "test" + assert_equal "test", config.actor.instance_eval("HELLO") + end end