dyoder-waves 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/app/Rakefile +14 -0
  2. data/app/bin/waves-console +3 -0
  3. data/app/bin/waves-server +3 -0
  4. data/app/configurations/development.rb.erb +31 -0
  5. data/app/configurations/mapping.rb.erb +14 -0
  6. data/app/configurations/production.rb.erb +30 -0
  7. data/app/lib/application.rb.erb +3 -0
  8. data/app/startup.rb +5 -0
  9. data/app/templates/errors/not_found_404.mab +2 -0
  10. data/app/templates/errors/server_error_500.mab +2 -0
  11. data/app/templates/layouts/default.mab +14 -0
  12. data/bin/waves +66 -0
  13. data/bin/waves-console +4 -0
  14. data/bin/waves-server +4 -0
  15. data/lib/commands/waves-console.rb +24 -0
  16. data/lib/commands/waves-server.rb +55 -0
  17. data/lib/controllers/mixin.rb +158 -0
  18. data/lib/dispatchers/base.rb +52 -0
  19. data/lib/dispatchers/default.rb +67 -0
  20. data/lib/foundations/default.rb +28 -0
  21. data/lib/foundations/simple.rb +17 -0
  22. data/lib/helpers/common.rb +62 -0
  23. data/lib/helpers/form.rb +39 -0
  24. data/lib/helpers/formatting.rb +30 -0
  25. data/lib/helpers/model.rb +33 -0
  26. data/lib/helpers/view.rb +24 -0
  27. data/lib/layers/default_errors.rb +24 -0
  28. data/lib/layers/simple_errors.rb +17 -0
  29. data/lib/mapping/mapping.rb +252 -0
  30. data/lib/mapping/pretty_urls.rb +94 -0
  31. data/lib/renderers/erubis.rb +61 -0
  32. data/lib/renderers/markaby.rb +33 -0
  33. data/lib/renderers/mixin.rb +53 -0
  34. data/lib/runtime/application.rb +65 -0
  35. data/lib/runtime/configuration.rb +180 -0
  36. data/lib/runtime/console.rb +20 -0
  37. data/lib/runtime/debugger.rb +9 -0
  38. data/lib/runtime/logger.rb +52 -0
  39. data/lib/runtime/mime_types.rb +22 -0
  40. data/lib/runtime/request.rb +77 -0
  41. data/lib/runtime/response.rb +40 -0
  42. data/lib/runtime/response_mixin.rb +35 -0
  43. data/lib/runtime/response_proxy.rb +27 -0
  44. data/lib/runtime/server.rb +94 -0
  45. data/lib/runtime/session.rb +56 -0
  46. data/lib/tasks/cluster.rb +25 -0
  47. data/lib/tasks/gem.rb +31 -0
  48. data/lib/tasks/generate.rb +15 -0
  49. data/lib/utilities/inflect.rb +194 -0
  50. data/lib/utilities/integer.rb +17 -0
  51. data/lib/utilities/kernel.rb +34 -0
  52. data/lib/utilities/module.rb +17 -0
  53. data/lib/utilities/object.rb +17 -0
  54. data/lib/utilities/proc.rb +9 -0
  55. data/lib/utilities/string.rb +47 -0
  56. data/lib/utilities/symbol.rb +7 -0
  57. data/lib/verify/mapping.rb +29 -0
  58. data/lib/verify/request.rb +40 -0
  59. data/lib/views/mixin.rb +108 -0
  60. data/lib/waves.rb +80 -0
  61. metadata +260 -0
