waves 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/app/Rakefile +4 -0
  2. data/app/configurations/default.rb.erb +8 -0
  3. data/app/configurations/development.rb.erb +22 -0
  4. data/app/configurations/mapping.rb.erb +49 -0
  5. data/app/configurations/production.rb.erb +25 -0
  6. data/app/controllers/default.rb.erb +39 -0
  7. data/app/doc/EMTPY +0 -0
  8. data/app/helpers/default.rb.erb +13 -0
  9. data/app/lib/application.rb.erb +52 -0
  10. data/app/lib/startup.rb.erb +2 -0
  11. data/app/lib/tasks/cluster.rb +26 -0
  12. data/app/lib/tasks/schema.rb +28 -0
  13. data/app/models/default.rb.erb +13 -0
  14. data/app/schema/migration/templates/empty.rb.erb +9 -0
  15. data/app/templates/errors/not_found.mab +2 -0
  16. data/app/templates/errors/server_error.mab +2 -0
  17. data/app/templates/layouts/default.mab +14 -0
  18. data/app/views/default.rb.erb +13 -0
  19. data/bin/waves +32 -0
  20. data/bin/waves-console +8 -0
  21. data/bin/waves-server +45 -0
  22. data/lib/controllers/mixin.rb +62 -0
  23. data/lib/dispatchers/base.rb +41 -0
  24. data/lib/dispatchers/default.rb +33 -0
  25. data/lib/helpers/common.rb +22 -0
  26. data/lib/helpers/form.rb +22 -0
  27. data/lib/helpers/formatting.rb +22 -0
  28. data/lib/helpers/model.rb +15 -0
  29. data/lib/helpers/view.rb +14 -0
  30. data/lib/renderers/erubis.rb +38 -0
  31. data/lib/renderers/markaby.rb +31 -0
  32. data/lib/renderers/mixin.rb +41 -0
  33. data/lib/runtime/application.rb +43 -0
  34. data/lib/runtime/configuration.rb +47 -0
  35. data/lib/runtime/console.rb +21 -0
  36. data/lib/runtime/logger.rb +28 -0
  37. data/lib/runtime/mapping.rb +82 -0
  38. data/lib/runtime/mime_types.rb +18 -0
  39. data/lib/runtime/request.rb +43 -0
  40. data/lib/runtime/response.rb +26 -0
  41. data/lib/runtime/response_mixin.rb +53 -0
  42. data/lib/runtime/response_proxy.rb +29 -0
  43. data/lib/runtime/server.rb +58 -0
  44. data/lib/runtime/session.rb +43 -0
  45. data/lib/utilities/integer.rb +8 -0
  46. data/lib/utilities/kernel.rb +8 -0
  47. data/lib/utilities/module.rb +7 -0
  48. data/lib/utilities/object.rb +13 -0
  49. data/lib/utilities/string.rb +36 -0
  50. data/lib/utilities/symbol.rb +5 -0
  51. data/lib/views/mixin.rb +54 -0
  52. data/lib/waves.rb +50 -0
  53. metadata +189 -0
