pakada 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Lars Gierth
1
+ Copyright (c) 2011 Lars Gierth
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/lib/pakada/module.rb CHANGED
@@ -6,7 +6,7 @@ class Pakada
6
6
  attr_reader :descendants
7
7
 
8
8
  def included(klass)
9
- klass.send :include, Hooked::Container
9
+ klass.send :include, Hooked
10
10
 
11
11
  class << klass
12
12
  attr_reader :module_name, :path
@@ -63,5 +63,9 @@ class Pakada
63
63
  def path
64
64
  self.class.path
65
65
  end
66
+
67
+ def boot; end
68
+
69
+ def hooks; end
66
70
  end
67
71
  end
@@ -1,3 +1,3 @@
1
1
  class Pakada
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/pakada.rb CHANGED
@@ -5,13 +5,9 @@ require "pakada/module"
5
5
  require "pakada/version"
6
6
 
7
7
  class Pakada
8
- include Hooked::Container
8
+ DEFAULT_APP = proc {|req_env| Pakada.request req_env }
9
9
 
10
- DEFAULT_APP = proc do |req_env|
11
- Rack::Response.new("Hi, I'm Pakada #{VERSION}").finish
12
- end
13
-
14
- attr_reader :env, :modules, :hooking, :middleware
10
+ attr_reader :env, :modules, :middleware
15
11
  attr_accessor :app
16
12
 
17
13
  def initialize
@@ -37,9 +33,9 @@ class Pakada
37
33
  def boot
38
34
  load_env
39
35
  load_modules
40
- load_hooking
36
+ load_hooks
41
37
 
42
- hooking.invoke(:boot) unless testing?
38
+ modules.each_value {|m| m.boot } unless testing?
43
39
  end
44
40
 
45
41
  def load_env
@@ -52,32 +48,23 @@ class Pakada
52
48
  end
53
49
  end
54
50
 
55
- def load_hooking
56
- raise "modules have not been loaded" unless modules.respond_to? :values
57
- @hooking = Hooked::Controller.new self, *modules.values
51
+ def load_hooks
52
+ modules.each_value {|m| m.hooks } unless testing?
58
53
  end
59
54
 
60
55
  def call(req_env)
61
- to_app.call(req_env)
56
+ to_app.call req_env
62
57
  end
63
58
 
64
59
  def to_app
65
60
  builder = Rack::Builder.new
66
61
  middleware.each {|mw| builder.use *(Array === mw ? mw : [mw]) }
67
- builder.run proc {|req_env| handle_request(req_env) }
68
- builder
62
+ builder.run app || DEFAULT_APP
63
+ builder.to_app
69
64
  end
70
65
 
71
- def handle_request(req_env)
72
- req = Rack::Request.new req_env
73
- hooking.invoke(:request, req).finish
74
- end
75
-
76
- hookable :boot do |nothing|; end
77
-
78
- hookable :request do |ctx|
79
- resp = (app || DEFAULT_APP).call ctx.args.env
80
- ctx.result = Rack::Response.new resp[2], *resp[0..1]
66
+ def request(req_env)
67
+ Rack::Response.new("Hi, I'm Pakada #{VERSION}").finish
81
68
  end
82
69
 
83
70
  class << self
data/pakada.gemspec CHANGED
@@ -8,17 +8,16 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Lars Gierth"]
10
10
  s.email = ["lars.gierth@gmail.com"]
11
- s.homepage = "http://rubygems.org/gems/pakada"
11
+ s.homepage = "https://rubygems.org/gems/pakada"
12
12
  s.summary = %q{Very Extensible HTTP Container For Ruby}
13
13
  #s.description = %q{TODO: Write a gem description}
14
14
 
15
- s.add_dependency "rack", "~> 1.2.1"
16
- s.add_dependency "hooked", "~> 0.1.0"
15
+ s.add_dependency "rack"
16
+ s.add_dependency "hooked", "~> 0.2"
17
17
 
