pakada-dispatch 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - ruby-1.8.7
3
+ - ruby-1.9.2
4
+ - ruby-head
5
+ - jruby
6
+ - rbx-2.0
7
+ - ree
data/Gemfile CHANGED
@@ -7,3 +7,5 @@ gem "awesome_print"
7
7
 
8
8
  gem "thin"
9
9
  gem "shotgun"
10
+
11
+ gem "hatetepe", :path => "../hatetepe"
data/Rakefile CHANGED
@@ -1,9 +1,4 @@
1
- require "bundler"
2
- Bundler.setup :development
1
+ task :default => :spec
3
2
 
4
3
  require "rspec/core/rake_task"
5
-
6
- task :default => :spec
7
4
  RSpec::Core::RakeTask.new :spec
8
-
9
- Bundler::GemHelper.install_tasks
@@ -17,17 +17,30 @@ class Pakada
17
17
 
18
18
  attr_reader :options, :params, :request, :response
19
19
 
20
- def initialize(env, options = {})
20
+ def initialize(env, options = {}, &block)
21
21
  @request = Rack::Request.new(env)
22
22
  @response = options[:response] || Rack::Response.new
23
23
 
24
- @options, @params = options, env["router.params"]
24
+ @options, @params = options, request.env["router.params"]
25
+ (request.env["pakada.controllers"] ||= []) << self
26
+
27
+ catch(:finish) { instance_eval &block }
25
28
  end
26
29
 
27
30
  def finish!
28
31
  throw :finish
29
32
  end
30
33
 
34
+ def actions
35
+ self.class.actions
36
+ end
37
+
38
+ def action(name, options = {})
39
+ aecktschn = self.class.action(name)
40
+ options[:response] ||= response
41
+ proc { aecktschn.call request.env, options }
42
+ end
43
+
31
44
  module ClassMethods
32
45
  def actions
33
46
  @actions ||= {}
@@ -35,10 +48,10 @@ class Pakada
35
48
 
36
49
  def action(name, options = {}, &block)
37
50
  if block
