hassox-pancake 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +95 -0
  3. data/Rakefile +56 -0
  4. data/TODO +18 -0
  5. data/bin/jeweler +19 -0
  6. data/bin/pancake-gen +17 -0
  7. data/bin/rubyforge +19 -0
  8. data/lib/pancake.rb +48 -0
  9. data/lib/pancake/bootloaders.rb +180 -0
  10. data/lib/pancake/configuration.rb +140 -0
  11. data/lib/pancake/core_ext/class.rb +44 -0
  12. data/lib/pancake/core_ext/object.rb +22 -0
  13. data/lib/pancake/core_ext/symbol.rb +15 -0
  14. data/lib/pancake/errors.rb +61 -0
  15. data/lib/pancake/generators.rb +8 -0
  16. data/lib/pancake/generators/base.rb +12 -0
  17. data/lib/pancake/generators/flat_generator.rb +17 -0
  18. data/lib/pancake/generators/short_generator.rb +17 -0
  19. data/lib/pancake/generators/stack_generator.rb +17 -0
  20. data/lib/pancake/generators/templates/common/dotgitignore +22 -0
  21. data/lib/pancake/generators/templates/common/dothtaccess +17 -0
  22. data/lib/pancake/generators/templates/flat/%stack_name%/%stack_name%.rb.tt +8 -0
  23. data/lib/pancake/generators/templates/flat/%stack_name%/.gitignore +21 -0
  24. data/lib/pancake/generators/templates/flat/%stack_name%/config.ru.tt +12 -0
  25. data/lib/pancake/generators/templates/flat/%stack_name%/pancake.init.tt +1 -0
  26. data/lib/pancake/generators/templates/flat/%stack_name%/public/.empty_directory +0 -0
  27. data/lib/pancake/generators/templates/flat/%stack_name%/tmp/.empty_directory +0 -0
  28. data/lib/pancake/generators/templates/short/%stack_name%/.gitignore +21 -0
  29. data/lib/pancake/generators/templates/short/%stack_name%/LICENSE.tt +20 -0
  30. data/lib/pancake/generators/templates/short/%stack_name%/README.tt +7 -0
  31. data/lib/pancake/generators/templates/short/%stack_name%/Rakefile.tt +48 -0
  32. data/lib/pancake/generators/templates/short/%stack_name%/VERSION.tt +1 -0
  33. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%.rb.tt +5 -0
  34. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/%stack_name%.rb.tt +6 -0
  35. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/config.ru.tt +12 -0
  36. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/gems/cache/.empty_directory +0 -0
  37. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/mounts/.empty_directory +0 -0
  38. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/public/.empty_directory +0 -0
  39. data/lib/pancake/generators/templates/short/%stack_name%/lib/%stack_name%/tmp/.empty_directory +0 -0
  40. data/lib/pancake/generators/templates/short/%stack_name%/pancake.init.tt +1 -0
  41. data/lib/pancake/generators/templates/short/%stack_name%/spec/%stack_name%_spec.rb.tt +7 -0
  42. data/lib/pancake/generators/templates/short/%stack_name%/spec/spec_helper.rb.tt +9 -0
  43. data/lib/pancake/generators/templates/stack/%stack_name%/.gitignore +21 -0
  44. data/lib/pancake/generators/templates/stack/%stack_name%/LICENSE.tt +20 -0
  45. data/lib/pancake/generators/templates/stack/%stack_name%/README.tt +7 -0
  46. data/lib/pancake/generators/templates/stack/%stack_name%/Rakefile.tt +48 -0
  47. data/lib/pancake/generators/templates/stack/%stack_name%/VERSION.tt +1 -0
  48. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%.rb.tt +3 -0
  49. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config.ru.tt +12 -0
  50. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/environments/development.rb.tt +18 -0
  51. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/environments/production.rb.tt +18 -0
  52. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/config/router.rb.tt +6 -0
  53. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/gems/cache/.empty_directory +0 -0
  54. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/mounts/.empty_directory +0 -0
  55. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/public/.empty_directory +0 -0
  56. data/lib/pancake/generators/templates/stack/%stack_name%/lib/%stack_name%/tmp/.empty_directory +0 -0
  57. data/lib/pancake/generators/templates/stack/%stack_name%/pancake.init.tt +1 -0
  58. data/lib/pancake/generators/templates/stack/%stack_name%/spec/%stack_name%_spec.rb.tt +7 -0
  59. data/lib/pancake/generators/templates/stack/%stack_name%/spec/spec_helper.rb.tt +9 -0
  60. data/lib/pancake/hooks/inheritable_inner_classes.rb +60 -0
  61. data/lib/pancake/hooks/on_inherit.rb +34 -0
  62. data/lib/pancake/master.rb +87 -0
  63. data/lib/pancake/middleware.rb +354 -0
  64. data/lib/pancake/mime_types.rb +265 -0
  65. data/lib/pancake/mixins/publish.rb +125 -0
  66. data/lib/pancake/mixins/publish/action_options.rb +104 -0
  67. data/lib/pancake/mixins/render.rb +61 -0
  68. data/lib/pancake/mixins/request_helper.rb +92 -0
  69. data/lib/pancake/mixins/stack_helper.rb +44 -0
  70. data/lib/pancake/mixins/url.rb +10 -0
  71. data/lib/pancake/more/controller.rb +4 -0
  72. data/lib/pancake/more/controller/base.rb +34 -0
  73. data/lib/pancake/paths.rb +218 -0
  74. data/lib/pancake/router.rb +99 -0
  75. data/lib/pancake/stack/app.rb +10 -0
  76. data/lib/pancake/stack/bootloader.rb +79 -0
  77. data/lib/pancake/stack/configuration.rb +44 -0
  78. data/lib/pancake/stack/middleware.rb +0 -0
  79. data/lib/pancake/stack/router.rb +18 -0
  80. data/lib/pancake/stack/stack.rb +57 -0
  81. data/lib/pancake/stacks/short.rb +2 -0
  82. data/lib/pancake/stacks/short/controller.rb +105 -0
  83. data/lib/pancake/stacks/short/stack.rb +194 -0
  84. data/spec/helpers/helpers.rb +20 -0
  85. data/spec/helpers/matchers.rb +25 -0
  86. data/spec/pancake/bootloaders_spec.rb +109 -0
  87. data/spec/pancake/configuration_spec.rb +177 -0
  88. data/spec/pancake/fixtures/foo_stack/pancake.init +0 -0
  89. data/spec/pancake/fixtures/paths/controllers/controller1.rb +0 -0
  90. data/spec/pancake/fixtures/paths/controllers/controller2.rb +0 -0
  91. data/spec/pancake/fixtures/paths/controllers/controller3.rb +0 -0
  92. data/spec/pancake/fixtures/paths/models/model1.rb +0 -0
  93. data/spec/pancake/fixtures/paths/models/model2.rb +0 -0
  94. data/spec/pancake/fixtures/paths/models/model3.rb +0 -0
  95. data/spec/pancake/fixtures/paths/stack/controllers/controller1.rb +0 -0
  96. data/spec/pancake/fixtures/paths/stack/models/model3.rb +0 -0
  97. data/spec/pancake/fixtures/paths/stack/views/view1.erb +0 -0
  98. data/spec/pancake/fixtures/paths/stack/views/view1.rb +0 -0
  99. data/spec/pancake/fixtures/paths/stack/views/view2.erb +0 -0
  100. data/spec/pancake/fixtures/paths/stack/views/view2.haml +0 -0
  101. data/spec/pancake/fixtures/render_templates/context_template.html.erb +1 -0
  102. data/spec/pancake/fixtures/render_templates/erb_template.html.erb +1 -0
  103. data/spec/pancake/fixtures/render_templates/erb_template.json.erb +1 -0
  104. data/spec/pancake/fixtures/render_templates/haml_template.html.haml +1 -0
  105. data/spec/pancake/fixtures/render_templates/haml_template.xml.haml +1 -0
  106. data/spec/pancake/hooks/on_inherit_spec.rb +65 -0
  107. data/spec/pancake/inheritance_spec.rb +100 -0
  108. data/spec/pancake/middleware_spec.rb +401 -0
  109. data/spec/pancake/mime_types_spec.rb +234 -0
  110. data/spec/pancake/mixins/publish_spec.rb +94 -0
  111. data/spec/pancake/mixins/render_spec.rb +55 -0
  112. data/spec/pancake/mixins/stack_helper_spec.rb +46 -0
  113. data/spec/pancake/pancake_spec.rb +31 -0
  114. data/spec/pancake/paths_spec.rb +210 -0
  115. data/spec/pancake/stack/app_spec.rb +28 -0
  116. data/spec/pancake/stack/bootloader_spec.rb +41 -0
  117. data/spec/pancake/stack/middleware_spec.rb +0 -0
  118. data/spec/pancake/stack/router_spec.rb +266 -0
  119. data/spec/pancake/stack/stack_configuration_spec.rb +101 -0
  120. data/spec/pancake/stack/stack_spec.rb +55 -0
  121. data/spec/pancake/stacks/short/controller_spec.rb +287 -0
  122. data/spec/pancake/stacks/short/router_spec.rb +132 -0
  123. data/spec/pancake/stacks/short/stack_spec.rb +40 -0
  124. data/spec/spec_helper.rb +21 -0
  125. metadata +238 -0