18
- s.add_development_dependency "rspec", "~> 2.3"
19
- s.add_development_dependency "mocha", "~> 0.9.10"
20
- s.add_development_dependency "fakefs", "~> 0.3.1"
21
- s.add_development_dependency "fakefs-require", "~> 0.2.1"
18
+ s.add_development_dependency "rspec"
19
+ s.add_development_dependency "fakefs"
20
+ s.add_development_dependency "fakefs-require"
22
21
 
23
22
  s.files = `git ls-files`.split("\n") - [".gitignore", ".rvmrc", "config.ru"]
24
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/spec/module_spec.rb CHANGED
@@ -140,10 +140,11 @@ describe "SomeModule" do
140
140
  Pakada::Module.descendants.clear
141
141
  end
142
142
 
143
- it "is a hooking container" do
143
+ it "can hook its methods" do
144
+ @obj.should respond_to(:before)
145
+ @obj.should respond_to(:after)
146
+ @obj.should respond_to(:around)
144
147
  @obj.should respond_to(:hooked)
145
- @obj.should respond_to(:hooked=)
146
- @obj.should respond_to(:hookable)
147
148
  end
148
149
 
149
150
  describe ".module_name" do
@@ -177,4 +178,16 @@ describe "SomeModule" do
177
178
  @obj.path.should == @klass.path
178
179
  end
179
180
  end
181
+
182
+ describe "#hooks" do
183
+ it "is a placeholder" do
184
+ @obj.should respond_to(:hooks)
185
+ end
186
+ end
187
+
188
+ describe "#boot" do
189
+ it "is a placeholder" do
190
+ @obj.should respond_to(:boot)
191
+ end
192
+ end
180
193
  end
data/spec/pakada_spec.rb CHANGED
@@ -3,12 +3,6 @@ require "spec_helper"
3
3
  describe Pakada do
4
4
  subject { Pakada.instance }
5
5
 
6
- it "is a hooking container" do
7
- Pakada.should respond_to(:hookables)
8
- Pakada.should respond_to(:hooks)
9
- Pakada.instance.should respond_to(:hooked=)
10
- end
11
-
12
6
  it "forwards all messages to the Singleton instance" do
13
7
  Pakada.instance.should_receive(:message)
14
8
  Pakada.message
@@ -24,12 +18,6 @@ describe Pakada do
24
18
  Pakada.respond_to?(:instance)
25
19
  end
26
20
 
27
- describe "::DEFAULT_APP" do
28
- it "prints a tender welcome greeting" do
29
- Pakada::DEFAULT_APP.call({})[2].body.to_s.should =~ /Pakada #{Pakada::VERSION}/
30
- end
31
- end
32
-
33
21
  describe ".instance" do
34
22
  it "creates a new Singleton instance and returns it" do
35
23
  Pakada.stub(:new) { double "a Pakada instance" }
@@ -46,53 +34,45 @@ describe Pakada do
46
34
  end
47
35
 
48
36
  describe ".boot" do
49
- def stub_load_methods
37
+ before :each do
50
38
  subject.stub :load_env
51
39
  subject.stub :load_modules
52
- subject.stub :load_hooking
53
- subject.stub(:hooking) { double "hooks", :invoke => 123 }
40
+ subject.stub :load_hooks
41
+
42
+ modules = [:foo, :bar, :baz].inject({}) do |modules, name|
43
+ modules[name] = stub "#{name} module", :boot => nil
44
+ modules
45
+ end
46
+ subject.stub(:modules).and_return modules
54
47
  end
55
48
 
56
49
  it "calls .load_env" do
57
- stub_load_methods
58
50
  subject.should_receive :load_env
59
51
  subject.boot
60
52
  end
61
53
 
62
54
  it "calls .load_modules" do
63
- stub_load_methods
64
55
  subject.should_receive :load_modules
65
56
  subject.boot
66
57
  end
67
58
 
68
- it "calls .load_hooking" do
69
- stub_load_methods
70
- subject.should_receive :load_hooking
59
+ it "calls .load_hooks" do
60
+ subject.should_receive :load_hooks
71
61
  subject.boot
72
62
  end
73
63
 
