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
Resource#action merged with #controller; #render merged with #view
automatthew (author)
Thu Aug 21 10:03:05 -0700 2008
commit  abe88ed145d083a3ab29fdb359a4273f110fadf1
tree    a47eb9b924293473266b2b21f57380617d90fdf3
parent  7e19c959c4fc2bfcf96034911c0f3426f4064d2b
...
53
54
55
56
57
58
59
60
61
 
 
 
 
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
64
65
...
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
0
@@ -53,13 +53,28 @@ module Waves
0
           auto_create_class :Default, Waves::Resources::Base
0
           auto_load :Default, :directories => [ :resources ]
0
           auto_eval :Default do
0
- def controller ; @controller ||= app::Controllers[ singular ].process( @request ) { self } ; end
0
- def view ; @view ||= app::Views[ singular ].process( @request ) { self } ; end
0
- def action( method, *args ) ; @data = controller.send( method, *args ) ; end
0
- def render( method, assigns = nil )
0
- assigns ||= { ( @data.kind_of?( Enumerable ) ? plural : singular ) => @data }
0
- view.send( method, assigns)
0
+
0
+ def controller( method = nil, *args, &block )
0
+ @controller ||= app::Controllers[ singular ].process( @request ) { self }
0
+ @data = @controller.send( method, *args ) if method
0
+ @controller
0
             end
0
+
0
+ def view( method = nil, assigns = nil)
0
+ @view ||= app::Views[ singular ].process( @request ) { self }
0
+ if method
0
+ assigns ||= { ( @data.kind_of?( Enumerable ) ? plural : singular ) => @data }
0
+ @view.send( method, assigns)
0
+ else
0
+ @view
0
+ end
0
+ end
0
+
0
+ def render( path, assigns = {} )
0
+ Views.render(path, assigns)
0
+ end
0
+
0
+ # can't distinguish nil param values from NoMethodError. need to super, or something
0
             def method_missing( name, *args, &block) ; params[ name.to_s ] ; end
0
           end
0
           auto_create_class true, app::Resources::Default
...
6
7
8
9
 
10
11
12
...
16
17
18
19
20
21
22
23
24
25
 
 
 
 
 
 
 
 
 
26
27
28
29
 
 
 
30
31
32
 
 
33
34
35
36
 
 
 
37
38
39
40
 
 
41
42
43
44
45
46
47
...
6
7
8
 
9
10
11
12
...
16
17
18
 
 
 
 
 
 
 
19
20
21
22
23
24
25
26
27
28
 
 
 
29
30
31
32
 
 
33
34
35
 
 
 
36
37
38
39
 
 
 
40
41
42
43
44
 
45
46
47
0
@@ -6,7 +6,7 @@ module Waves
0
     # Just include this in your Renderer class and write your render method.
0
     module Mixin
0
 
0
- # Adds the following methods to the target class:
0
+ # Adds the following methods to the mod class:
0
       #
0
       # - extension: allows you to set or get the extension used by this renderer.
0
       #
0
@@ -16,32 +16,32 @@ module Waves
0
       # - template: read the template from the file corresponding to the given logical path.
0
       # - helper: return a helper module that corresponds to the given logical path.
0
       #
0
- def self.included(target)
0
- class << target
0
-
0
- def extension(*args)
0
- return @extension if args.length == 0
0
- @extension = args.first
0
- end
0
+ def self.included(mod)
0
+
0
+ # Register the renderer with the Views module
0
+ Views.renderers << mod
0
+
0
+ def mod.extension(*args)
0
+ return @extension if args.length == 0
0
+ @extension = args.first
0
+ end
0
 
0
- def filename(path)
0
- :templates / "#{path}.#{self.extension}"
0
- end
0
+ def mod.filename(path)
0
+ :templates / "#{path}.#{self.extension}"
0
+ end
0
 
0
- def render(path,args=nil)
0
- end
0
+ def mod.render(path,args=nil)
0
+ end
0
 
