Browse Source

Allow specifying database and memcache settings as blocks.

Noah Kantrowitz 12 years ago
parent
commit
b12bd65f34
2 changed files with 41 additions and 2 deletions
  1. 31 0
      libraries/default.rb
  2. 10 2
      resources/rails.rb

+ 31 - 0
libraries/default.rb

@@ -24,6 +24,23 @@ class Chef
     FORBIDDEN_IVARS.concat [:@application, :@application_provider]
     HIDDEN_IVARS.concat [:@application, :@application_provider]
 
+    class Application
+      module OptionsCollector
+        def options
+          @options ||= {}
+        end
+
+        def method_missing(method_sym, value=nil, &block)
+          super
+        rescue NameError
+          value ||= block
+          method_sym = method_sym.to_s.chomp('=').to_sym
+          options[method_sym] = value if value
+          options[method_sym] ||= nil
+        end
+      end
+    end
+
     module ApplicationBase
       def self.included(klass)
         klass.actions :before_compile, :before_deploy, :before_migrate, :before_symlink, :before_restart, :after_restart
@@ -50,6 +67,20 @@ class Chef
       def release_path
         application_provider.release_path
       end
+
+      class OptionsBlock
+        include Chef::Resource::Application::OptionsCollector
+      end
+
+      def options_block(options=nil, &block)
+        options ||= {}
+        if block
+          collector = OptionsBlock.new
+          collector.instance_eval(&block)
+          options.update(collector.options)
+        end
+        options
+      end
     end
   end
 

+ 10 - 2
resources/rails.rb

@@ -22,7 +22,15 @@ include Chef::Resource::ApplicationBase
 
 attribute :database_master_role, :kind_of => [String, NilClass], :default => nil
 attribute :memcached_role, :kind_of => [String, NilClass], :default => nil
-attribute :database, :kind_of => Hash, :default => {}
-attribute :memcached, :kind_of => Hash, :default => {}
 attribute :gems, :kind_of => [Array, Hash], :default => []
 attribute :bundler_deployment, :kind_of => [NilClass, TrueClass, FalseClass], :default => nil
+
+def database(*args, &block)
+  @database ||= Mash.new
+  @database.update(options_block(*args, &block))
+end
+
+def memcached(*args, &block)
+  @database ||= Mash.new
+  @database.update(options_block(*args, &block))
+end