74
- it "invokes the :boot hookable" do
75
- stub_load_methods
76
- obj = Object.new
77
- subject.stub(:hooking) { obj }
78
- subject.hooking.should_receive(:invoke).with(:boot)
64
+ it "calls #boot on each module" do
65
+ subject.modules.each_value {|m| m.should_receive :boot }
79
66
  subject.boot
80
67
  end
81
68
 
82
- it "doesn't invoke the :boot hookable in testing environment" do
83
- stub_load_methods
84
- subject.stub(:env) { :testing }
85
- obj = Object.new
86
- subject.stub(:hooking) { obj }
87
- subject.hooking.should_not_receive(:invoke).with(:boot)
69
+ it "doesn't call #boot in testing environments" do
70
+ subject.stub(:env).and_return :testing
71
+ subject.modules.each_value {|m| m.should_not_receive :boot }
88
72
  subject.boot
89
73
  end
90
74
  end
91
75
 
92
- describe ".hookable(:boot)" do
93
- it "does nothing" do; end
94
- end
95
-
96
76
  describe ".load_env" do
97
77
  before :each do
98
78
  @env = ENV["RACK_ENV"]
@@ -127,40 +107,30 @@ describe Pakada do
127
107
  end
128
108
  end
129
109
 
130
- describe ".[]" do
131
- it "forwards to .modules" do
132
- mod = stub "module"
133
- subject.stub(:modules) { {:some_module => mod} }
134
- subject[:some_module].should == subject.modules[:some_module]
135
- end
136
- end
137
-
138
- describe ".load_hooking" do
139
- it "initializes the hooking controller" do
140
- subject.stub(:modules) { {} }
141
- subject.load_hooking
142
- subject.hooking.should respond_to(:invoke)
110
+ describe ".load_hooks" do
111
+ before :each do
112
+ subject.stub(:modules).and_return({})
113
+ [:foo, :bar].each do |m|
114
+ subject.modules[m] = stub("#{m} module")
115
+ end
143
116
  end
144
117
 
145
- it "adds itself to the hooking controller" do
146
- subject.stub(:modules) { {} }
147
- subject.load_hooking
148
- subject.hooking.should have(1).containers
149
- subject.hooking.containers[0].should == subject
118
+ it "calls #hooks on each module" do
119
+ subject.modules.each_value {|m| m.should_receive :hooks }
120
+ subject.load_hooks
150
121
  end
151
122
 
152
- it "adds the modules to the hooking controller" do
153
- modcls = double "module klass", :hookables => [], :hooks => {}
154
- mod = double "module", :hooked= => nil, :class => modcls
155
- subject.stub(:modules) { {:foo => mod} }
156
- subject.load_hooking
157
- subject.hooking.should have(2).containers
158
- subject.hooking.containers[1].should == mod
123
+ it "doesn't call #hooks in testing environments" do
124
+ subject.stub(:env).and_return :testing
125
+ subject.modules.each_value {|m| m.should_not_receive :hooks }
126
+ subject.load_hooks
159
127
  end
160
-
161
- it "fails if the modules have not been loaded" do
162
- subject.stub(:modules) { nil }
163
- proc { subject.load_hooking }.should raise_error(RuntimeError, "modules have not been loaded")
128
+ end
129
+
130
+ describe ".[]" do
131
+ it "forwards to .modules" do
132
+ subject.stub(:modules).and_return :some_module => stub("module")
133
+ subject[:some_module].should == subject.modules[:some_module]
164
134
  end
165
135
  end
166
136
 
@@ -205,76 +175,61 @@ describe Pakada do
205
175
  end
206
176
 
207
177
  describe ".to_app" do
208
- it "builds a Rack endpoint with .middleware and .handle_request" do
209
- @builder = Rack::Builder.new
210
- Rack::Builder.stub(:new) { @builder }
211
-
212
- mw1 = Class.new do
213
- attr_reader :app
214
- def initialize(app); @app = app; end
178
+ before :each do
179
+ @builder = stub "builder"
180
+ Rack::Builder.stub(:new).and_return @builder
181
+ end
182
+
183
+ it "builds a Rack endpoint" do
184
+ mw = proc do |pos|
185
+ Class.new do
186
+ attr_reader :app, :pos
187
+ def initialize(app); @app, @pos = app, pos; end
188
+ end
215
189
  end