@@ -0,0 +1,25 @@
1
+ namespace :cluster do
2
+
3
+ desc 'Start a cluster of waves applications.'
4
+ task :start do |task|
5
+ script = USE_WAVES_SRC ? 'bin/waves-server' : 'waves-server'
6
+ ( Waves::Console.config.ports || [ Waves::Console.config.port ] ).each do |port|
7
+ cmd = "#{script} -p #{port} -c #{ENV['mode']||'development'} -d"
8
+ puts cmd ; `#{cmd}`
9
+ end
10
+ end
11
+
12
+ desc 'Stop a cluster of waves applications.'
13
+ task :stop do |task|
14
+ Dir[ :log / '*.pid' ].each do |pidfile|
15
+ pid = File.read(pidfile).to_i
16
+ puts "Stopping process #{pid} ..."
17
+ Process.kill( 'INT', pid ) rescue nil
18
+ end
19
+ end
20
+
21
+ desc 'Restart a cluster of waves applications.'
22
+ task :restart => [ :stop, :start ] do |task|
23
+ end
24
+
25
+ end
data/lib/tasks/gem.rb ADDED
@@ -0,0 +1,31 @@
1
+ namespace :gem do
2
+ desc "freeze a gem using gem=<gem name> [version=<gem version>]"
3
+ task :freeze do
4
+ raise "No gem specified" unless gem_name = ENV['gem']
5
+
6
+ require 'rubygems'
7
+ Gem.manage_gems
8
+
9
+ gem = (version = ENV['version']) ?
10
+ Gem.cache.search(gem_name, "= #{version}").first :
11
+ Gem.cache.search(gem_name).sort_by { |g| g.version }.last
12
+
13
+ version ||= gem.version.version rescue nil
14
+
15
+ target_dir = File.join(Waves::Configurations::Default.root, 'gems')
16
+ mkdir_p target_dir
17
+ sh "gem install #{gem_name} --version #{version} -i #{target_dir} --no-rdoc --no-ri"
18
+
19
+ puts "Unpacked #{gem_name} #{version} to '#{target_dir}'"
20
+ end
21
+
22
+ desc "unfreeze a gem using gem=<gem>"
23
+ task :unfreeze do
24
+ raise "No gem specified" unless gem_name = ENV['gem']
25
+
26
+ target_dir = File.join(Waves::Configurations::Default.root, 'gems')
27
+ ENV['GEM_HOME'] = target_dir # no install_dir option for gem uninstall
28
+
29
+ sh "gem uninstall #{gem_name}"
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ namespace :generate do
2
+ desc 'Generate a new model'
3
+ task :model do |task|
4
+ template = File.read :models / 'default.rb'
5
+ File.write( :models / ENV['name'] + '.rb',
6
+ template.gsub('class Default < Sequel::Model',
7
+ "class #{ENV['name'].camel_case} < Sequel::Model(:#{ENV['name'].plural})") )
8
+ end
9
+ desc 'Generate a new controller'
10
+ task :controller do |task|
11
+ template = File.read :controllers / 'default.rb'
12
+ File.write( :controllers / ENV['name'] + '.rb',
13
+ template.gsub('class Default',"class #{ENV['name'].camel_case}") )
14
+ end
15
+ end
@@ -0,0 +1,194 @@
1
+ # Much love to Facets (more specifically English) for this module
2
+ # http://english.rubyforge.org/
3
+ # changed slightly in the hopes of one day implementing a different set
4
+ # of rules for different languages
5
+ # NOTE: this is NOT implemented yet.
6
+ # plural and singular work directly with the English class
7
+
8
+ module Inflect
9
+ module InflectorMethods
10
+
11
+ # Define a general exception.
12
+ def word(singular, plural=nil)
13
+ plural = singular unless plural
14
+ singular_word(singular, plural)
15
+ plural_word(singular, plural)
16
+ end
17
+
18
+ # Define a singularization exception.
19
+ def singular_word(singular, plural)
20
+ @singular_of[plural] = singular
21
+ end
22
+
23
+ # Define a pluralization exception.
24
+ def plural_word(singular, plural)
25
+ @plural_of[singular] = plural
26
+ end
27
+
28
+ # Define a general rule.
29
+ def rule(singular, plural)
30
+ singular_rule(singular, plural)
31
+ plural_rule(singular, plural)
32
+ end
33
+
34
+ # Define a singularization rule.
35
+ def singular_rule(singular, plural)
36
+ @singular_rules << [singular, plural]
37
+ end
38
+
39
+ # Define a plurualization rule.
40
+ def plural_rule(singular, plural)
41
+ @plural_rules << [singular, plural]
42
+ end
43
+
44
+ # Read prepared singularization rules.
45
+ def singularization_rules
46
+ return @singularization_rules if @singularization_rules
47
+ sorted = @singular_rules.sort_by{ |s, p| "#{p}".size }.reverse
48
+ @singularization_rules = sorted.collect do |s, p|
49
+ [ /#{p}$/, "#{s}" ]
50
+ end
51
+ end
52
+
53
+ # Read prepared pluralization rules.
54
+ def pluralization_rules
55
+ return @pluralization_rules if @pluralization_rules
56
+ sorted = @plural_rules.sort_by{ |s, p| "#{s}".size }.reverse
57
+ @pluralization_rules = sorted.collect do |s, p|
58
+ [ /#{s}$/, "#{p}" ]
59
+ end
60
+ end
61
+
62
+ #
63
+ def plural_of
64
+ @plural_of
65
+ end
66
+
67
+ #
68
+ def singular_of
69
+ @singular_of
70
+ end
71
+
72
+ # Convert an English word from plurel to singular.
73
+ #
74
+ # "boys".singular #=> boy
75
+ # "tomatoes".singular #=> tomato
76
+ #
77
+ def singular(word)
78
+ if result = singular_of[word]
79
+ return result.dup
80
+ end
81
+ result = word.dup
82
+ singularization_rules.each do |(match, replacement)|
83
+ break if result.gsub!(match, replacement)
84
+ end
85
+ return result
86
+ end
87
+
88
+ # Convert an English word from singular to plurel.
89
+ #
90
+ # "boy".plural #=> boys
91
+ # "tomato".plural #=> tomatoes
92
+ #
93
+ def plural(word)
94
+ if result = plural_of[word]
95
+ return result.dup
96
+ end
97
+ #return self.dup if /s$/ =~ self # ???
98
+ result = word.dup
99
+ pluralization_rules.each do |(match, replacement)|
100
+ break if result.gsub!(match, replacement)
101
+ end
102
+ return result
103
+ end
104
+ end
105
+
106
+ class English
107
+ @singular_of = {}
108
+ @plural_of = {}
109
+
110
+ @singular_rules = []
111
+ @plural_rules = []
112
+
113
+ class << self
114
+ include InflectorMethods
115
+ end
116
+
117
+ # One argument means singular and plural are the same.
118
+ word 'equipment'
119
+ word 'information'
120
+ word 'money'
121
+ word 'species'
122
+ word 'series'
123
+ word 'fish'
124
+ word 'sheep'
125
+ word 'moose'
126
+ word 'hovercraft'
127
+
128
+ # Two arguments defines a singular and plural exception.
129
+ word 'Swiss' , 'Swiss'
130
+ word 'life' , 'lives'
131
+ word 'wife' , 'wives'
132
+ word 'virus' , 'viri'
133
+ word 'octopus' , 'octopi'
134
+ word 'cactus' , 'cacti'
135
+ word 'goose' , 'geese'
136
+ word 'criterion' , 'criteria'
137
+ word 'alias' , 'aliases'
138
+ word 'status' , 'statuses'
139
+ word 'axis' , 'axes'
140
+ word 'crisis' , 'crises'
141
+ word 'testis' , 'testes'
142
+ word 'child' , 'children'
143
+ word 'person' , 'people'
144
+ word 'potato' , 'potatoes'
145
+ word 'tomato' , 'tomatoes'
146
+ word 'buffalo' , 'buffaloes'
147
+ word 'torpedo' , 'torpedoes'
148
+ word 'quiz' , 'quizes'
149
+ word 'matrix' , 'matrices'
150
+ word 'vertex' , 'vetices'
151
+ word 'index' , 'indices'
152
+ word 'ox' , 'oxen'
153
+ word 'mouse' , 'mice'
154
+ word 'louse' , 'lice'
155
+ word 'thesis' , 'theses'
156
+ word 'thief' , 'thieves'
157
+ word 'analysis' , 'analyses'
158
+
159
+ # One-way singularization exception (convert plural to singular).
160
+ singular_word 'cactus', 'cacti'
161
+
162
+ # General rules.
163
+ rule 'hive' , 'hives'
164
+ rule 'rf' , 'rves'
165
+ rule 'af' , 'aves'
166
+ rule 'ero' , 'eroes'
167
+ rule 'man' , 'men'
168
+ rule 'ch' , 'ches'
169
+ rule 'sh' , 'shes'
170
+ rule 'ss' , 'sses'
171
+ rule 'ta' , 'tum'
172
+ rule 'ia' , 'ium'
173
+ rule 'ra' , 'rum'
174
+ rule 'ay' , 'ays'
175
+ rule 'ey' , 'eys'
176
+ rule 'oy' , 'oys'
177
+ rule 'uy' , 'uys'
178
+ rule 'y' , 'ies'
179
+ rule 'x' , 'xes'
180
+ rule 'lf' , 'lves'
181
+ rule 'us' , 'uses'
182
+ rule '' , 's'
183
+
184
+ # One-way singular rules.
185
+ singular_rule 'of' , 'ofs' # proof
186
+ singular_rule 'o' , 'oes' # hero, heroes
187
+ singular_rule 'f' , 'ves'
188
+
189
+ # One-way plural rules.
190
+ plural_rule 'fe' , 'ves' # safe, wife
191
+ plural_rule 's' , 'ses'
192
+ end
193
+ end
194
+
@@ -0,0 +1,17 @@
1
+ # Added methods to Integer for commonly used "numeric phrases" like 6.days or 30.megabytes.
2
+ class Integer
3
+ def seconds ; self ; end
4
+ def minutes ; self * 60 ; end
5
+ def hours ; self * 60.minutes ; end
6
+ def days ; self * 24.hours ; end
7
+ def weeks ; self * 7.days ; end
8
+ def bytes ; self ; end
9
+ def kilobytes ; self * 1024 ; end
10
+ def megabytes ; self * 1024.kilobytes ; end
11
+ def gigabytes ; self * 1024.megabytes ; end
12
+ def terabytes ; self * 1024.gigabytes ; end
13
+ def petabytes ; self * 1024.terabytes ; end
14
+ def exabytes ; self * 1024.petabytes ; end
15
+ def zettabytes ; self * 1024.exabytes ; end
16
+ def yottabytes ; self * 1024.zettabytes ; end
17
+ end
@@ -0,0 +1,34 @@
1
+ module Kernel
2
+ #
3
+ # From Rails. This comes in handy when you want to return a value that you need to modify.
4
+ # So instead of the awkward:
5
+ #
6
+ # foo = Foo.new
7
+ # foo.bar = 'bar'
8
+ # foo
9
+ #
10
+ # You can just say
11
+ #
12
+ # returning Foo.new { |foo| foo.bar = 'bar' }
13
+ #
14
+ def returning( object, &block )
15
+ yield object; object
16
+ end
17
+
18
+ #
19
+ # Inspired by a question on comp.lang.ruby. Sort of like returning, except
20
+ # you don't need to pass in the returnee as an argument. The drawback is that,
21
+ # since this relies on instance_eval, you can't access in methods in the local
22
+ # scope. You can work around this by assigning values to variables, but in that
23
+ # case, you might be better off using returning.
24
+ #
25
+ # Our above example would look like this:
26
+ #
27
+ # with( Foo.new ) { bar = 'bar' }
28
+ #
29
+ def with( object, &block )
30
+ object.instance_eval(&block); object
31
+ end
32
+
33
+
34
+ end
@@ -0,0 +1,17 @@
1
+ class Module
2
+
3
+ # This comes in handy when you are trying to do meta-programming with modules / classes
4
+ # that may be nested within other modules / classes. I think I've seen it defined in
5
+ # facets, but I'm not relying on facets just for this one method.
6
+ def basename
7
+ self.name.split('::').last || ''
8
+ end
9
+
10
+ # Just a convenience method for accessing a const within a Module
11
+ def []( cname )
12
+ const_get( cname.to_s.camel_case )
13
+ end
14
+
15
+
16
+
17
+ end
@@ -0,0 +1,17 @@
1
+ class Object
2
+ # This is an extremely powerful little function that will be built-in to Ruby 1.9.
3
+ # This version is from Mauricio Fernandez via ruby-talk. Works like instance_eval
4
+ # except that you can pass parameters to the block. This means you can define a block
5
+ # intended for use with instance_eval, pass it to another method, which can then
6
+ # invoke with parameters. This is used quite a bit by the Waves::Mapping code.
7
+ def instance_exec(*args, &block)
8
+ mname = "__instance_exec_#{Thread.current.object_id.abs}"
9
+ class << self; self end.class_eval{ define_method(mname, &block) }
10
+ begin
11
+ ret = send(mname, *args)
12
+ ensure
13
+ class << self; self end.class_eval{ undef_method(mname) } rescue nil
14
+ end
15
+ ret
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ class Proc
2
+
3
+ def |(lambda)
4
+ lambda do
5
+ lambda.call( self.call )
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,47 @@
1
+ # Waves extends String with a variety of methods for changing from singular to plural and back, and switching to different types of case and word separators. These methods are similar to those found in Rails and other frameworks, but some (all?) of the names are different. The names here were chosen for increased clarity and are hopefully easy to adjust to ...
2
+
3
+ class String
4
+
5
+ # Does a File.join on the two arguments joined by the /. Very handy
6
+ # for doing platform-safe paths without having to use File.join.
7
+ #
8
+ # I unfortunately don't recall where i first saw this ... see
9
+ # Symbol extension as well, allowing for :files / 'afilename.txt'
10
+
11
+ def / ( string )
12
+ File.join(self,string.to_s)
13
+ end
14
+
15
+ def singular
16
+ Inflect::English.singular(self)
17
+ end
18
+
19
+ alias_method(:singularize, :singular)
20
+
21
+ def plural
22
+ Inflect::English.plural(self)
23
+ end
24
+
25
+ alias_method(:pluralize, :plural)
26
+
27
+ def lower_camel_case
28
+ gsub(/(_)(\w)/) { $2.upcase }
29
+ end
30
+
31
+ def camel_case
32
+ lower_camel_case.gsub(/^([a-z])/) { $1.upcase }
33
+ end
34
+
35
+ def snake_case
36
+ gsub(/([a-z\d])([A-Z])/){ "#{$1}_#{$2}"}.tr("-", "_").downcase
37
+ end
38
+
39
+ def title_case
40
+ gsub(/(^|\s)\s*([a-z])/) { $1 + $2.upcase }
41
+ end
42
+
43
+ def text
44
+ gsub(/[\_\-\.\:]/,' ')
45
+ end
46
+
47
+ end
@@ -0,0 +1,7 @@
1
+ class Symbol
2
+ # Does File.join on self and string, converting self to string before invocation.
3
+ # See String#/ for more.
4
+ def / ( string )
5
+ self.to_s / string
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ module Waves
2
+ module Verify
3
+ module Helpers
4
+ module Mapping
5
+
6
+ def mapping
7
+ ::Waves::Application.instance.mapping
8
+ end
9
+
10
+ def path(*args,&block)
11
+ mapping.path(*args,&block)
12
+ end
13
+
14
+ def url(*args,&block)
15
+ mapping.url(*args,&block)
16
+ end
17
+
18
+ def handle(*args,&block)
19
+ mapping.handle(*args,&block)
20
+ end
21
+
22
+ def threaded(*args,&block)
23
+ mapping.threaded(*args,&block)
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end