Browse files

paths, dir structure

  • Loading branch information...
1 parent 32e2274 commit 32e01e4811658e91222811ea35c6fd1eaaac60e2 cloudhead committed Apr 15, 2009
Showing with 1 addition and 358 deletions.
  1. +0 −5 document.rb
  2. +0 −64 melon.rb
  3. +1 −1 melon.ru
  4. +0 −27 modules/base/controller.rb
  5. +0 −84 modules/base/model.rb
  6. +0 −17 modules/comments.rb
  7. +0 −9 modules/error.rb
  8. +0 −18 modules/posts.rb
  9. +0 −90 modules/session.rb
  10. +0 −43 router.rb
View
5 document.rb
@@ -1,5 +0,0 @@
-module Melon
- class Document < Hash
-
- end
-end
View
64 melon.rb
@@ -1,64 +0,0 @@
-# Ruby
-require 'rack'
-require 'date'
-require 'digest'
-require 'json'
-
-require 'document'
-require 'router'
-
-# Load modules
-Dir.chdir('modules') do
- puts '* Opening modules/'
- require 'base/model'
- require 'base/controller'
- Dir.new('.').entries.each do |f|
- unless File.directory?( f )
- puts '* Found ' + f
- require f
- end
- end
-end
-
-module Melon
- class Server
- def initialize
- puts "*"
- puts "* Initializing Melon..."
- puts "* Ruby " + RUBY_VERSION
-
- raise "Melon needs ruby 1.9.1 or higher!" if RUBY_VERSION < '1.9.1'
- end
-
- # Called on each request
- def call env
- Router.new( env ).go.finish
- end
- end
-
-end
-
-class Object
- # Syntactic sugar for const_get()
- def self.[]( const )
- self.const_get( const )
- end
-
- # For modules
- def []( const )
- self.const_get( const )
- end
-
- def null
- nil.to_json
- end
-end
-
-class String
- alias each each_line
-end
-
-class Array
- def second; self[1] end
- def third; self[2] end
-end
View
2 melon.ru
@@ -1,4 +1,4 @@
-require 'melon'
+require 'lib/melon'
# Rack config
use Rack::Static, :urls => ['/css', '/js', '/images', '/html'], :root => 'public'
View
27 modules/base/controller.rb
@@ -1,27 +0,0 @@
-module Melon
- module Base
- class Controller
- attr_reader :session
-
- def initialize( key = '', params = {}, session )
- @key = key
- @input = params
- @session = Melon::Session.new( session )
- @response = {}
-
- self
- end
-
- #
- # All the CRUD stuff goes through here
- #
- def do action
- { content: send( action ), session: @session }
- end
-
- def method_missing *args
- { error: args.first + "doesn't exist" }
- end
- end
- end
-end
View
84 modules/base/model.rb
@@ -1,84 +0,0 @@
-module Melon
- module Base
- class Model
- attr_reader :doc
-
- @has = nil
- @belongs = ''
- @key = :id
-
- #
- # Set the key to `value`
- #
- def initialize( doc = {} )
-
- if doc.is_a? Document # From response object
- @doc = doc
- else # New document
- @doc = Document.new
- self.is = self.class.to_s.downcase.split('::').first
-
- # Send the values through the attribute= functions
- doc.each do |k, v|
- send( k.to_s + '=', v )
- end
-
- puts "* Setting default values..."
- # Set defaults on missing values
- self.class.has.each do |k, v|
- send( k.to_s + '=', v ) unless @doc[ k ]
- end
- puts "* New #{ self.class }: " + @doc.inspect
- @doc[:id] = id
- end
-
- self
- end
-
- def id
- @doc.id || send( self.class.key ).downcase.gsub(/[^a-z0-9]+/i, '-')
- end
-
- def is= a
- @doc.is = a
- end
-
- #
- # Accessor methods
- #
- # Define how the model hierarchy will be traversed
- #
- class << self
- def has( has = nil )
- @has = has || @has
- end
-
- def key
- has.keys.first
- end
-
- def get id
- self.new( Sky.get( id ) )
- end
- end
-
- def to_json
- @doc.to_json
- end
-
- #
- # If the method isn't found, look in @doc
- #
- def method_missing *args
- print "* super: Method :#{ args.first } not found, looking in @doc... "
- if args.first.to_s =~ /=$/
- puts "setting to #{args.last}"
- @doc[ args.first.to_s.chop.intern ] = args.last
- else # CASE
- puts @doc[ args.first ]
- @doc[ args.first ]
- end
- end
- end
- end
-end
View
17 modules/comments.rb
@@ -1,17 +0,0 @@
-module Melon
- module Comments
- class Controller < Base::Controller
- def create
- Dir.chdir('public/json/comments') do
- File.open( @input[:id] + '.json' ) do |f|
- f.write( ( JSON.parse( f.read ) << {
- author: @input[:author],
- body: @input[:body],
- date: Time.now.to_i
- }).to_json )
- end
- end
- end
- end
- end
-end
View
9 modules/error.rb
@@ -1,9 +0,0 @@
-module Melon
- module Error
- class Controller < Base::Controller
- def report
- "Routing error!"
- end
- end
- end
-end
View
18 modules/posts.rb
@@ -1,18 +0,0 @@
-module Melon
- module Posts
- class Model < Base::Model
- end
-
- class Controller < Base::Controller
- def frontpage
- Dir.chdir("public/json/posts") do
- Dir.new( Dir.pwd ).entries.collect do |f|
- if File.file?(f) && f =~ /.json$/
- JSON.parse( File.read( f ) )
- end
- end.compact
- end
- end
- end
- end
-end
View
90 modules/session.rb
@@ -1,90 +0,0 @@
-module Melon
- module Session
- def self.new(*args)
- Session::Model.new(*args)
- end
-
- class Model < Hash
- def initialize session = {}
- @env = session #unless session.empty?
-
- self[:auth] = false
- self[:failed] = 0
-
- self <= session if session
-
- self
- end
-
- # Copy a hash into the session
- def <= s
- s.each do |k, v|
- self[ k ] = v
- end
- end
-
- def god?
- self[:god]
- end
-
- def god= bool
- self[:god] = bool
- end
-
- def name
- self[:name]
- end
-
- def login name = nil
- self[:auth] = true
- self[:name] ||= name
- self
- end
-
- # Delete all cookies
- def logout
- self.each do |k,|
- self.delete k
- end
- self
- end
-
- # Write session to env
- def save!
- @env.replace self
- end
-
- end
-
- class Controller < Base::Controller
- def create
- unless @input.empty?
- puts "* " + @input['name'] + " is logging in..."
- thinker = Sky.get @input['name']
-
- if @input['password'] == thinker['password']
- # Login
- @session.login @input['name']
- @session.god = @input['name'] == Settings.cloudder.god ? true : false
- else
- @session[:auth] = false
- @session[:failed] += 1
- end
- end
- { session: @session, redirect: self./ }
- end
-
- def new
- { session: @session }
- end
-
- def destroy
- { session: @session.logout }
- end
-
- def method_missing *args
- super
- end
- end
- end
-end
View
43 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

0 comments on commit 32e01e4

Please sign in to comment.