38
- actions[name] = proc {|env, opts = {}|
39
- c = self.new(env, options.merge(opts))
40
- catch(:finish) { c.instance_eval &block }
41
- c.response.finish
51
+ actions[name] = proc {|env, *args|
52
+ opts = args[0] || {}
53
+ action = self.new(env, options.merge(opts), &block)
54
+ action.response.finish unless opts[:response]
42
55
  }
43
56
  end
44
57
  actions[name]
@@ -48,11 +61,11 @@ class Pakada
48
61
  method(:call).to_proc
49
62
  end
50
63
 
51
- def call(env)
64
+ def call(env, options = {})
52
65
  env["router.params"] ||= {}
53
66
  name = env["router.params"][:action] ||
54
67
  env["REQUEST_METHOD"].downcase
55
- action(name.to_sym).call env
68
+ action(name.to_sym).call env, options
56
69
  end
57
70
  end
58
71
  end
@@ -4,9 +4,7 @@ class Pakada
4
4
  class Dispatch
5
5
  module Module
6
6
  def controller_path
7
- # TODO change this when Pakada uses Pathname for module paths
8
- #@controller_path ||= path.join("controllers")
9
- @controller_path ||= Pathname.new(path).join("controllers")
7
+ @controller_path ||= path.join("controllers")
10
8
  end
11
9
 
12
10
  def controllers
@@ -1,5 +1,5 @@
1
1
  class Pakada
2
2
  class Dispatch
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_dependency "pakada"
16
16
  s.add_dependency "http_router"
17
+ s.add_dependency "rack"
17
18
 
18
19
  s.add_development_dependency "rspec"
19
20
  s.add_development_dependency "yard"
@@ -4,11 +4,14 @@ describe Pakada::Dispatch::Controller do
4
4
  let(:block) { proc {} }
5
5
  let(:ctrlr_mod) { Pakada.safety Pakada::Dispatch::Controller }
6
6
  let(:ctrlr) { ctrlr_mod.create &block }
7
+ let(:ctrlr_obj) { ctrlr.new env, options, &block }
8
+ let(:env) { {"router.params" => stub("router params")} }
9
+ let(:options) { {:response => stub("response")} }
7
10
 
8
11
  context ".included hook" do
9
12
  it "extends the includer class with ClassMethods" do
10
13
  mod = Pakada.safety(Pakada::Dispatch::Controller::ClassMethods)
11
- ctrlr.singleton_class.included_modules.should include(mod)
14
+ (class << ctrlr; self; end).included_modules.should include(mod)
12
15
  end
13
16
  end
14
17
 
@@ -25,19 +28,16 @@ describe Pakada::Dispatch::Controller do
25
28
  end
26
29
  end
27
30
 
28
- context "#initialize(env, options)" do
29
- let(:env) { {"router.params" => stub("router params")} }
30
- let(:options) { {:response => stub("response")} }
31
- let(:ctrlr_obj) { ctrlr.new env, options }
32
-
31
+ context "#initialize(env, options) { ... }" do
33
32
  it "sets the options and routing params objects" do
34
33
  ctrlr_obj.options.should equal(options)
35
34
  ctrlr_obj.params.should equal(env["router.params"])
36
35
  end
37
36
 
38
37
  it "sets the request and response objects" do
38
+ response = options[:response]
39
39
  ctrlr_obj.request.env.should equal(env)
40
- ctrlr_obj.response.should equal(options[:response])
40
+ ctrlr_obj.response.should equal(response)
41
41
  end
42
42
 
43
43
  it "defaults to an empty response object" do
@@ -45,23 +45,77 @@ describe Pakada::Dispatch::Controller do
45
45
  ctrlr_obj.response.status.should equal(200)
46
46
  ctrlr_obj.response.should_not equal(old_response)
47
47
  end
48
+
49
+ it "adds itself to env[pakada.controllers]" do
50
+ ctrlr_obj
51
+ env["pakada.controllers"].should == [ctrlr_obj]
52
+ end
53
+
54
+ let(:ctrlr_alloc) { ctrlr.allocate }
55
+
56
+ it "instance_evals the passed block, catching :finish" do
57
+ ctrlr_alloc.should_receive(:instance_eval) {|&blk|
58
+ blk.should equal(block)
59
+ throw :catch
60
+ }
61
+ proc {
62
+ ctrlr_alloc.send :initialize, env, &block
63
+ }.should throw_symbol(:catch)
64
+ end
48
65
  end
49
66
 
50
67
  context "#finish!" do
51
68
  it "throws :finish" do
52
- proc { ctrlr.new({}).finish! }.should throw_symbol(:finish)
69
+ proc { ctrlr_obj.finish! }.should throw_symbol(:finish)
70
+ end
71
+ end
72
+
73
+ context "#actions" do
74
+ let(:actions) { stub "action" }
75
+
76
+ it "forwards to .actions" do
77
+ ctrlr.should_receive(:actions) { actions }
78
+ ctrlr_obj.actions.should equal(actions)
79
+ end
80
+ end
81
+
82
+ context "#action(name, options = {})" do
83
+ before {
84
+ ctrlr_obj.actions[:foo] = stub("action")
85
+ }
86
+
87
+ it "forwards to .action" do
88
+ ctrlr.action(:foo).should_receive(:call) {|env, opts|
89
+ env.should equal(ctrlr_obj.request.env)
90
+ }
91
+ ctrlr_obj.action(:foo).call
92
+ end
93
+
94
+ it "passes the response in options" do
95
+ ctrlr.action(:foo).should_receive(:call) {|env, opts|
96
+ opts.should == options
97
+ }
98
+ ctrlr_obj.action(:foo).call
53
99
  end
54
100
  end
55
101
 
56
102
  context ".actions" do
57
103
  it "is an empty Hash" do
58
- ctrlr.actions.should be_an(Hash)
104
+ ctrlr.actions.should be_a(Hash)
59
105
  ctrlr.actions.should be_empty
60
106
  end
61
107
  end
62
108
 
63
- context ".action(name, options) { ... }" do
64
- let(:block) { proc {} }
109
+ context ".action(name)" do
110
+ let(:action) { stub "action" }
111
+ before { ctrlr.actions[:foo] = action }
112
+
113
+ it "returns the specified action" do
114
+ ctrlr.action(:foo).should equal(action)
115
+ end
116
+ end
117
+
118
+ context ".action(name, options = {}) { ... }" do
65
119
  let(:options) { {:foo => stub("foo option")} }
66
120
  let(:override_options) { {:bar => stub("bar option")} }
67
121
  let(:merged_options) { options.merge override_options }
@@ -78,27 +132,21 @@ describe Pakada::Dispatch::Controller do
78
132
  ctrlr.action(:foo).should respond_to(:call)
79
133
  end
80
134
 
81
- it "'s proc creates a controller object passing env and options" do
82
- ctrlr.should_receive(:new).with(env, merged_options) { ctrlr_obj }
135
+ it "'s proc creates a controller object passing env, options and block" do
136
+ ctrlr.should_receive(:new) {|e, opts, &blk|
137
+ e.should equal(env)
138
+ opts.should == merged_options
139
+ blk.should equal(block)
140
+ }
83
141
  ctrlr.action(:foo).call env, override_options
84
142
  end
85
143
 
86
- it "'s proc instance_evals the action proc catching :finish" do
87
- ctrlr_obj.should_receive(:instance_eval) {|&blk| block.should equal(blk) }
88
- ctrlr.action(:foo).call env
89
- end
90
-
91
144
  it "'s proc returns the finished response" do
92
145
  ctrlr.action(:foo).call(env).should equal(response.finish)
93
146
  end
94
- end
95
-
96
- context ".action(name)" do
97
- let(:action) { stub "action" }
98
- before { ctrlr.actions[:foo] = action }
99
147
 
100
- it "returns the specified action" do
101
- ctrlr.action(:foo).should equal(action)
148
+ it "'s proc returns nothing if a response gets passed in via options" do
149
+ ctrlr.action(:foo).call(env, :response => response).should be_nil
102
150
  end
103
151
  end
104
152
 
@@ -128,7 +176,7 @@ describe Pakada::Dispatch::Controller do
128
176
  }