0
- def template( path )
0
- File.read( filename( path ) )
0
- end
0
+ def mod.template( path )
0
+ File.read( filename( path ) )
0
+ end
0
 
0
- def helper( path )
0
- Waves.main[ :helpers ][ File.basename( File.dirname( path ) ).camel_case ]
0
- end
0
+ def mod.helper( path )
0
+ Waves.main[ :helpers ][ File.basename( File.dirname( path ) ).camel_case ]
0
         end
0
       end
0
 
0
-
0
     end
0
 
0
   end
...
55
56
57
58
59
60
61
62
63
64
65
 
 
 
 
 
 
 
66
67
 
 
 
 
 
 
68
69
 
 
 
 
70
71
72
...
87
88
89
90
91
92
 
93
94
95
96
97
98
99
 
 
 
 
100
101
 
102
103
 
104
105
106
107
108
 
109
110
111
...
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
...
96
97
98
 
 
 
99
100
101
 
 
 
 
 
102
103
104
105
106
107
108
109
 
110
111
112
113
114
115
116
117
118
119
0
@@ -55,18 +55,27 @@ module Waves
0
 
0
   module Views
0
 
0
- class NoTemplateError < Exception ; end
0
-
0
     # A class method that returns the known Renderers, which is any module that is defined within Waves::Renderers and includes the Renderers::Mixin. You can define new Renderers simply be reopening Waves::Renderers and defining a module that mixes in Renderers::Mixin.
0
- def Views.renderers
0
- return [] if Renderers.constants.nil?
0
- Renderers.constants.sort.inject([]) do |rx,cname|
0
- ( Module === (c=Renderers.const_get(cname)) &&
0
- c < Renderers::Mixin ) ? ( rx << c ) : rx
0
+ def self.renderers
0
+ @renderers ||= []
0
+ end
0
+
0
+ def self.renderer_for(path)
0
+ @renderers.find do |renderer|
0
+ File.exists?( renderer.filename( path ) )
0
       end
0
     end
0
+
0
+ def self.render( path, assigns = {} )
0
+ template = Views.renderer_for(path) || Views.renderer_for( :generic / File.basename(path) )
0
+ raise NoTemplateError.new( path ) if template.nil?
0
+ template.render( path, assigns )
0
+ end
0
 
0
- # The View mixin simply sets up the machinery for invoking a template, along with methods for accessing the request context and the standard interface for invoking a view method.
0
+ class NoTemplateError < Exception # :nodoc:
0
+ end
0
+
0
+ # The View mixin simply sets up the machinery for invoking a template, along with methods for accessing the request assigns and the standard interface for invoking a view method.
0
     module Mixin
0
 
0
       attr_reader :request
0
@@ -87,25 +96,24 @@ module Waves
0
       # Return the first renderer for which a template file can be found.
0
       # Uses Renderers::Mixin.filename to construct the filename for each renderer.
0
       def renderer(path)
0
- Views.renderers.find do |renderer|
0
- File.exists?( renderer.filename( path ) )
0
- end
0
+ Views.renderer_for(path)
0
       end
0
 
0
- def render( path, context = {} )
0
- context.merge!( :request => request )
0
- template = renderer( path ) || renderer( :generic / File.basename(path) )
0
- raise NoTemplateError.new( path ) if template.nil?
0
- template.render( path, context )
0
+ # Render the template found in the directory named after this view (snake cased, of course)
0
+ # E.g. App::Views::Gnome.new.render( "stink" ) would look for templates/gnome/stink.<ext>
0
+ def render( path, assigns = {} )
0
+ Views.render("#{self.class.basename.snake_case}/#{path}", assigns.merge!( :request => request ))
0
       end
0
 
0
+ # Render the template with the name of the missing method.
0
       def method_missing(name,*args)
0
- render( "/#{self.class.basename.snake_case}/#{name}", *args )
0
+ render( name, *args )
0
       end