data/app/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'waves'
2
+ Waves::Console.load
3
+ %w( schema ).each { |task| require "lib/tasks/#{task}.rb" }
4
+
@@ -0,0 +1,8 @@
1
+ module <%= name %>
2
+ module Configurations
3
+ class Default < Waves::Configurations::Default
4
+ database :host => host, :adapter => 'mysql', :database => 'blog',
5
+ :user => 'root', :password => ''
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ module <%= name %>
2
+ module Configurations
3
+ class Development < Default
4
+
5
+ host '127.0.0.1'
6
+
7
+ port 3000
8
+
9
+ reloadable [ <%= name %> ]
10
+
11
+ log :level => :debug
12
+
13
+ application do
14
+ use Rack::ShowExceptions
15
+ run Waves::Dispatchers::Default.new
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+
22
+
@@ -0,0 +1,49 @@
1
+ module <%= name %>
2
+
3
+ module Configurations
4
+
5
+ module Mapping
6
+
7
+ extend Waves::Mapping
8
+
9
+ name = '([\w\-\_\.\+\@]+)'; model = '([\w\-]+)'
10
+
11
+ path %r{^/$} { redirect('/entries') }
12
+
13
+ # get all resources for the given model
14
+ path %r{^/#{model}/?$} do | model |
15
+ use( model ) | controller { all } | view { |data| list( model.plural => data ) }
16
+ end
17
+
18
+ # get all resources identifiers for the given model in json format
19
+ path %r{^/#{model}/?$}, :accept => /json/ do | model |
20
+ use( model ) | controller { all } | view { |data| list( model.plural => data ) }
21
+ end
22
+
23
+ # get the given resource for the given model
24
+ path %r{^/#{model}/#{name}/?$} do | model, name |
25
+ use(model) | controller { find( name ) } |
26
+ view { |data| show( model => data ) }
27
+ end
28
+
29
+ # display an editor for the given resource / model
30
+ path %r{^/#{model}/#{name}/editor/?$} do | model, name |
31
+ use(model) | controller { find( name ) } |
32
+ view { |data| editor( model => data ) }
33
+ end
34
+
35
+ # add / update the given resource for the given model
36
+ path %r{^/#{model}/#{name}/?$}, :method => :post do | model, name |
37
+ use(model) | controller { update( name ) }; redirect( url )
38
+ end
39
+
40
+ # delete the given resource for the given model
41
+ path %r{^/#{model}/#{name}/?$}, :method => :delete do | model, name |
42
+ use(model) | controller { delete( name ) }; redirect( "/#{model}/" )
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,25 @@
1
+ module <%= name %>
2
+
3
+ module Configurations
4
+
5
+ class Production < Default
6
+
7
+ host '0.0.0.0'
8
+
9
+ port 80
10
+
11
+ reloadable []
12
+
13
+ log :level => :error,
14
+ :output => ( :log / "waves.#{$$}" ),
15
+ :rotation => :weekly
16
+
17
+ application do
18
+ run Waves::Dispatchers::Default.new
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,39 @@
1
+ module <%= name %>
2
+
3
+ module Controllers
4
+
5
+ class Default
6
+
7
+ include Waves::Controllers::Mixin
8
+
9
+ def all
10
+ model.all
11
+ end
12
+
13
+ def find( name )
14
+ model[ name ]
15
+ end
16
+
17
+ def create( attrs = {} )
18
+ model.new( attrs )
19
+ end
20
+
21
+ def update
22
+ attrs = params[model_name.singular]
23
+ if instance = find( attrs[:name] )
24
+ instance.set( attrs )
25
+ instance.save
26
+ else
27
+ model.create( attrs )
28
+ end
29
+ end
30
+
31
+ def delete
32
+ model[ name ].destroy
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
data/app/doc/EMTPY ADDED
File without changes
@@ -0,0 +1,13 @@
1
+ module <%= name %>
2
+ module Helpers
3
+ module Default
4
+ attr_reader :request, :content
5
+ include Waves::ResponseMixin
6
+ include Waves::Helpers::Common
7
+ include Waves::Helpers::Formatting
8
+ include Waves::Helpers::Model
9
+ include Waves::Helpers::View
10
+ include Waves::Helpers::Form
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ require 'sequel'
2
+ module <%= name %>
3
+
4
+ extend Autocreate; extend Autoload; extend Reloadable
5
+ autoload true; directories :lib
6
+
7
+ [ :Configurations, :Models, :Views, :Controllers, :Helpers ].each do | name |
8
+ autocreate( name, Module.new ) do
9
+
10
+ # dynamically access module constants
11
+ def self.[]( cname )
12
+ eval("#{name}::#{cname.to_s.camel_case}")
13
+ end
14
+
15
+ # first try to load and only create if that fails
16
+ # which means install autoload *after* autocreate
17
+ extend Autocreate; extend Autoload
18
+
19
+ # autoload any files in appropriately named directories
20
+ # exampe: models/blog.rb for Blog
21
+ autoload true; directories name.to_s.snake_case
22
+
23
+ # autocreate declarations ...
24
+ case name
25
+ # don't autocreate configs
26
+ when :Configurations then nil
27
+ # set the dataset for Models
28
+ when :Models
29
+ autocreate true, eval("<%= name %>::Models::Default") do
30
+ set_dataset <%= name %>.database[ basename.snake_case.plural.intern ]
31
+ end
32
+ # everything else just use the exemplar
33
+ else
34
+ autocreate true, eval("<%= name %>::#{name}::Default")
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ # accessor methods for modules and other key application objects ...
42
+ class << self
43
+ def config ; Waves::Server.config rescue nil || Waves::Console.config ; end
44
+ def database ; @database ||= Sequel.mysql( config.database ) ; end
45
+ def configurations ; <%= name %>::Configurations ; end
46
+ def controllers ; <%= name %>::Controllers ; end
47
+ def models ; <%= name %>::Models ; end
48
+ def helpers ; <%= name %>::Helpers ; end
49
+ def views ; <%= name %>::Views ; end
50
+ end
51
+
52
+ end
@@ -0,0 +1,2 @@
1
+ require 'lib/<%= name.downcase %>'
2
+ Waves << <%= name %>
@@ -0,0 +1,26 @@
1
+ namespace :cluster do
2
+
3
+ desc 'Start a cluster of waves applications.'
4
+ task :start => :stop do |task|
5
+ pids = []; path = :log / :pids
6
+ Waves::Console.config.ports.each do |port|
7
+ pid = fork { exec "waves_server -p #{port} #{ENV['mode']} -d" }
8
+ Process.detach( pid ); pids << pid
9
+ end
10
+ File.write( path, pids.join(',') )
11
+ end
12
+
13
+ desc 'Stop a cluster of waves applications.'
14
+ task :stop do |task|
15
+ path = :log / :pids
16
+ if File.exist? path
17
+ File.read( path ).split(',').each { |pid| Process.kill( 'INT', pid.to_i ) rescue nil }
18
+ FileUtils.rm( path )
19
+ end
20
+ end
21
+
22
+ desc 'Restart a cluster of waves applications.'
23
+ task :restart => [ :stop, :start ] do |task|
24
+ end
25
+
26
+ end
@@ -0,0 +1,28 @@
1
+ require 'sequel'
2
+ namespace :schema do
3
+
4
+ desc 'Create a new Sequel Migration using name.'
5
+ task :migration do |task|
6
+
7
+ version = ( ENV['version'].to_i ||
8
+ Sequel::Migrator.get_current_migration_version( Blog.database ) ) + 1
9
+
10
+ name = ENV['name'] || 'migration'
11
+ class_name = name.camel_case
12
+
13
+ template = ( ENV['template'] || 'empty' ) + '.rb.erb'
14
+ source = :schema / :migration / :templates / template
15
+ destination = :schema / :migration / "#{'%03d' % version}_#{name}.rb"
16
+ code = Erubis::Eruby.new( File.read( source ) ).result( binding )
17
+ File.write( destination, code )
18
+
19
+ end
20
+
21
+ desc 'Performs migration from version, to version.'
22
+ task :migrate do |task|
23
+ version = ENV['version']; version = version.to_i unless version.nil?
24
+ Sequel::Migrator.apply( Waves.application.database, :schema / :migration , version )
25
+ end
26
+
27
+ end
28
+
@@ -0,0 +1,13 @@
1
+ module <%= name %>
2
+
3
+ module Models
4
+
5
+ class Default < Sequel::Model
6
+ before_save do
7
+ set(:updated_on => Time.now) if columns.include? :updated_on
8
+ end
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ class <%= class_name %> < Sequel::Migration
2
+
3
+ def up
4
+ end
5
+
6
+ def down
7
+ end
8
+
9
+ end
@@ -0,0 +1,2 @@
1
+ h1 '404'
2
+ p %q( That URL does not exist on this server. )
@@ -0,0 +1,2 @@
1
+ h1 '500'
2
+ p %q( Internal server error. Sorry, but your request could not be processed. )
@@ -0,0 +1,14 @@
1
+ doctype :html4_strict
2
+
3
+ html
4
+
5
+ head do
6
+ title @title
7
+ end
8
+
9
+ body do
10
+ layout_content
11
+ end
12
+
13
+ end
14
+
@@ -0,0 +1,13 @@
1
+ module <%= name %>
2
+
3
+ module Views
4
+
5
+ class Default
6
+
7
+ include Waves::Views::Mixin
8
+
9
+ end
10
+
11
+ end
12
+
13
+ end
data/bin/waves ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ unless ARGV.length == 1
4
+ $stderr.puts "Usage: waves {name}"
5
+ exit 1
6
+ end
7
+
8
+ require 'rubygems'
9
+ require 'waves'
10
+
11
+ puts "** Creating new Waves application ..."
12
+ app_name = ARGV[0]
13
+ template = File.join( File.dirname(__FILE__),'..','app')
14
+ system "cp -R #{template} #{app_name}"
15
+
16
+ # get rid of placeholder files left over from gem install
17
+ Dir["#{app_name}/**/EMPTY"].each { |path| system "rm #{path}" }
18
+
19
+ # next, process all template files ...
20
+ Dir["#{app_name}/**/*.erb"].each do |path|
21
+ unless path =~ %r{^#{app_name}/(schema/migration/templates|templates)}
22
+ name = app_name.camel_case
23
+ File.write( path.gsub(/\.erb$/,''),
24
+ Erubis::Eruby.new( File.read( path ) ).result( binding ) )
25
+ system "rm #{path}"
26
+ end
27
+ end
28
+
29
+ # finally, change the lib/application.rb file
30
+ system "mv #{app_name}/lib/application.rb #{app_name}/lib/#{app_name}.rb"
31
+
32
+ puts "** Application created!"
data/bin/waves-console ADDED
@@ -0,0 +1,8 @@
1
+ require 'waves'
2
+
3
+ if ARGV.length > 1
4
+ puts STDERR, "Usage: console [mode]"; exit 1
5
+ end
6
+
7
+ Waves::Console.load( ARGV[0] ? ARGV[0].intern : :development )
8
+ require 'irb'; IRB.start
data/bin/waves-server ADDED
@@ -0,0 +1,45 @@
1
+ require 'waves'
2
+ require 'choice'
3
+
4
+ Choice.options do
5
+ header 'Run a waves application server.'
6
+ header ''
7
+ option :port do
8
+ short '-p'
9
+ long '--port=PORT'
10
+ desc 'Port to listen on.'
11
+ desc 'Defaults to value given in configuration.'
12
+ cast Integer
13
+ end
14
+ separator ''
15
+ option :host do
16
+ short '-h'
17
+ long '--host=HOST'
18
+ desc 'Host or IP address of the host to bind.'
19
+ desc 'Defaults to value given in configuration.'
20
+ end
21
+ separator ''
22
+ option :mode do
23
+ short '-c'
24
+ long '--config=CONFIG'
25
+ desc 'Configuration to use.'
26
+ desc 'Defaults to development.'
27
+ cast Symbol
28
+ end
29
+ separator ''
30
+ option :directory do
31
+ short '-D'
32
+ long '--dir=DIR'
33
+ desc 'Directory containing the application.'
34
+ desc 'Defaults to the current directory.'
35
+ end
36
+ separator ''
37
+ option :daemon do
38
+ short '-d'
39
+ long '--daemon'
40
+ desc 'Run as a daemon.'
41
+ end
42
+ separator ''
43
+ end
44
+
45
+ Waves::Server.run( Choice.choices )