216
- mw2 = mw1.clone
217
- subject.middleware << mw1 << mw2
190
+ subject.stub(:middleware).and_return [mw.call(0), mw.call(1)]
218
191
 
219
- prc = nil
220
- @builder.should_receive(:use).with(mw1).once
221
- @builder.should_receive(:use).with(mw2).once
222
- @builder.should_receive(:run).with(duck_type(:call)).once {|p| prc = p }
223
- subject.to_app
192
+ pending "what about middleware insertion order?"
193
+ end
194
+
195
+ it "handles middleware arguments" do
196
+ @builder.stub(:run)
197
+ @builder.stub(:to_app)
224
198
 
225
- req_env = {"key" => "value"}
226
- res = [200, {}, ""]
227
- subject.should_receive(:handle_request).with(req_env) {|r_e| res }
228
- prc.call(req_env).should == res
199
+ mw = [
200
+ [stub("mw#0"), stub("mw arg#0"), stub("mw arg#1")],
201
+ stub("mw#1")
202
+ ]
203
+ subject.stub(:middleware).and_return mw
204
+
205
+ @builder.should_receive(:use).with *mw[0]
206
+ @builder.should_receive(:use).with mw[1]
207
+ subject.to_app
229
208
  end
230
209
  end
231
210
 
232
211
  describe ".call" do
233
212
  it "forwards to .to_app.call" do
234
- req = {"key" => "value"}
235
- resp = Rack::Response.new("hello!").finish
236
- app = double "app"
237
- subject.should_receive(:to_app) { app }
238
- app.should_receive(:call).with(req) { resp }
213
+ req, resp = stub("request"), stub("response")
214
+ subject.stub(:to_app).and_return stub("app")
215
+
216
+ subject.to_app.should_receive(:call).with(req).and_return resp
239
217
  subject.call(req).should == resp
240
218
  end
241
219
  end
242
220
 
243
- describe ".handle_request" do
244
- it "invokes the :request hookable" do
245
- req = Rack::Request.new "key" => "value"
246
- Rack::Request.stub(:new).with(req.env) { req }
247
- res = Rack::Response.new
248
- subject.boot
249
- subject.hooking.should_receive(:invoke).with(:request, req) {|n, r_e| res }
250
- subject.handle_request(req.env).should == res.finish
221
+ describe "::DEFAULT_APP" do
222
+ it "calls .request" do
223
+ req, resp = stub("request"), stub("response")
224
+ Pakada.should_receive(:request).with(req).and_return resp
225
+
226
+ Pakada::DEFAULT_APP.call(req).should == resp
251
227
  end
252
228
  end
253
229
 
254
- describe ".hookable(:request)" do
255
- before :each do
256
- subject.boot
257
- @req = Rack::Request.new "key" => "value"
258
- @resp = Rack::Response.new("foo!", 201)
259
- end
260
-
261
- it "calls .app's return value with the request environment" do
262
- subject.app = stub "app"
263
- subject.app.should_receive(:call).with(@req.env) { @resp.finish }
264
- subject.hooking.invoke(:request, @req)
265
- end
266
-
267
- it "calls DEFAULT_APP if .app isn't set" do
268
- Pakada::DEFAULT_APP.should_receive(:call).with(@req.env) { @resp.finish }
269
- subject.hooking.invoke(:request, @req)
270
- end
271
-
272
- it "returns a Rack-compliant response" do
273
- Pakada.app = stub "app", :call => @resp.finish
274
- resp = subject.hooking.invoke(:request, @req)
275
- resp.status.should == @resp.status
276
- resp.headers.should == @resp.headers
277
- resp.body.to_s.should == @resp.body.to_s
230
+ describe ".request" do
231
+ it "prints a tender welcome greeting" do
232
+ Pakada.request({})[2].body.to_s.should =~ /Pakada #{Pakada::VERSION}/
278
233
  end
279
234
  end