0
 
0
     end
0
 
0
     # :)
0
+ # An emoticon means never having to say you're sorry.
0
     const_set( :Base, Class.new ).module_eval { include Mixin }
0
 
0
   end
...
7
8
9
10
 
11
12
13
14
 
15
16
17
18
19
20
21
22
 
 
 
 
 
23
24
25
...
7
8
9
 
10
11
12
13
 
14
15
16
17
 
 
 
 
 
18
19
20
21
22
23
24
25
0
@@ -7,19 +7,19 @@ module Blog
0
       extend Waves::Mapping
0
       
0
       response( :get => [] ) { redirect "/waves/status"}
0
- response( :get => [ "waves", "status" ], :resource => :waves) { render( :status ) }
0
+ response( :get => [ "waves", "status" ]) { render "waves/status" }
0
       
0
       # specific to comments - on create redirect to the entry, not the comment itself
0
       response :create, :resource => :comment, :post => [ 'comments' ] do
0
- redirect( Blog.paths( :entry ).read( action( :create ).entry.name ) )
0
+ redirect( Blog.paths( :entry ).read( controller( :create ).entry.name ) )
0
       end
0
       
0
       # defaults for generic resources
0
- response( :list, :get => [ :resources ] ) { action( :all ) and render( :list ) }
0
- response( :create, :post => [ :resources ] ) { redirect( paths.read( action( :create ).name, 'edit' ) ) }
0
- response( :read, :get => [ :resource, :name, { :mode => 'show' } ] ) { action( :find, name ) and render( mode ) }
0
- response( :update, :put => [ :resource, :name ] ) { action( :update, name ) and redirect( paths.read( name ) ) }
0
- response( :delete, :delete => [ :resource, :name ] ) { action( :delete, name ) }
0
+ response( :list, :get => [ :resources ] ) { controller( :all ) and view( :list ) }
0
+ response( :create, :post => [ :resources ] ) { redirect( paths.read( controller( :create ).name, 'edit' ) ) }
0
+ response( :read, :get => [ :resource, :name, { :mode => 'show' } ] ) { controller( :find, name ) and view( mode ) }
0
+ response( :update, :put => [ :resource, :name ] ) { controller( :update, name ) and redirect( paths.read( name ) ) }
0
+ response( :delete, :delete => [ :resource, :name ] ) { controller( :delete, name ) }
0
       
0
     end
0
 
...
4
5
6
7
8
 
9
10
11
12
13
14
 
 
15
16
17
18
 
19
20
21
22
23
24
 
25
26
27
...
4
5
6
 
 
7
8
9
10
 
 
 
11
12
13
14
15
 
16
17
18
19
20
21
22
23
24
25
26
0
@@ -4,24 +4,23 @@ module Blog
0
 
0
     class Production < Default
0
 
0
- database :host => 'localhost', :adapter => 'mysql', :database => 'blog',
0
- :user => 'root', :password => ''
0
+ database :adapter => 'sqlite', :database => 'blog.db'
0
 
0
       reloadable []
0
 
0
- log :level => :error,
0
- :output => ( :log / "waves.#{$$}" ),
0
- :rotation => :weekly
0
+ log :level => :info,
0
+ :output => ( :log / "waves.production" )
0
 
0
       host '0.0.0.0'
0
 
0
- port 80
0
+ port 3000
0
 
0
       handler ::Rack::Handler::Mongrel, :Host => host, :Port => port
0
       # handler ::Rack::Handler::WEBrick, :BindAddress => host, :Port => port
0
       # handler ::Rack::Handler::Thin, :Host => host, :Port => port
0
 
0
       application do
0
+ use ::Rack::Static, :urls => [ '/css', '/javascript', '/favicon.ico' ], :root => 'public'
0
         run ::Waves::Dispatchers::Default.new
0
       end
0
 

Comments

    No one has commented yet.