Permalink
Browse files

Add Cuba.settings.

- It's inheritable, a hash, and is accessible via
  #settings.
  • Loading branch information...
cyx committed Feb 14, 2012
1 parent 2cfd1db commit c7e6d43e3dcbf8505927365df3da7147bff8e84a
Showing with 51 additions and 0 deletions.
  1. +12 −0 lib/cuba.rb
  2. +39 −0 test/settings.rb
View
@@ -50,6 +50,14 @@ def self.plugin(mixin)
mixin.setup(self) if mixin.respond_to?(:setup)
end
+ def self.settings
+ @settings ||= {}
+ end
+
+ def self.inherited(child)
+ child.settings.replace(settings)
+ end
+
attr :env
attr :req
attr :res
@@ -60,6 +68,10 @@ def initialize(&blk)
@captures = []
end
+ def settings
+ self.class.settings
+ end
+
def call(env)
dup.call!(env)
end
View
@@ -0,0 +1,39 @@
+require File.expand_path("helper", File.dirname(__FILE__))
+
+test "settings is an empty hash by default" do
+ assert Cuba.settings.kind_of?(Hash)
+ assert Cuba.settings.empty?
+end
+
+test "is inheritable and allows overriding" do
+ Cuba.settings[:foo] = "bar"
+
+ class Admin < Cuba; end
+
+ assert_equal "bar", Admin.settings[:foo]
+
+ Admin.settings[:foo] = "baz"
+
+ assert_equal "bar", Cuba.settings[:foo]
+ assert_equal "baz", Admin.settings[:foo]
+end
+
+test do
+ Cuba.settings[:hello] = "Hello World"
+
+ Cuba.define do
+ on default do
+ res.write settings[:hello]
+ end
+ end
+
+ _, _, resp = Cuba.call({ "PATH_INFO" => "/", "SCRIPT_NAME" => ""})
+
+ body = []
+
+ resp.each do |line|
+ body << line
+ end
+
+ assert_equal ["Hello World"], body
+end

0 comments on commit c7e6d43

Please sign in to comment.