129
177
 
130
178
  it "sets default routing params" do
131
- ctrlr.action(:bar).should_receive(:call) {|e|
179
+ ctrlr.action(:bar).should_receive(:call) {|e, opts|
132
180
  e["router.params"].should == {}
133
181
  }
134
182
 
@@ -44,8 +44,8 @@ describe Pakada::Dispatch do
44
44
  mod = Pakada.safety(Pakada::Dispatch::Module)
45
45
 
46
46
  dispatch.boot
47
- module1.singleton_class.included_modules.should include(mod)
48
- module2.singleton_class.included_modules.should include(mod)
47
+ (class << module1; self; end).included_modules.should include(mod)
48
+ (class << module2; self; end).included_modules.should include(mod)
49
49
  end
50
50
 
51
51
  it "loads each module's controllers" do
@@ -40,7 +40,7 @@ describe Pakada::Dispatch::Module do
40
40
  }
41
41
 
42
42
  it "creates a controller class from the given block" do
43
- Pakada.safety(Pakada::Dispatch::Controller)
43
+ Pakada.safety(Pakada::Dispatch::Controller) \
44
44
  .should_receive(:create) {|&blk|
45
45
  blk.should equal(block)
46
46
  controller
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pakada-dispatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-12 00:00:00.000000000Z
12
+ date: 2011-08-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pakada
16
- requirement: &79590830 !ruby/object:Gem::Requirement
16
+ requirement: &75057660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *79590830
24
+ version_requirements: *75057660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: http_router
27
- requirement: &79590540 !ruby/object:Gem::Requirement
27
+ requirement: &75057450 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *79590540
35
+ version_requirements: *75057450
36
+ - !ruby/object:Gem::Dependency
37
+ name: rack
38
+ requirement: &75057240 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *75057240
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rspec
38
- requirement: &79590290 !ruby/object:Gem::Requirement
49
+ requirement: &75057010 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *79590290
57
+ version_requirements: *75057010
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: yard
49
- requirement: &79589990 !ruby/object:Gem::Requirement
60
+ requirement: &75056780 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *79589990
68
+ version_requirements: *75056780
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: rdiscount
60
- requirement: &79589700 !ruby/object:Gem::Requirement
71
+ requirement: &75056570 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,7 +76,7 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *79589700
79
+ version_requirements: *75056570
69
80
  description:
70
81
  email:
71
82
  - lars.gierth@gmail.com
@@ -74,6 +85,7 @@ extensions: []
74
85
  extra_rdoc_files: []
75
86
  files:
76
87
  - .rspec
88
+ - .travis.yml
77
89
  - .yardopts
78
90
  - Gemfile
79
91
  - LICENSE