Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

moved some code around

  • Loading branch information...
commit 6b2c1497350ca7cb27c5840c7c73460c7bdc91c2 1 parent 6d8d15f
cloudhead authored
Showing with 59 additions and 52 deletions.
  1. +0 −5 lib/document.rb
  2. +59 −4 lib/melon.rb
  3. +0 −43 lib/router.rb
View
5 lib/document.rb
@@ -1,5 +0,0 @@
-module Melon
- class Document < Hash
-
- end
-end
View
63 lib/melon.rb
@@ -6,16 +6,13 @@
$:.unshift File.dirname(__FILE__)
-require 'document'
-require 'router'
-
# Load modules
Dir.chdir('lib/modules') do
puts '* Opening modules/'
require 'base/model'
require 'base/controller'
Dir.new('.').entries.each do |f|
- unless File.directory?( f )
+ if File.file?( f ) && f =~ /.rb$/
puts '* Found ' + f
require f
end
@@ -38,6 +35,64 @@ def call env
end
end
+ class Router
+ def initialize env
+ @request = Rack::Request.new env
+ @response = Rack::Response.new
+ @session, path, @input = Session.new( @request.env['rack.session'] ),
+ @request.path_info,
+ @request.params
+
+ # Parse route from url
+ route = path.
+ split('/').
+ reject { |i| i.empty? }. # Clear empty strings
+ drop(1) # Remove /m/
+
+ # Make sure request was an XHR, and the route is valid
+ if route.size >= 2 && @request.xhr?
+ if defined? route.first.capitalize
+ r *route
+ else
+ r :error, :module
+ end
+ else
+ r :error, :route
+ end
+
+ #puts
+ #puts "* Path: #@module/#@action/#@key"
+
+ self
+ end
+
+ def r *args
+ @module, @action, @key = *args
+ end
+
+
+ #
+ # Create controller object & call action
+ #
+ def go
+ controller = Melon[ @module.capitalize ]::Controller.new( @key, @input, @session )
+
+ if controller.respond_to? @action
+ @output = controller.do( @action )
+ (@session <= @output[:session]).save!
+ @response.body = @output[:content].to_json
+ else
+
+ end
+ @response['Content-Length'] = @response.body.size.to_s
+ @response['Content-Type'] = 'application/json'
+
+ @response
+ end
+ end
+
+ class Document < Hash
+ end
end
class Object
View
43 lib/router.rb
@@ -1,43 +0,0 @@
-module Melon
- class Router
- def initialize env
- @request = Rack::Request.new env
- @response = Rack::Response.new
- @session, path, @input = Session.new( @request.env['rack.session'] ),
- @request.path_info,
- @request.params
-
- # Break path into discreet parts & trim slashes
- route = path.
- split('/').
- reject { |i| i.empty? }.
- drop(1) # Remove /m/
-
- if route.size >= 2 && @request.xhr?
- @module, @action, @key = *route
- else
- @module, @action = :error, :report
- end
-
- puts
- puts "* Path: #@module/#@action/#@key"
-
- self
- end
-
- #
- # Create controller object & call action
- #
- def go
- controller = Melon[ @module.capitalize ]::Controller.new( @key, @input, @session )
- @output = controller.do( @action ) || {}
- (@session <= @output[:session]).save!
-
- @response.body = @output[:content].to_json
- @response['Content-Length'] = @response.body.size.to_s
- @response['Content-Type'] = 'application/json'
-
- @response
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.