@@ -0,0 +1,177 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Pancake::Configuration" do
4
+
5
+ it "should let me make a new configuration" do
6
+ conf_klass = Pancake::Configuration.make
7
+ conf_klass.should inherit_from(Pancake::Configuration::Base)
8
+ end
9
+
10
+ describe "usage" do
11
+ before(:each) do
12
+ @Conf = Pancake::Configuration.make
13
+ end
14
+
15
+ it "should let me set defaults" do
16
+ @Conf.default :foo, :bar
17
+ c = @Conf.new
18
+ c.foo.should == :bar
19
+ @Conf.new.foo.should == :bar
20
+ end
21
+
22
+ it "should let me set a description on the default" do
23
+ @Conf.default :foo, :bar
24
+ @Conf.default :bar, :baz, "A description of bar"
25
+ c = @Conf.new
26
+ c.foo.should == :bar
27
+ c.bar.should == :baz
28
+ c.defaults[:foo][:description].should == ""
29
+ c.defaults[:bar][:description].should == "A description of bar"
30
+ end
31
+
32
+ it "should allow me to make multiple different configuration objects without polluting each other" do
33
+ c1 = Pancake::Configuration.make
34
+ c2 = Pancake::Configuration.make
35
+ c1.default :foo, :bar
36
+ c2.default :foo, :baz, "A description of foo"
37
+ c1i, c2i = c1.new, c2.new
38
+ c1i.foo.should == :bar
39
+ c1i.defaults[:foo][:value].should == :bar
40
+ c1i.defaults[:foo][:description].should == ""
41
+ c2i.foo.should == :baz
42
+ c2i.defaults[:foo][:value].should == :baz
43
+ c2i.defaults[:foo][:description].should == "A description of foo"
44
+ end
45
+
46
+ it "should allow me to define defaults in the make block" do
47
+ c1 = Pancake::Configuration.make do
48
+ default :foo, :bar, "Foo Desc"
49
+ default :baz, 42, "The Answer"
50
+ end
51
+ c = c1.new
52
+ c.foo.should == :bar
53
+ c.baz.should == 42
54
+ end
55
+
56
+ it "should overwrite the default values" do
57
+ @Conf.default :foo, :bar
58
+ c = @Conf.new
59
+ c.foo = :baz
60
+ c.foo.inspect
61
+ c.foo.should == :baz
62
+ c.defaults[:foo][:value].should == :bar
63
+ end
64
+
65
+ it "should not add a default value for values I set blindly" do
66
+ c = @Conf.new
67
+ c.bar.should be_nil
68
+ c.defaults.keys.should_not include(:bar)
69
+ end
70
+
71
+ it "should allow me to set a value then the default and not get mixed up" do
72
+ c = @Conf.new
73
+ c.bar = :foo
74
+ c.defaults[:bar][:value].should == nil
75
+ end
76
+
77
+ it "should let me declare defaults after I've initizlied the configuartion object" do
78
+ c = @Conf.new
79
+ @Conf.default :foo, :bar
80
+ c.foo.should == :bar
81
+ end
82
+
83
+ it "should give me a list of the current defaults" do
84
+ @Conf.default :foo, :bar
85
+ @Conf.default :bar, :baz, "Some description"
86
+ c = @Conf.new
87
+ c.defaults.should == {
88
+ :foo => {
89
+ :description => "",
90
+ :value => :bar
91
+ },
92
+ :bar => {
93
+ :description => "Some description",
94
+ :value => :baz
95
+ }
96
+ }
97
+ end
98
+
99
+ it "should give me a description for a default" do
100
+ @Conf.default :foo, :bar, "foo description"
101
+ c = @Conf.new
102
+ c.description_for(:foo).should == "foo description"
103
+ end
104
+
105
+ it "should give me a list of values for the current object" do
106
+ @Conf.default :foo, :bar
107
+ c = @Conf.new
108
+ c.values.should == {}
109
+ c.foo
110
+ c.values.should == {:foo => :bar}
111
+ c.baz = :paz
112
+ c.values.should == {:foo => :bar, :baz => :paz}
113
+ end
114
+
115
+ it "should allow me to define methods in the make block" do
116
+ c = Pancake::Configuration.make do
117
+ default :foo, :bar
118
+
119
+ default :bar do
120
+ foobar
121
+ end
122
+
123
+ def foobar
124
+ "This is in foobar"
125
+ end
126
+ end
127
+
128
+ ci = c.new
129
+ ci.foo.should == :bar
130
+ ci.bar.should == "This is in foobar"
131
+ end
132
+
133
+ it "should not cache the default when it's defined in a block" do
134
+ c = Pancake::Configuration.make do
135
+ default :foo do
136
+ bar
137
+ end
138
+ default :bar, :bar
139
+ end
140
+ ci = c.new
141
+ ci.foo.should == :bar
142
+ ci.bar = :baz
143
+ ci.bar.should == :baz
144
+ ci.foo.should == :baz
145
+ end
146
+
147
+ it "should overwrite the proc when set directly" do
148
+ @Conf.default :foo, :foo
149
+ @Conf.default(:bar){ foo }
150
+ ci = @Conf.new
151
+ ci.bar.should == :foo
152
+ ci.foo = :baz
153
+ ci.bar.should == :baz
154
+ ci.bar = :foobar
155
+ ci.bar.should == :foobar
156
+ end
157
+
158
+ it "should not cache nil when accessing before defaults are set" do
159
+ c = @Conf.new
160
+ c.foo.should be_nil
161
+ @Conf.default :foo, :bar
162
+ c.foo.should == :bar
163
+ c.foo = :baz
164
+ c.foo.should == :baz
165
+ end
166
+
167
+ end
168
+ end
169
+
170
+
171
+ describe "pancake configuartion" do
172
+
173
+ it "should provide access to it's configuration object" do
174
+ Pancake.configuration.class.should inherit_from(Pancake::Configuration::Base)
175
+ end
176
+
177
+ end
@@ -0,0 +1 @@
1
+ <%= the_current_context %>
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe "Pancake::Stack inheritance" do
4
+ describe "inheritance hooks" do
5
+ before(:all) do
6
+ $on_inherit_blocks = Pancake::Stack.on_inherit.dup
7
+ end
8
+ after(:all) do
9
+ Pancake::Stack.on_inherit.clear
10
+ $on_inherit_blocks.each do |blk|
11
+ Pancake::Stack.on_inherit(&blk)
12
+ end
13
+ end
14
+
15
+ before(:each) do
16
+ $collector = []
17
+ clear_constants(:FooStack)
18
+ Pancake::Stack.on_inherit.clear
19
+ end
20
+
21
+ after(:each) do
22
+ clear_constants(:FooStack)
23
+ end
24
+
25
+ it "should be able to add inheritance hooks" do
26
+ Pancake::Stack.on_inherit do |base, parent|
27
+ $collector << base
28
+ end
29
+ class ::FooStack < Pancake::Stack; end
30
+ $collector.should == [FooStack]
31
+ end
32
+
33
+ it "should be able to add multiple inheritance hooks" do
34
+ Pancake::Stack.on_inherit{|b,p| $collector << b}
35
+ Pancake::Stack.on_inherit{|b,p| $collector << :foo}
36
+
37
+ class ::FooStack < Pancake::Stack
38
+ end
39
+
40
+ $collector.should == [FooStack, :foo]
41
+ end
42
+
43
+ end
44
+
45
+ describe "Inheriting Stacks" do
46
+ before(:all) do
47
+ clear_constants(:FooStack, :BarStack)
48
+ end
49
+
50
+ before(:each) do
51
+ class ::FooStack < Pancake::Stack; end
52
+ end
53
+
54
+ after(:each) do
55
+ clear_constants(:FooStack, :BarStack)
56
+ end
57
+
58
+ describe "configuration" do
59
+ it "should inherit the configuration with a stack" do
60
+ class ::BarStack < ::FooStack; end
61
+ BarStack::Configuration.should inherit_from(FooStack::Configuration)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,100 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Pancake Inheritance" do
4
+ describe "on inherit hook" do
5
+
6
+ before(:each) do
7
+ clear_constants(:MyFoo, :OtherFoo, :FutherFoo, :SomeFoo, :DeeperFoo)
8
+ class ::MyFoo
9
+ extend Pancake::Hooks::OnInherit
10
+ end
11
+
12
+ ::MyFoo.on_inherit do |base, parent|
13
+ $inherited_capture << {:base => base, :parent => parent}
14
+ end
15
+
16
+ $inherited_capture = []
17
+ end
18
+
19
+ it "should provide on_inherit callbacks" do
20
+ class ::OtherFoo < MyFoo; end
21
+ $inherited_capture.should == [{:base => OtherFoo, :parent => MyFoo}]
22
+ end
23
+
24
+ it "should inherit multiple times" do
25
+ class ::OtherFoo < MyFoo; end
26
+ class ::FurtherFoo < OtherFoo; end
27
+ $inherited_capture.should == [{:base => OtherFoo, :parent => MyFoo}, {:base => FurtherFoo, :parent => OtherFoo}]
28
+ end
29
+
30
+ it "should allow for multiple callbacks" do
31
+ MyFoo.on_inherit{|base, parent| $inherited_capture << :second}
32
+ class ::OtherFoo < MyFoo; end
33
+ $inherited_capture.should == [{:base => OtherFoo, :parent => MyFoo}, :second]
34
+ end
35
+
36
+ it "should allow for multiple callbacks that are inherited" do
37
+ MyFoo.on_inherit{|base, parent| $inherited_capture << :second}
38
+ class ::OtherFoo < MyFoo; end
39
+ $inherited_capture = []
40
+ OtherFoo.on_inherit{|base, parent| $inherited_capture << :inherited}
41
+ class ::SomeFoo < OtherFoo; end
42
+ $inherited_capture.should == [{:base => SomeFoo, :parent => OtherFoo}, :second, :inherited]
43
+ end
44
+
45
+ it "should not pollute the parent with the child inherited hooks" do
46
+ class ::OtherFoo < MyFoo; end
47
+ $inherited_capture = []
48
+ OtherFoo.on_inherit{|b,p| $inherited_captuer << :should_not_be_in_there}
49
+ class ::SomeFoo < MyFoo; end
50
+ $inherited_capture.should == [{:base => SomeFoo, :parent => MyFoo}]
51
+ end
52
+ end # "on inherit hook"
53
+
54
+ describe "inheritable inner classes" do
55
+
56
+ before(:each) do
57
+ clear_constants(:MyFoo, :OtherFoo, :InnerFoo, :SomeFoo, :DeeperFoo)
58
+
59
+ class ::MyFoo
60
+ inheritable_inner_classes :InnerFoo
61
+ class InnerFoo; end
62
+ end
63
+ end
64
+
65
+ it "should inherit the inner class along with the outer class" do
66
+ class ::OtherFoo < MyFoo; end
67
+ OtherFoo::InnerFoo.superclass.should equal(MyFoo::InnerFoo)
68
+ OtherFoo::InnerFoo.should_not equal(MyFoo::InnerFoo)
69
+ end
70
+
71
+ it "should inherit the inner class multiple times" do
72
+ class ::OtherFoo < MyFoo; end
73
+ class ::SomeFoo < OtherFoo; end
74
+ class ::DeeperFoo < SomeFoo; end
75
+
76
+ SomeFoo::InnerFoo.superclass.should equal(OtherFoo::InnerFoo)
77
+ SomeFoo::InnerFoo.should_not equal(OtherFoo::InnerFoo)
78
+ DeeperFoo::InnerFoo.superclass.should equal(SomeFoo::InnerFoo)
79
+ DeeperFoo::InnerFoo.should_not equal(OtherFoo::InnerFoo)
80
+ DeeperFoo::InnerFoo.superclass.should equal(SomeFoo::InnerFoo)
81
+ end
82
+
83
+ it "should allow additional inner classes to be declared without polluting the parent" do
84
+ class ::OtherFoo < MyFoo
85
+ inheritable_inner_classes :SomeDeepFoo
86
+ class SomeDeepFoo; end
87
+ end
88
+
89
+ class ::SomeFoo < MyFoo; end
90
+ class ::DeeperFoo < ::OtherFoo; end
91
+
92
+ SomeFoo::InnerFoo.should inherit_from(MyFoo::InnerFoo)
93
+ SomeFoo.const_defined?(:SomeDeepFoo).should be_false
94
+
95
+ DeeperFoo::InnerFoo.should inherit_from(OtherFoo::InnerFoo)
96
+ DeeperFoo::SomeDeepFoo.should inherit_from(OtherFoo::SomeDeepFoo)
97
+ end
98
+
99
+ end # "inheritable inner classes"
100
+ end
@@ -0,0 +1,401 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Pancake::Middleware" do
4
+ before(:all) do
5
+ $pk_mid = Pancake.middlewares.dup
6
+ end
7
+
8
+ after(:all) do
9
+ Pancake.middlewares.replace $pk_mid
10
+ end
11
+
12
+ before(:each) do
13
+ Pancake.middlewares.clear
14
+ def app
15
+ @app
16
+ end
17
+
18
+ def default_env
19
+ Rack::MockRequest.env_for
20
+ end
21
+
22
+ $current_env = {}
23
+
24
+ class ::GeneralMiddleware
25
+ attr_accessor :app
26
+ def initialize(app, opts={});@app = app; end
27
+
28
+ def mark_env(env)
29
+ env["p.s.c"] ||= []
30
+ env["p.s.c"] << self.class
31
+ end
32
+
33
+ def call(env)
34
+ mark_env(env)
35
+ @app.call(env)
36
+ end
37
+ end # GeneralMiddlware
38
+
39
+ class ::FooApp < Pancake::Stack
40
+ def self.new_app_instance; self end
41
+
42
+ def self.call(env)
43
+ $current_env = env
44
+ [200,{"Content-Type" => "text/plain"}, [name]]
45
+ end
46
+ end # FooApp
47
+ FooApp.roots << Pancake.get_root("/tmp")
48
+ end
49
+
50
+ after(:each) do
51
+ clear_constants(:FooApp, :BarApp, :BazApp, :GeneralMiddleware, :BarMiddle, :FooMiddle, :BazMiddle, :PazMiddle)
52
+ end
53
+
54
+ describe "pancake middlewares" do
55
+ before(:each) do
56
+ @root = File.join(Pancake.get_root(__FILE__), "fixtures", "foo_stack")
57
+ @the_app = Proc.new{|e| }
58
+ Pancake::StackMiddleware.reset!
59
+ end
60
+
61
+ it "should allow me to add middleware to pancake" do
62
+ Pancake.use GeneralMiddleware
63
+ @app = Pancake.start(:root => @root){ FooApp.stackup }
64
+ Pancake.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
65
+ get "/"
66
+ $current_env["p.s.c"].should include(GeneralMiddleware)
67
+ end
68
+
69
+ it "should allow me to add multiple middlewares to panckae" do
70
+ class ::FooMiddle < GeneralMiddleware; end
71
+ Pancake.use GeneralMiddleware
72
+ FooApp.use FooMiddle
73
+ @app = Pancake.start(:root => @root){ FooApp.stackup }
74
+ get "/"
75
+ $current_env["p.s.c"].should == [GeneralMiddleware, FooMiddle]
76
+ end
77
+
78
+ it "should put the pancake middlewares out in front" do
79
+ class ::FooMiddle < GeneralMiddleware; end
80
+ class ::BarMiddle < GeneralMiddleware; end
81
+ class ::BarApp < FooApp; end
82
+ class ::BazApp < FooApp; end
83
+
84
+ BarApp.use BarMiddle
85
+ BazApp.use FooMiddle
86
+ Pancake.use GeneralMiddleware
87
+
88
+ FooApp.with_router do |r|
89
+ r.mount(BarApp.stackup, "/bar")
90
+ r.mount(BazApp.stackup, "/baz")
91
+ end
92
+
93
+ @app = Pancake.start(:root => @root){ FooApp.stackup }
94
+ get "/baz"
95
+ $current_env["p.s.c"].should == [GeneralMiddleware, FooMiddle]
96
+ get "/bar"
97
+ $current_env["p.s.c"].should == [GeneralMiddleware, BarMiddle]
98
+ end
99
+ end
100
+
101
+ it "should allow me to add middleware" do
102
+ FooApp.class_eval do
103
+ use GeneralMiddleware
104
+ end
105
+ @app = FooApp.stackup
106
+ get "/"
107
+ $current_env["p.s.c"].should include(GeneralMiddleware)
108
+ end
109
+
110
+ it "should allow me to add multiple middlewares" do
111
+ class ::FooMiddle < GeneralMiddleware; end
112
+ FooApp.class_eval do
113
+ use GeneralMiddleware
114
+ use FooMiddle
115
+ end
116
+ @app = FooApp.stackup
117
+ get "/"
118
+ [GeneralMiddleware, FooMiddle].each do |m|
119
+ $current_env["p.s.c"].should include(m)
120
+ end
121
+ end
122
+
123
+ it "should allow you to add middleware from outside the class" do
124
+ FooApp.use GeneralMiddleware
125
+ @app = FooApp.stackup
126
+ get "/"
127
+ $current_env["p.s.c"].should == [GeneralMiddleware]
128
+ end
129
+
130
+ describe "replace middleware" do
131
+ before(:each) do
132
+ FooApp.stack(:replaceable).use(GeneralMiddleware, :some => :option){ :original }
133
+ class FooMiddle < GeneralMiddleware; end
134
+ end
135
+
136
+ it "should replace the middleware with another middleware" do
137
+ orig = FooApp::StackMiddleware[:replaceable]
138
+ orig.middleware.should == GeneralMiddleware
139
+ orig.config.should == {:some => :option}
140
+
141
+ FooApp.stack(:replaceable).use(FooMiddle, :foo => :options)
142
+ replaced = FooApp.stack(:replaceable)
143
+ replaced.middleware.should == FooMiddle
144
+ replaced.config.should == {:foo => :options}
145
+ end
146
+ end
147
+
148
+ describe "deleteing middleware" do
149
+ before(:each) do
150
+ FooApp.stack(:deleteable).use(GeneralMiddleware, :some => :option){ :original }
151
+ end
152
+
153
+ it "should delete the middleware" do
154
+ orig = FooApp::StackMiddleware[:deleteable]
155
+ orig.should_not be_nil
156
+ orig.middleware.should == GeneralMiddleware
157
+ FooApp.stack(:deleteable).delete!
158
+ FooApp::StackMiddleware[:deleteable].should be_nil
159
+ end
160
+
161
+ it "should not include middleware that depends on being deleted" do
162
+ class FooMiddle < GeneralMiddleware; end
163
+ FooApp.stack(:foo, :after => :deleteable).use(FooMiddle)
164
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
165
+ FooApp.stack(:deleteable).delete!
166
+ FooApp.middlewares.map{|m| m.middleware}.should == []
167
+ end
168
+ end
169
+
170
+ describe "edit middleware" do
171
+ before(:each) do
172
+ FooApp.stack(:editable).use(GeneralMiddleware,:some => :config)
173
+ end
174
+
175
+ it "should allow me to update settings for middleware" do
176
+ FooApp.stack(:editable).config.should == {:some => :config}
177
+ FooApp.stack(:editable).config = {:foo => :bar}
178
+ FooApp.stack(:editable).config.should == {:foo => :bar}
179
+ end
180
+ end
181
+
182
+
183
+
184
+ describe "Inherited middleware" do
185
+ before(:each) do
186
+ class FooMiddle < GeneralMiddleware; end
187
+ class BarMiddle < GeneralMiddleware; end
188
+ end
189
+
190
+ it "should inherit middleware from it's parent class" do
191
+ FooApp.use GeneralMiddleware
192
+ FooApp.use FooMiddle
193
+ class BarApp < FooApp; end
194
+ BarApp.middlewares.should == FooApp.middlewares
195
+ end
196
+
197
+ it "should not pollute the parent when including new middlewares in the child" do
198
+ FooApp.use GeneralMiddleware
199
+ class BarApp < FooApp; end
200
+ BarApp.use FooMiddle
201
+ BarApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
202
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
203
+ end
204
+
205
+ describe "editing inherited middlware" do
206
+ it "should not edit the parent when editing the child" do
207
+ FooApp.use GeneralMiddleware, :some => :option
208
+ class BarApp < FooApp; end
209
+ BarApp.stack(GeneralMiddleware).middleware.should == GeneralMiddleware
210
+ BarApp.stack(GeneralMiddleware).config = {:foo => :bar}
211
+ BarApp.stack(GeneralMiddleware).config.should == {:foo => :bar}
212
+ FooApp.stack(GeneralMiddleware).config.should == {:some => :option}
213
+ end
214
+
215
+ it "should not update the parent when updating a childs config" do
216
+ FooApp.use GeneralMiddleware, :some => :option
217
+ class BarApp < FooApp; end
218
+ BarApp.stack(GeneralMiddleware).config[:another] = :option
219
+ FooApp.stack(GeneralMiddleware).config.should == {:some => :option}
220
+ BarApp.stack(GeneralMiddleware).config.should == {:some => :option, :another => :option}
221
+ end
222
+ end
223
+ end
224
+
225
+ describe "Stacks should inherit middleware" do
226
+ before(:all) do
227
+ $pk_middlewares = Pancake::Stack.middlewares.dup
228
+ end
229
+
230
+ after(:all) do
231
+ Pancake::Stack.middlewares.replace $pk_middlewares
232
+ end
233
+
234
+ before(:each) do
235
+ class FooMiddle < GeneralMiddleware; end
236
+ class BarMiddle < GeneralMiddleware; end
237
+ end
238
+
239
+ after(:each) do
240
+ Pancake::Stack::StackMiddleware.reset!
241
+ end
242
+
243
+ it "should clear the middlewares for the specs" do
244
+ Pancake::Stack.middlewares.should be_blank
245
+ end
246
+
247
+ it "should allow me to set middlewares on Pancake::Stack" do
248
+ Pancake::Stack.use GeneralMiddleware
249
+ Pancake::Stack.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
250
+ end
251
+
252
+ it "should carry down middlewares from the Pancake::Stack to inherited stacks" do
253
+ Pancake::Stack.use GeneralMiddleware
254
+ class FooApp < Pancake::Stack; end
255
+ FooApp.use FooMiddle
256
+ Pancake::Stack.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware]
257
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
258
+ end
259
+
260
+ it "should let me have different middlewares in different children" do
261
+ Pancake::Stack.use GeneralMiddleware
262
+ class FooApp < Pancake::Stack; end
263
+ FooApp.use FooMiddle
264
+ class BarApp < Pancake::Stack; end
265
+ BarApp.use BarMiddle
266
+ FooApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle]
267
+ BarApp.middlewares.map{|m| m.middleware}.should == [GeneralMiddleware, BarMiddle]
268
+ end
269
+
270
+ describe "Stack Middleware Enabled Constant", :shared => true do
271
+
272
+ before(:each) do
273
+ raise "You must set a @konstant for the stack construction spec" unless @konstant
274
+ @konstant::StackMiddleware.reset!
275
+ end
276
+
277
+ describe "named middleware" do
278
+ it "should allow me to name a #{@konstant} middleware" do
279
+ @konstant.stack(:foo).use(GeneralMiddleware)
280
+ @konstant.stack[:foo].middleware.should == GeneralMiddleware
281
+ end
282
+
283
+ it "should implicitly name a middleware" do
284
+ @konstant.stack.use(GeneralMiddleware)
285
+ @konstant.stack[GeneralMiddleware].middleware.should == GeneralMiddleware
286
+ end
287
+ end
288
+
289
+ describe "before/after middleware" do
290
+ before(:each) do
291
+ class ::BazMiddle < GeneralMiddleware; end
292
+ class ::PazMiddle < GeneralMiddleware; end
293
+ end
294
+ # :FooApp, :BarApp, :BazApp, :GeneralMiddleware, :BarMiddle, :FooMiddle, :BazMiddle, :PazMiddle
295
+ it "should allow me to add middleware before other middleware" do
296
+ @konstant.use(GeneralMiddleware)
297
+ @konstant.stack(:bar).use(BarMiddle)
298
+ @konstant.stack(:foo, :before => :bar).use(FooMiddle)
299
+ result = @konstant.middlewares.map{|m| m.middleware}
300
+ result.should == [GeneralMiddleware, FooMiddle, BarMiddle]
301
+ end
302
+
303
+ it "should allow me to add middleware after other middleware " do
304
+ @konstant.use(BarMiddle)
305
+ @konstant.stack(:general).use(GeneralMiddleware)
306
+ @konstant.stack(:foo, :after => BarMiddle).use(FooMiddle)
307
+ result = @konstant.middlewares.map{|m| m.middleware}
308
+ result.should == [ BarMiddle, FooMiddle, GeneralMiddleware]
309
+ end
310
+
311
+ it "should allow me to add middleware arbitrarily and have it in the correct order" do
312
+ @konstant.use(GeneralMiddleware)
313
+ @konstant.stack(:bar).use(BarMiddle)
314
+ @konstant.stack(:foo, :before => :bar).use(FooMiddle)
315
+ @konstant.stack(:baz, :after => :foo).use(BazMiddle)
316
+ @konstant.stack(:paz, :before => GeneralMiddleware).use(PazMiddle)
317
+ result = @konstant.middlewares.map{|m| m.middleware}
318
+ result.should == [PazMiddle, GeneralMiddleware, FooMiddle, BazMiddle, BarMiddle]
319
+ end
320
+ end
321
+ end
322
+
323
+ describe "Pancake Middleware Construction" do
324
+ before(:each) do
325
+ @konstant = Pancake
326
+ end
327
+
328
+ it_should_behave_like("Stack Middleware Enabled Constant")
329
+ end
330
+
331
+ describe "Pancake Stack Middleware" do
332
+ before(:each) do
333
+ @konstant = Pancake::Stack
334
+ end
335
+ it_should_behave_like("Stack Middleware Enabled Constant")
336
+ end
337
+
338
+ describe "An inherited panckae stack app" do
339
+ before(:each) do
340
+ class ::FooApp < Pancake::Stack; end
341
+ @konstant = FooApp
342
+ end
343
+ it_should_behave_like("Stack Middleware Enabled Constant")
344
+ end
345
+
346
+ describe "a deeply inherited stack app" do
347
+ before(:each) do
348
+ class ::FooApp < Pancake::Stack; end
349
+ class ::BarApp < FooApp; end
350
+ @konstant = BarApp
351
+ end
352
+ it_should_behave_like("Stack Middleware Enabled Constant")
353
+ end
354
+
355
+ it "should allow me to inherit middleware from a parent stack" do
356
+ class ::FooApp < Pancake::Stack; end
357
+ FooApp.use(GeneralMiddleware)
358
+ class ::BarApp < FooApp; end
359
+ BarApp.stack(:foo).use(FooMiddle)
360
+ BarApp.stack(:bar, :after => GeneralMiddleware).use(BarMiddle)
361
+ result = BarApp.middlewares.map{|m|m.middleware}
362
+ result.should == [GeneralMiddleware, BarMiddle, FooMiddle]
363
+ end
364
+
365
+ end # Stack Inheritance
366
+
367
+ describe "types of stacks" do
368
+ before(:each) do
369
+ class FooMiddle < GeneralMiddleware; end
370
+ class BarMiddle < GeneralMiddleware; end
371
+ class BazMiddle < GeneralMiddleware; end
372
+
373
+ FooApp.stack(:general, :labels => [:production] ).use(GeneralMiddleware)
374
+ FooApp.stack(:foo, :labels => [:production, :demo] ).use(FooMiddle)
375
+ FooApp.stack(:bar, :labels => [:test] ).use(BarMiddle)
376
+ FooApp.stack(:baz, :labels => [:any] ).use(BazMiddle)
377
+ end
378
+
379
+ it "should differentiate between stack types" do
380
+ FooApp.middlewares(:production).map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle, BazMiddle]
381
+ FooApp.middlewares(:demo).map{|m| m.middleware}.should == [FooMiddle, BazMiddle]
382
+ FooApp.middlewares(:test).map{|m| m.middleware}.should == [BarMiddle, BazMiddle]
383
+ FooApp.middlewares(:demo, :test).map{|m| m.middleware}.should == [FooMiddle, BarMiddle,BazMiddle]
384
+ end
385
+
386
+ it "should add a middleware that is not declared with any particular label to all stacks" do
387
+ class PazMiddle < GeneralMiddleware; end
388
+ FooApp.stack(:paz).use(PazMiddle)
389
+ FooApp.middlewares(:test).map{|m| m.middleware}.should == [BarMiddle, BazMiddle, PazMiddle]
390
+ FooApp.middlewares(:demo).map{|m| m.middleware}.should == [FooMiddle, BazMiddle, PazMiddle]
391
+ FooApp.middlewares(:production).map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle, BazMiddle, PazMiddle]
392
+ end
393
+
394
+ it "should not use a middleware if it is dependent on middleware that is not in the correct stack" do
395
+ class PazMiddle < GeneralMiddleware; end
396
+ FooApp.stack(:paz, :after => :bar).use(PazMiddle)
397
+ FooApp.middlewares(:production).map{|m| m.middleware}.should == [GeneralMiddleware, FooMiddle, BazMiddle]
398
+ FooApp.middlewares(:test).map{|m| m.middleware}.should == [BarMiddle, PazMiddle, BazMiddle]
399
+ end
400
+ end
401
+ end