280
235
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pakada
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
4
+ prerelease:
5
+ version: 0.2.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Lars Gierth
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-01-13 00:00:00 +01:00
13
+ date: 2011-04-20 00:00:00 +02:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -22,13 +18,9 @@ dependencies:
22
18
  requirement: &id001 !ruby/object:Gem::Requirement
23
19
  none: false
24
20
  requirements:
25
- - - ~>
21
+ - - ">="
26
22
  - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 2
30
- - 1
31
- version: 1.2.1
23
+ version: "0"
32
24
  type: :runtime
33
25
  prerelease: false
34
26
  version_requirements: *id001
@@ -39,11 +31,7 @@ dependencies:
39
31
  requirements:
40
32
  - - ~>
41
33
  - !ruby/object:Gem::Version
42
- segments:
43
- - 0
44
- - 1
45
- - 0
46
- version: 0.1.0
34
+ version: "0.2"
47
35
  type: :runtime
48
36
  prerelease: false
49
37
  version_requirements: *id002
@@ -52,60 +40,34 @@ dependencies:
52
40
  requirement: &id003 !ruby/object:Gem::Requirement
53
41
  none: false
54
42
  requirements:
55
- - - ~>
43
+ - - ">="
56
44
  - !ruby/object:Gem::Version
57
- segments:
58
- - 2
59
- - 3
60
- version: "2.3"
45
+ version: "0"
61
46
  type: :development
62
47
  prerelease: false
63
48
  version_requirements: *id003
64
49
  - !ruby/object:Gem::Dependency
65
- name: mocha
50
+ name: fakefs
66
51
  requirement: &id004 !ruby/object:Gem::Requirement
67
52
  none: false
68
53
  requirements:
69
- - - ~>
54
+ - - ">="
70
55
  - !ruby/object:Gem::Version
71
- segments:
72
- - 0
73
- - 9
74
- - 10
75
- version: 0.9.10
56
+ version: "0"
76
57
  type: :development
77
58
  prerelease: false
78
59
  version_requirements: *id004
79
60
  - !ruby/object:Gem::Dependency
80
- name: fakefs
61
+ name: fakefs-require
81
62
  requirement: &id005 !ruby/object:Gem::Requirement
82
63
  none: false
83
64
  requirements:
84
- - - ~>
65
+ - - ">="
85
66
  - !ruby/object:Gem::Version
86
- segments:
87
- - 0
88
- - 3
89
- - 1
90
- version: 0.3.1
67
+ version: "0"
91
68
  type: :development
92
69
  prerelease: false
93
70
  version_requirements: *id005
94
- - !ruby/object:Gem::Dependency
95
- name: fakefs-require
96
- requirement: &id006 !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ~>
100
- - !ruby/object:Gem::Version
101
- segments:
102
- - 0
103
- - 2
104
- - 1
105
- version: 0.2.1
106
- type: :development
107
- prerelease: false
108
- version_requirements: *id006
109
71
  description:
110
72
  email:
111
73
  - lars.gierth@gmail.com
@@ -129,7 +91,7 @@ files:
129
91
  - spec/pakada_spec.rb
130
92
  - spec/spec_helper.rb
131
93
  has_rdoc: true
132
- homepage: http://rubygems.org/gems/pakada
94
+ homepage: https://rubygems.org/gems/pakada
133
95
  licenses: []
134
96
 
135
97
  post_install_message:
@@ -142,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
104
  requirements:
143
105
  - - ">="
144
106
  - !ruby/object:Gem::Version
145
- hash: -268766609
107
+ hash: 895770369
146
108
  segments:
147
109
  - 0
148
110
  version: "0"
@@ -151,14 +113,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
113
  requirements:
152
114
  - - ">="
153
115
  - !ruby/object:Gem::Version
154
- hash: -268766609
116
+ hash: 895770369
155
117
  segments:
156
118
  - 0
157
119
  version: "0"
158
120
  requirements: []
159
121
 
160
122
  rubyforge_project:
161
- rubygems_version: 1.3.7
123
+ rubygems_version: 1.6.2
162
124
  signing_key:
163
125
  specification_version: 3
164
126
  summary: Very Extensible HTTP Container For Ruby