GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Resource-oriented open source Ruby framework for Web apps.
Homepage: http://rubywaves.com/
Clone URL: git://github.com/dyoder/waves.git
filters and handlers working with new mapping code
automatthew (author)
Thu Aug 21 22:01:01 -0700 2008
commit  2bb220cd50dcb3d54718bb07c02dad73d6afb61c
tree    00c12270bfc000c1c7dba08ffb1ad2d7c24fe705
parent  6e07a8141af4bd79fd2105e2938498fe4ff50190
...
47
48
49
50
 
51
52
 
53
54
55
...
47
48
49
 
50
51
 
52
53
54
55
0
@@ -47,9 +47,9 @@ module Waves
0
 
0
         begin
0
 
0
- request.not_found if mapping[ :response ].empty?
0
+ request.not_found if mapping[ :main ].empty?
0
           mapping[ :before ].each { | action | action.call( request ) }
0
- response.write( mapping[ :response ].first.call( request ) )
0
+ response.write( mapping[ :main ].first.call( request ) )
0
           mapping[ :after ].each { | action | action.call( request ) }
0
           
0
         rescue Exception => e
...
45
46
47
48
 
49
50
51
52
53
54
55
56
57
58
59
60
 
61
62
 
 
 
 
 
 
 
 
 
63
64
 
 
 
65
 
 
 
 
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
90
91
92
...
106
107
108
109
 
110
111
112
113
 
114
115
116
...
45
46
47
 
48
49
50
51
52
53
 
 
 
 
 
 
54
55
56
 
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
 
 
 
 
81
82
83
84
85
 
 
 
86
87
88
89
90
 
91
92
93
94
...
108
109
110
 
111
112
113
114
 
115
116
117
118
0
@@ -45,48 +45,50 @@ module Waves
0
     include Functor::Method
0
     
0
     METHODS = %w( get put post delete ).map( &:intern )
0
- RULES = %w( before response after always handle ).map( &:intern )
0
+ RULES = %w( before after always ).map( &:intern )
0
     
0
     def mappings ; @mappings ||= Hash.new { |h,k| h[k] = [] } ; end
0
 
0
     def clear ; @mappings = Hash.new { |h,k| h[k] = [] } ; end
0
     
0
- def method_missing( name, *args, &block )
0
- return super unless RULES.include? name
0
- args << block if block_given?
0
- mappings[ name ] << map( *args )
0
- end
0
-
0
     def on( options, &block )
0
+ @type ||= :main
0
       args = [options, block].compact
0
- mappings[ :response ] << map( *args )
0
+ mappings[ @type ] << map( *args )
0
+ end
0
+
0
+ def before(&block)
0
+ @type = :before; yield if block; @type = nil
0
+ end
0
+
0
+ def after(&block)
0
+ @type = :after; yield if block; @type = nil
0
     end
0
     
0
+ def always(&block)
0
+ @type = :always; yield if block; @type = nil
0
+ end
0
     
0
+ def handle(exception, options={}, &block)
0
+ args = [ exception, options, block].compact
0
+ mappings[ :handle ] << map( *args )
0
+ end
0
     
0
     def with( options, &block )
0
       @options = options; yield if block_given? ; @options = nil
0
     end
0
 
0
- # primary input like: response( :klump, :get => [ "foo" ] ) { bar }
0
- functor( :map, Symbol, Hash, Proc ) do | name, options, block |
0
- options[:as] = name ; options[:block] = block ; map( options )
0
- end # no longer needed?
0
-
0
     # primary input like: on( :get => [ "foo" ], :as => :klump ) { bar }
0
     functor( :map, Hash, Proc ) do | options, block |
0
       options[:block] = block; map( options )
0
     end
0
     
0
- # primary input like response( :klump, :get => [ "foo" ] ); missing a block
0
- functor( :map, Symbol, Hash ) { | name, options | options[:as] = name ; map( options ) } # no longer needed?
0
-
0
     # secondary input where &block has already been slurped into the options hash
0
     functor( :map, Hash ) do | options |
0
       raise ArgumentError, "A mapping must have a block or an :as param" if !options[:as] && !options[:block]
0
       options = ( @options || {} ).merge( options )
0
       options[ :method ] = method = METHODS.find { |method| options[ method ] }
0
- options[ :path ] = options[ method ] if method
0
+ options[ :path ] = options[ method || :any ]
0
       Action.new( options )
0
     end
0
 
0
@@ -106,11 +108,11 @@ module Waves
0
     
0
     def []( request )
0
       results = Hash.new { |h,k| h[k] = [] }
0
- RULES.each do | rule |
0
+ (RULES + [ :handle, :main] ).each do | rule |
0
         mappings[ rule ].each { | action | ( binding = action.bind( request ) ) and results[ rule ].push( binding ) }
0
       end
0
       return results
0
- end
0
+ end
0
 
0
     private :map
0
     
...
9
10
11
12
13
 
14
15
16
17
18
 
 
 
 
 
 
19
20
21
 
22
23
24
...
26
27
28
29
30
 
