waves-edge 2009.03.10.13.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. data/bin/waves +30 -0
  2. data/doc/HISTORY +1 -0
  3. data/doc/LICENSE +22 -0
  4. data/doc/README +1 -0
  5. data/doc/VERSION +1 -0
  6. data/lib/caches/file.rb +48 -0
  7. data/lib/caches/memcached.rb +40 -0
  8. data/lib/caches/simple.rb +25 -0
  9. data/lib/caches/synchronized.rb +25 -0
  10. data/lib/commands/console.rb +35 -0
  11. data/lib/commands/generate.rb +52 -0
  12. data/lib/commands/help.rb +5 -0
  13. data/lib/commands/server.rb +68 -0
  14. data/lib/dispatchers/base.rb +68 -0
  15. data/lib/dispatchers/default.rb +25 -0
  16. data/lib/ext/float.rb +13 -0
  17. data/lib/ext/hash.rb +31 -0
  18. data/lib/ext/integer.rb +27 -0
  19. data/lib/ext/kernel.rb +20 -0
  20. data/lib/ext/module.rb +20 -0
  21. data/lib/ext/object.rb +33 -0
  22. data/lib/ext/string.rb +20 -0
  23. data/lib/ext/symbol.rb +11 -0
  24. data/lib/ext/tempfile.rb +5 -0
  25. data/lib/foundations/classic.rb +59 -0
  26. data/lib/foundations/compact.rb +52 -0
  27. data/lib/helpers/basic.rb +11 -0
  28. data/lib/helpers/doc_type.rb +34 -0
  29. data/lib/helpers/extended.rb +21 -0
  30. data/lib/helpers/form.rb +42 -0
  31. data/lib/helpers/formatting.rb +30 -0
  32. data/lib/helpers/layouts.rb +37 -0
  33. data/lib/helpers/model.rb +37 -0
  34. data/lib/helpers/view.rb +22 -0
  35. data/lib/layers/inflect/english.rb +35 -0
  36. data/lib/layers/mvc.rb +41 -0
  37. data/lib/layers/mvc/controllers.rb +41 -0
  38. data/lib/layers/mvc/extensions.rb +52 -0
  39. data/lib/layers/orm/migration.rb +79 -0
  40. data/lib/layers/orm/providers/active_record.rb +84 -0
  41. data/lib/layers/orm/providers/active_record/migrations/empty.rb.erb +9 -0
  42. data/lib/layers/orm/providers/active_record/tasks/generate.rb +28 -0
  43. data/lib/layers/orm/providers/active_record/tasks/schema.rb +22 -0
  44. data/lib/layers/orm/providers/data_mapper.rb +37 -0
  45. data/lib/layers/orm/providers/filebase.rb +25 -0
  46. data/lib/layers/orm/providers/sequel.rb +87 -0
  47. data/lib/layers/orm/providers/sequel/migrations/empty.rb.erb +9 -0
  48. data/lib/layers/orm/providers/sequel/tasks/generate.rb +30 -0
  49. data/lib/layers/orm/providers/sequel/tasks/schema.rb +16 -0
  50. data/lib/layers/renderers/erubis.rb +52 -0
  51. data/lib/layers/renderers/haml.rb +67 -0
  52. data/lib/layers/renderers/markaby.rb +41 -0
  53. data/lib/matchers/accept.rb +21 -0
  54. data/lib/matchers/base.rb +30 -0
  55. data/lib/matchers/content_type.rb +17 -0
  56. data/lib/matchers/path.rb +67 -0
  57. data/lib/matchers/query.rb +21 -0
  58. data/lib/matchers/request.rb +27 -0
  59. data/lib/matchers/resource.rb +19 -0
  60. data/lib/matchers/traits.rb +19 -0
  61. data/lib/matchers/uri.rb +20 -0
  62. data/lib/renderers/mixin.rb +13 -0
  63. data/lib/resources/mixin.rb +136 -0
  64. data/lib/resources/paths.rb +132 -0
  65. data/lib/runtime/configuration.rb +100 -0
  66. data/lib/runtime/console.rb +23 -0
  67. data/lib/runtime/logger.rb +35 -0
  68. data/lib/runtime/mime_types.rb +536 -0
  69. data/lib/runtime/mocks.rb +14 -0
  70. data/lib/runtime/monitor.rb +32 -0
  71. data/lib/runtime/request.rb +152 -0
  72. data/lib/runtime/response.rb +43 -0
  73. data/lib/runtime/response_mixin.rb +54 -0
  74. data/lib/runtime/runtime.rb +69 -0
  75. data/lib/runtime/server.rb +20 -0
  76. data/lib/runtime/session.rb +27 -0
  77. data/lib/runtime/worker.rb +86 -0
  78. data/lib/servers/base.rb +42 -0
  79. data/lib/servers/mongrel.rb +13 -0
  80. data/lib/servers/webrick.rb +13 -0
  81. data/lib/tasks/gem.rb +32 -0
  82. data/lib/tasks/generate.rb +85 -0
  83. data/lib/views/errors.rb +49 -0
  84. data/lib/views/mixin.rb +64 -0
  85. data/lib/waves.rb +63 -0
  86. data/samples/blog/Rakefile +25 -0
  87. data/samples/blog/configurations/default.rb +11 -0
  88. data/samples/blog/configurations/development.rb +29 -0
  89. data/samples/blog/configurations/production.rb +26 -0
  90. data/samples/blog/models/comment.rb +23 -0
  91. data/samples/blog/models/entry.rb +31 -0
  92. data/samples/blog/public/css/site.css +13 -0
  93. data/samples/blog/public/javascript/jquery-1.2.6.min.js +32 -0
  94. data/samples/blog/public/javascript/site.js +13 -0
  95. data/samples/blog/resources/entry.rb +39 -0
  96. data/samples/blog/resources/map.rb +9 -0
  97. data/samples/blog/schema/migrations/001_initial_schema.rb +17 -0
  98. data/samples/blog/schema/migrations/002_add_comments.rb +18 -0
  99. data/samples/blog/schema/migrations/templates/empty.rb.erb +9 -0
  100. data/samples/blog/startup.rb +8 -0
  101. data/samples/blog/templates/comment/add.mab +12 -0
  102. data/samples/blog/templates/comment/list.mab +6 -0
  103. data/samples/blog/templates/entry/edit.mab +14 -0
  104. data/samples/blog/templates/entry/list.mab +16 -0
  105. data/samples/blog/templates/entry/show.mab +18 -0
  106. data/samples/blog/templates/entry/summary.mab +9 -0
  107. data/samples/blog/templates/errors/not_found_404.mab +7 -0
  108. data/samples/blog/templates/errors/server_error_500.mab +2 -0
  109. data/samples/blog/templates/layouts/default.mab +19 -0
  110. data/samples/blog/templates/waves/status.mab +85 -0
  111. data/templates/classic/Rakefile +90 -0
  112. data/templates/classic/configurations/default.rb.erb +9 -0
  113. data/templates/classic/configurations/development.rb.erb +26 -0
  114. data/templates/classic/configurations/production.rb.erb +29 -0
  115. data/templates/classic/controllers/.gitignore +0 -0
  116. data/templates/classic/helpers/.gitignore +0 -0
  117. data/templates/classic/lib/tasks/.gitignore +0 -0
  118. data/templates/classic/models/.gitignore +0 -0
  119. data/templates/classic/public/css/.gitignore +0 -0
  120. data/templates/classic/public/flash/.gitignore +0 -0
  121. data/templates/classic/public/images/.gitignore +0 -0
  122. data/templates/classic/public/javascript/.gitignore +0 -0
  123. data/templates/classic/resources/.gitignore +0 -0
  124. data/templates/classic/resources/map.rb.erb +8 -0
  125. data/templates/classic/schema/migrations/.gitignore +0 -0
  126. data/templates/classic/startup.rb.erb +11 -0
  127. data/templates/classic/templates/errors/not_found_404.mab +7 -0
  128. data/templates/classic/templates/errors/server_error_500.mab +7 -0
  129. data/templates/classic/templates/layouts/default.mab +14 -0
  130. data/templates/classic/tmp/sessions/.gitignore +0 -0
  131. data/templates/classic/views/.gitignore +0 -0
  132. data/templates/compact/startup.rb.erb +11 -0
  133. data/test/ext/object.rb +55 -0
  134. data/test/ext/shortcuts.rb +73 -0
  135. data/test/helpers.rb +17 -0
  136. data/test/match/accept.rb +34 -0
  137. data/test/match/methods.rb +22 -0
  138. data/test/match/params.rb +33 -0
  139. data/test/match/path.rb +106 -0
  140. data/test/match/query.rb +40 -0
  141. data/test/process/request.rb +75 -0
  142. data/test/process/resource.rb +53 -0
  143. data/test/resources/path.rb +166 -0
  144. data/test/runtime/configurations.rb +19 -0
  145. data/test/runtime/request.rb +63 -0
  146. data/test/runtime/response.rb +55 -0
  147. data/test/views/views.rb +34 -0
  148. metadata +394 -0
@@ -0,0 +1,30 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class Base
6
+
7
+ attr_accessor :constraints
8
+
9
+ def []( *args ) ; call( *args ) ; end
10
+
11
+ # used to provide consisting matching logic across all matchers
12
+ def test( request )
13
+ constraints.all? do | key, val |
14
+ if val.nil? or val == true
15
+ true
16
+ else
17
+ if val.respond_to? :call
18
+ val.call( request )
19
+ else
20
+ val == request.send( key ) or val === request.send( key ) or request.send( key ) === val
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,17 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class ContentType < Base
6
+
7
+ def initialize( content_type ) ; @constraints = { :content_type => content_type } ; end
8
+
9
+ # we could maybe do something more sophisticated here, matching against content type patterns
10
+ # such */html or */xml, etc.; adapt from the matching done on accepts ... ?
11
+ def call( request ) ; test( request ) ; end
12
+
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,67 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class Path < Base
6
+
7
+ # Takes an array of pattern elements ... coming soon, support for formatted strings!
8
+ def initialize( pattern ) ; @pattern = pattern ; end
9
+
10
+ # returns a hash of captured values
11
+ def call( request )
12
+ if @pattern.is_a? Array
13
+ path = extract_path( request ).reverse
14
+ return {} if @pattern.empty? && path.empty?
15
+ capture = {}
16
+ match = @pattern.all? do | want |
17
+ case want
18
+ when true # same as a Range of 1..-1
19
+ path = [] unless path.empty?
20
+ when Range
21
+ if want.end == -1
22
+ path = [] if path.length >= want.begin
23
+ else
24
+ path = [] if want.include? path.length
25
+ end
26
+ when String then want == path.pop
27
+ when Symbol then capture[ want ] = path.pop
28
+ when Regexp then want === path.pop
29
+ when Hash
30
+ key, value = want.to_a.first
31
+ case value
32
+ when true
33
+ ( capture[ key ], path = path.reverse, [] ) unless path.empty?
34
+ when Range
35
+ if value.end == -1
36
+ ( capture[ key ], path = path.reverse, [] ) if path.length >= value.begin
37
+ else
38
+ ( capture[ key ], path = path.reverse, [] ) if value.include? path.length
39
+ end
40
+ when String, Symbol
41
+ got = path.pop
42
+ capture[ key ] = got ? got : value.to_s
43
+ when Regexp then
44
+ got = path.pop
45
+ capture[ key ] = got if value === got
46
+ end
47
+ end
48
+ end
49
+ capture if match && path.empty?
50
+ elsif @pattern == true or @pattern == false or @pattern == nil
51
+ {}
52
+ end
53
+ end
54
+
55
+ # private
56
+
57
+ # just a little helper method
58
+ def extract_path( request )
59
+ request.traits.waves.path ||= request.path.scan(/[^\/]+/).map { |e| Rack::Utils.unescape(e) }
60
+ end
61
+
62
+ end
63
+
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,21 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class Query < Base
6
+
7
+ def initialize( pattern ) ; @pattern = ( pattern or {} ) ; end
8
+
9
+ def call( request )
10
+ @pattern.all? do | key, val |
11
+ key = key.to_s
12
+ ( ( val.respond_to?(:call) and val.call( request.query[ key ] ) ) or
13
+ ( request.query[ key ] and ( ( val == true ) or ( val === request.query[ key ] ) ) ) )
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,27 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class Request < Base
6
+
7
+ def initialize( options )
8
+ @uri = Matchers::URI.new( options )
9
+ @constraints = {
10
+ :content_type => Matchers::ContentType.new( options[ :content_type ] ),
11
+ :accept => Matchers::Accept.new( options ),
12
+ :query => Matchers::Query.new( options[:query] ),
13
+ :traits => Matchers::Traits.new( options[:traits] )
14
+ }
15
+ end
16
+
17
+ def call( request )
18
+ if test( request ) and captured = @uri[ request ]
19
+ request.traits.waves.captured = captured
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,19 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class Resource
6
+
7
+ def initialize( options )
8
+ @matcher = Waves::Matchers::Request.new( options )
9
+ end
10
+
11
+ def call( resource ) ; @matcher.call( resource.request ) ; end
12
+ def []( *args ) ; call( *args ) ; end
13
+
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class Traits < Base
6
+
7
+ def initialize( pattern ) ; @pattern = ( pattern or {} ) ; end
8
+
9
+ def call( request )
10
+ @pattern.all? do | key, val |
11
+ ( val.is_a? Proc and val.call( request.traits[ key ] ) ) or val === request.traits[ key ]
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,20 @@
1
+ module Waves
2
+
3
+ module Matchers
4
+
5
+ class URI < Base
6
+
7
+ def initialize( options )
8
+ @path = Waves::Matchers::Path.new( options[ :path ] )
9
+ @constraints = { :server => options[ :server ], :scheme => options[ :scheme ] }
10
+ end
11
+
12
+ def call( request )
13
+ @path.call( request ) if test( request )
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,13 @@
1
+ module Waves
2
+
3
+ module Renderers # :nodoc:
4
+
5
+ module Mixin
6
+
7
+
8
+
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,136 @@
1
+ module Waves
2
+
3
+ module Resources
4
+
5
+ StatusCodes = {
6
+ Waves::Dispatchers::NotFoundError => 404
7
+ }
8
+
9
+
10
+ module Mixin
11
+
12
+ attr_reader :request
13
+
14
+ module ClassMethods
15
+
16
+ def paths
17
+ unless @paths
18
+ resource = self
19
+ @paths = Class.new( superclass.respond_to?( :paths ) ? superclass.paths : Waves::Resources::Paths ) do
20
+ @resource = resource
21
+ def self.resource ; @resource ; end
22
+ end
23
+ else
24
+ @paths
25
+ end
26
+ end
27
+ def with( options ) ; @options = options ; yield ; @options = nil ; end
28
+ def on( method, path = true, options = nil, &block )
29
+ if path.is_a? Hash
30
+ generator = path.keys.first
31
+ path = path.values.first
32
+ end
33
+ if options
34
+ options[ :path ] = path
35
+ else
36
+ options = { :path => path }
37
+ end
38
+ options = @options.merge( options ) if @options
39
+ matcher = Waves::Matchers::Resource.new( options )
40
+ methods = case method
41
+ when nil then nil
42
+ when true then [ :post, :get, :put, :delete ]
43
+ when Array then method
44
+ else [ method ]
45
+ end
46
+ methods.each do | method |
47
+ functor_with_self( method, matcher, &block )
48
+ end
49
+ paths.module_eval {
50
+ define_method( generator ) { | *args | generate( path, args ) }
51
+ } if generator
52
+ end
53
+ def before( path = nil, options = {}, &block )
54
+ on( :before, path, options, &block )
55
+ end
56
+ def after( path = nil, options = {}, &block )
57
+ on( :after, path, options, &block )
58
+ end
59
+ def wrap( path = nil, options = {}, &block )
60
+ before( path, options, &block )
61
+ after( path, options, &block )
62
+ end
63
+ def handler( exception, &block ) ; functor( :handler, exception, &block ) ; end
64
+ def always( &block ) ; define_method( :always, &block ) ; end
65
+
66
+ end
67
+
68
+ # this is necessary because you can't define functors within a module because the functor attempts
69
+ # to incorporate the superclass functor table into it's own
70
+ def self.included( resource )
71
+
72
+ resource.module_eval do
73
+
74
+ include ResponseMixin, Functor::Method ; extend ClassMethods
75
+
76
+ def initialize( request ); @request = request ; end
77
+
78
+ def process
79
+ begin
80
+ before ; body = send( request.method ) ; after
81
+ rescue Waves::Dispatchers::Redirect => e
82
+ raise e
83
+ rescue Exception => e
84
+ response.status = ( StatusCodes[ e.class ] || 500 )
85
+ ( body = handler( e ) ) rescue raise e
86
+ Waves::Logger.warn "Handled #{e.class}: #{e}"
87
+ e.backtrace.each { |t| Waves::Logger.debug " #{t}" }
88
+ ensure
89
+ always
90
+ end
91
+ return body
92
+ end
93
+
94
+ def to( resource )
95
+ resource = case resource
96
+ when Base
97
+ resource
98
+ when Symbol, String
99
+ begin
100
+ Waves.main::Resources[ resource ]
101
+ rescue NameError => e
102
+ Waves::Logger.debug e.to_s
103
+ e.backtrace.each { |t| Waves::Logger.debug " #{t}" }
104
+ raise Waves::Dispatchers::NotFoundError
105
+ end
106
+ Waves.main::Resources[ resource ]
107
+ end
108
+ r = traits.waves.resource = resource.new( request )
109
+ r.process
110
+ end
111
+
112
+ def redirect( path ) ; request.redirect( path ) ; end
113
+
114
+ # override for resources that may have long-running requests. this helps servers
115
+ # determine how to handle the request
116
+ def deferred? ; false ; end
117
+
118
+ before {} ; after {} ; always {}
119
+ # handler( Waves::Dispatchers::Redirect ) { |e| raise e }
120
+
121
+ %w( post get put delete head ).each do | method |
122
+ on( method ) { not_found }
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+
131
+ class Base ; include Mixin ; end
132
+
133
+ end
134
+
135
+
136
+ end
@@ -0,0 +1,132 @@
1
+ module Waves
2
+
3
+ module Resources
4
+
5
+ class Paths
6
+
7
+ def self.compiled; @compiled ||= {} ; end
8
+
9
+ def compiled_paths; self.class.compiled ; end
10
+
11
+ def generate( template, args )
12
+ return "/" if template.empty?
13
+ if template.is_a? Array
14
+ if args.size == 1 and args.first.is_a? Hash
15
+ process_hash( template, args.first )
16
+ else
17
+ process_array( template, args)
18
+ end
19
+ else
20
+ "/#{ args * '/' }"
21
+ end
22
+ end
23
+
24
+ def process_array( template, args )
25
+ template_key = template
26
+ compiled = compiled_paths[template_key]
27
+ if compiled
28
+ return ( compiled % args ) rescue raise [template, args].inspect
29
+ end
30
+ compilable = true
31
+ cpath, interpolations = "", []
32
+ result = ( cpath % interpolations ) if template.all? do | want |
33
+ case want
34
+ when Symbol
35
+ cpath << "/%s" ; interpolations << args.shift
36
+ when String
37
+ cpath << "/#{want}"
38
+ when true
39
+ compilable = false
40
+ cpath += "/#{args.join("/")}"; args = []
41
+ when Hash
42
+ compilable = false
43
+ key, value = want.to_a.first
44
+ case value
45
+ when true
46
+ cpath += "/#{args.join("/")}"; args = []
47
+ when String, Symbol
48
+ compilable = true
49
+ component = args.shift
50
+ cpath << "/%s"
51
+ component ? interpolations << component : interpolations << value
52
+ when Regexp
53
+ component = args.shift.to_s
54
+ raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ value
55
+ cpath << "/%s"; interpolations << component
56
+ end
57
+ when Regexp
58
+ compilable = false
59
+ component = args.shift.to_s
60
+ raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ want
61
+ cpath << "/%s"; interpolations << component
62
+ end
63
+ end
64
+ raise ArgumentError, "Too many args" unless args.empty?
65
+ compiled_paths[template_key] = cpath if compilable
66
+ result
67
+ end
68
+
69
+ def process_hash( template, hash )
70
+ path = []
71
+ ( "/#{ path * '/' }" ) if template.all? do |want|
72
+ case want
73
+ when Symbol
74
+ raise ArgumentError, "Path generator needs a value for #{want.inspect}" unless component = hash[want]
75
+ path << component
76
+ when String then path << want
77
+ when Hash
78
+ key, value = want.to_a.first
79
+ case value
80
+ when Regexp
81
+ raise ArgumentError, "Path generator needs a value for #{want.inspect}" unless component = hash[key]
82
+ raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ value
83
+ path << component
84
+ when String, Symbol
85
+ hash.has_key?(key) ? path << hash[key] : path << value
86
+ when true
87
+ raise ArgumentError, "Path generator needs a value for #{want.inspect}" unless component = hash[key]
88
+ path += [component].flatten
89
+ end
90
+ when Regexp, true
91
+ raise ArgumentError, "Path generator can't take an args hash, as it contains a Regexp or the value true"
92
+ end
93
+ end
94
+ end
95
+
96
+ def original_generate( template, args )
97
+ if template.is_a? Array and not template.empty?
98
+ path = []
99
+ ( "/#{ path * '/' }" ) if template.all? do | want |
100
+ case want
101
+ when true then path += args
102
+ when String then path << want
103
+ when Symbol then path << args.shift
104
+ when Regexp
105
+ component = args.shift.to_s
106
+ raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ want
107
+ path << component
108
+ when Hash
109
+ key, value = want.to_a.first
110
+ case value
111
+ when true then path += args
112
+ when String, Symbol
113
+ # if no args to interpolate, use hash element value as default
114
+ !args.empty? ? path << args.shift : path << value
115
+ when Regexp
116
+ component = args.shift.to_s
117
+ raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ value
118
+ path << component
119
+ end
120
+ end
121
+ end
122
+ else
123
+ "/#{ args * '/' }"
124
+ end
125
+ end
126
+
127
+
128
+
129
+ end
130
+ end
131
+
132
+ end