31
32
33
34
35
36
37
 
 
 
 
 
38
39
40
41
42
 
43
44
45
46
 
 
 
 
 
 
47
48
49
...
56
57
58
59
 
60
61
62
63
64
 
65
66
67
 
 
 
 
 
68
69
70
...
9
10
11
 
 
12
13
14
15
 
 
16
17
18
19
20
21
22
23
 
24
25
26
27
...
29
30
31
 
 
32
33
 
34
35
36
 
 
37
38
39
40
41
42
43
44
45
46
47
48
 
 
 
49
50
51
52
53
54
55
56
57
...
64
65
66
 
67
68
 
 
69
70
71
72
 
 
73
74
75
76
77
78
79
80
0
@@ -9,16 +9,19 @@ STATE = []
0
 describe "A before filter" do
0
   
0
   before do
0
- mapping.clear
0
- mapping.response( :get => [ "somewhere" ] ) { "main" }
0
+ mappings.clear
0
   end
0
   
0
   it "is evaluated before the action" do
0
- mapping.before( :mapping_name, :get => [ "somewhere"] ) do
0
- response.body << "before, "
0
+ mappings do
0
+ before do
0
+ on( :get => [ "somewhere"] ) { response.body << "before, " }
0
+ on( :get => [ true ] ) { response.body << "later, " }
0
+ end
0
+ on( :get => [ "somewhere" ] ) { "main" }
0
     end
0
     
0
- mock_request.get("/somewhere").body.should == "before, main"
0
+ mock_request.get("/somewhere").body.should == "before, later, main"
0
   end
0
   
0
 end
0
@@ -26,24 +29,29 @@ end
0
 describe "An after filter" do
0
   
0
   before do
0
- STATE[0] = "foo"
0
- mapping.clear
0
+ mappings.clear
0
     handle( ArgumentError ) { response.status = 404 }
0
- mapping.response( :get => [ "somewhere" ] ) { "main" }
0
   end
0
   
0
   it "is evaluated after the action" do
0
- mapping.after( :mapping_name, :get => [ "somewhere"] ) do
0
- response.body << ", after"
0
+ mappings do
0
+ on( :get => [ "somewhere" ] ) { "main" }
0
+ after do
0
+ on(:get => [ "somewhere"] ) { response.body << ", after" }
0
+ end
0
     end
0
     
0
     mock_request.get("/somewhere").body.should == "main, after"
0
   end
0
   
0
+ # this is testing Dispatcher behavior. Needs a rethink
0
   it "is not evaluated if the action raises an exception" do
0
- mapping.response( :get => [ "elsewhere" ] ) { raise ArgumentError }
0
- mapping.after( :mapping_name, :get => [ "elsewhere"] ) do
0
- STATE[0] = "bar"
0
+ STATE[0] = "foo"
0
+ mappings do
0
+ on( :get => [ "elsewhere" ] ) { raise ArgumentError }
0
+ after do
0
+ on(:get => [ "elsewhere"] ) { STATE[0] = "bar" }
0
+ end
0
     end
0
     
0
     mock_request.get("/elsewhere")
0
@@ -56,15 +64,17 @@ describe "An always filter" do
0
   
0
   before do
0
     STATE[0] = "foo"
0
- mapping.clear
0
+ mappings.clear
0
     handle( ArgumentError ) { response.status = 404 }
0
-
0
- mapping.response( :get => [ "somewhere" ] ) { raise ArgumentError }
0
   end
0
   
0
+ # this is also testing Dispatcher behavior.
0
   it "is evaluated no matter what horrible things may happen in the action" do
0
- mapping.always( :mapping_name, :get => [ "somewhere"] ) do
0
- STATE[0] = "bar"
0
+ mappings do
0
+ on( :get => [ "somewhere" ] ) { raise ArgumentError }
0
+ always do
0
+ on(:get => [ "somewhere"] ) { STATE[0] = "bar" }
0
+ end
0
     end
0
     
0
     mock_request.get("/somewhere")
...
16
17
18
 
19
20
21
...
23
24
25
 
 
 
26
27
28
...
16
17
18
19
20
21
22
...
24
25
26
27
28
29
30
31
32
0
@@ -16,6 +16,7 @@ describe "In a mapping's path-matcher" do
0
         on( :put => [ 'somewhere' ] ) { "PUT method" }
0
         on( :post => [ 'somewhere' ] ) { "POST method" }
0
         on( :delete => [ 'somewhere' ] ) { "DELETE method" }
0
+ on( :any => [ 'anywhere' ] ) { "Whatever" }
0
       end
0
 
0
     
0
@@ -23,6 +24,9 @@ describe "In a mapping's path-matcher" do
0
       mock_request.put("/somewhere").body.should == "PUT method"
0
       mock_request.post("/somewhere").body.should == "POST method"
0
       mock_request.delete("/somewhere").body.should == "DELETE method"
0
+
0
+ mock_request.get("/anywhere").body.should == "Whatever"
0
+ mock_request.post("/anywhere").body.should == "Whatever"
0
     end
0
     
0
   end

Comments

    No one has commented yet.