summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/zorglub.rb1
-rw-r--r--lib/zorglub/app.rb66
-rw-r--r--lib/zorglub/config.rb82
-rw-r--r--lib/zorglub/engines/file.rb2
-rw-r--r--lib/zorglub/engines/haml.rb10
-rw-r--r--lib/zorglub/node.rb46
-rw-r--r--lib/zorglub/session.rb17
-rw-r--r--spec/node_spec.rb19
-rw-r--r--spec/spec_helper.rb24
9 files changed, 128 insertions, 139 deletions
diff --git a/lib/zorglub.rb b/lib/zorglub.rb
index 7ae2395..233fd44 100644
--- a/lib/zorglub.rb
+++ b/lib/zorglub.rb
@@ -1,7 +1,6 @@
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
#
-require 'zorglub/config'
require 'zorglub/node'
require 'zorglub/app'
#
diff --git a/lib/zorglub/app.rb b/lib/zorglub/app.rb
index fa4ff6b..2be2f6b 100644
--- a/lib/zorglub/app.rb
+++ b/lib/zorglub/app.rb
@@ -9,10 +9,33 @@ module Zorglub
def initialize map={}, &block
super
@map = map
+ @engines_cache = { }
+ @options = {
+ :debug => false,
+ :root => '.',
+ :layout => 'default',
+ :view_dir => 'view',
+ :layout_dir => 'layout',
+ :static_dir => 'static',
+ :engine => nil,
+ :engines_cache_enabled => true,
+ :engines => { },
+ :haml_options => {
+ :format => :html5,
+ :ugly => false,
+ :encoding => 'utf-8'
+ },
+ :session_options => {
+ :session_on => false,
+ :session_key => 'zorglub.sid',
+ :session_secret => 'session-secret-secret',
+ :session_sid_len => 64,
+ }
+ }
instance_eval &block if block_given?
remap @map
- @engines_cache = { }
end
+ #
attr_reader :engines_cache
#
def map location, object
@@ -40,6 +63,47 @@ module Zorglub
@map.dup
end
#
+ # OPTIONS @options
+ #
+ def opt sym
+ @options[sym]
+ end
+ #
+ def opt! sym, val
+ @options[sym] = val
+ end
+ #
+ def register_engine! name, ext, proc
+ return unless name
+ if ext.nil? or ext.empty?
+ x = nil
+ else
+ x = (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext)
+ end
+ @options[:engines][name]=[ proc, x ]
+ end
+ #
+ def engine_proc_ext engine, ext
+ p,x = @options[:engines][engine]
+ return [nil, ''] if p.nil?
+ [ p, ((ext.nil? or ext.empty?) ? x : ext ) ]
+ end
+ #
+ def view_base_path
+ p = @options[:view_path]
+ ( p.nil? ? File.join(@options[:root], @options[:view_dir]) : p )
+ end
+ #
+ def layout_base_path
+ p = @options[:layout_path]
+ ( p.nil? ? File.join(@options[:root], @options[:layout_dir]) : p )
+ end
+ #
+ def static_base_path
+ p = @options[:static_path]
+ ( p.nil? ? File.join(@options[:root], @options[:static_dir]) : p )
+ end
+ #
end
#
end
diff --git a/lib/zorglub/config.rb b/lib/zorglub/config.rb
deleted file mode 100644
index b7e5785..0000000
--- a/lib/zorglub/config.rb
+++ /dev/null
@@ -1,82 +0,0 @@
-# -*- coding: UTF-8 -*-
-#
-module Zorglub
- #
- class Config
- @options = {
- :debug => false,
- :root => '.',
- :engine => nil,
- :layout => 'default',
- :view_dir => 'view',
- :layout_dir => 'layout',
- :static_dir => 'static',
- :session_on => false,
- :session_key => 'zorglub.sid',
- :session_secret => 'session-secret-secret',
- :session_sid_len => 64,
- :engines_cache_enabled => true,
- :haml_options => {
- :format => :html5,
- :ugly => false,
- :encoding => 'utf-8'
- }
- #
- }
- @engines = { }
- class << self
- #
- def [] k
- @options[k]
- end
- #
- def []= k, v
- @options[k]=v
- end
- #
- def view_base_path
- p = @options[:view_path]
- ( p.nil? ? File.join(@options[:root], @options[:view_dir]) : p )
- end
- #
- def layout_base_path
- p = @options[:layout_path]
- ( p.nil? ? File.join(@options[:root], @options[:layout_dir]) : p )
- end
- #
- def static_base_path
- p = @options[:static_path]
- ( p.nil? ? File.join(@options[:root], @options[:static_dir]) : p )
- end
- #
- def register_engine name, ext, proc
- return unless name
- if ext.nil? or ext.empty?
- x = nil
- else
- x = (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext)
- end
- @engines[name]=[ proc, x ]
- end
- #
- def engine_proc_ext engine, ext
- p,x = @engines[engine]
- return [nil, ''] if p.nil?
- [ p, ((ext.nil? or ext.empty?) ? x : ext ) ]
- end
- #
- end
- #
- def self.method_missing m, *args, &block
- if m=~/(.*)=$/
- @options[$1.to_sym]=args[0]
- else
- @options[m.to_sym]
- end
- end
- #
- end
- #
-end
-#
-# EOF
diff --git a/lib/zorglub/engines/file.rb b/lib/zorglub/engines/file.rb
index fd9fe1e..75f6f30 100644
--- a/lib/zorglub/engines/file.rb
+++ b/lib/zorglub/engines/file.rb
@@ -15,6 +15,4 @@ module Zorglub
end
end
#
-Zorglub::Config.register_engine :file, nil, Zorglub::Engines::File.method(:proc)
-#
# EOF
diff --git a/lib/zorglub/engines/haml.rb b/lib/zorglub/engines/haml.rb
index 458f29a..9325d19 100644
--- a/lib/zorglub/engines/haml.rb
+++ b/lib/zorglub/engines/haml.rb
@@ -7,11 +7,11 @@ module Zorglub
module Engines
module Haml
def self.proc path,obj
- if Zorglub::Config.engines_cache_enabled
- key = path.sub Zorglub::Config.root,''
- haml = obj.app.engines_cache[key] ||= ::Haml::Engine.new( ::File.open(path,'r'){|f| f.read }, Zorglub::Config.haml_options )
+ if obj.app.opt(:engines_cache_enabled)
+ key = path.sub obj.app.opt(:root),''
+ haml = obj.app.engines_cache[key] ||= ::Haml::Engine.new( ::File.open(path,'r'){|f| f.read }, obj.app.opt(:haml_options) )
else
- haml = ::Haml::Engine.new( ::File.open(path,'r'){|f| f.read }, Zorglub::Config.haml_options )
+ haml = ::Haml::Engine.new( ::File.open(path,'r'){|f| f.read }, obj.app.opt(:haml_options) )
end
html = haml.render(obj)
return html, 'text/html'
@@ -20,6 +20,4 @@ module Zorglub
end
end
#
-Zorglub::Config.register_engine :haml, 'haml', Zorglub::Engines::Haml.method(:proc)
-#
# EOF
diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb
index f889259..5a9aced 100644
--- a/lib/zorglub/node.rb
+++ b/lib/zorglub/node.rb
@@ -4,24 +4,36 @@ module Zorglub
#
class Node
#
+ UNDEFINED=-1
+ #
# class level engine, layout, static, layout_base_path, view_base_path configuration
#
class << self
#
- attr_reader :engine, :layout, :static
+ attr_reader :static
#
def engine! engine
@engine = engine
end
#
+ def engine
+ @engine = @app.opt(:engine) if @engine==UNDEFINED and @app
+ @engine
+ end
+ #
def no_layout!
- @layout = nil
+ layout! nil
end
#
def layout! layout
@layout = layout
end
#
+ def layout
+ @layout = @app.opt(:layout) if @layout==UNDEFINED and @app
+ @layout
+ end
+ #
def static! val
@static = ( (val==true or val==false) ? val : false )
end
@@ -31,7 +43,7 @@ module Zorglub
end
#
def layout_base_path
- @layout_base_path ||= Config.layout_base_path
+ @layout_base_path ||= @app.layout_base_path
end
#
def view_base_path! path
@@ -39,7 +51,7 @@ module Zorglub
end
#
def view_base_path
- @view_base_path ||= Config.view_base_path
+ @view_base_path ||= @app.view_base_path
end
end
#
@@ -81,7 +93,7 @@ module Zorglub
#
def static
return nil if not @options[:static] or @options[:view].nil?
- File.join(Config.static_base_path, @options[:view])+ext
+ File.join(app.static_base_path, @options[:view])+ext
end
#
def ext! ext
@@ -203,8 +215,8 @@ module Zorglub
class << self
#
def inherited sub
- sub.engine! engine||(self==Zorglub::Node ? Config.engine : nil )
- sub.layout! layout||(self==Zorglub::Node ? Config.layout : nil )
+ sub.engine! engine||(self==Zorglub::Node ? UNDEFINED : nil )
+ sub.layout! layout||(self==Zorglub::Node ? UNDEFINED : nil )
sub.instance_variable_set :@inherited_vars, {}
@inherited_vars.each do |s,v| sub.inherited_var s, *v end
end
@@ -212,7 +224,7 @@ module Zorglub
def call env
meth, *args = env['PATH_INFO'].sub(/^\//,'').split '/'
meth||= 'index'
- puts "=> #{meth}(#{args.join ','})" if Config.debug
+ puts "=> #{meth}(#{args.join ','})" if app.opt :debug
node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args,:static=>static}
return error_404 node if not node.respond_to? meth
node.realize!
@@ -226,7 +238,7 @@ module Zorglub
end
#
def error_404 node
- puts " !! method not found" if Config.debug
+ puts " !! method not found" if app.opt :debug
resp = node.response
resp.status = 404
resp['Content-Type'] = 'text/plain'
@@ -280,12 +292,12 @@ module Zorglub
def static_page! path
if not File.exists? path
compile_page!
- Dir.mkdir Config.static_base_path
+ Dir.mkdir app.static_base_path
Dir.mkdir File.dirname path
File.open(path, 'w') {|f| f.write("@mime:"+@mime+"\n"); f.write(@content); }
- puts " * cache file created : #{path}" if Config.debug
+ puts " * cache file created : #{path}" if app.opt :debug
else
- puts " * use cache file : #{path}" if Config.debug
+ puts " * use cache file : #{path}" if app.opt :debug
content = File.open(path, 'r') {|f| f.read }
@content = content.sub /^@mime:(.*)\n/,''
@mime = $1
@@ -293,12 +305,10 @@ module Zorglub
end
#
def compile_page!
- e, @options[:ext] = Config.engine_proc_ext @options[:engine], @options[:ext]
- v, l = view, layout
- if Config.debug
- puts " * "+(File.exists?(l) ? 'use layout' : 'not found layout')+" : "+l
- puts " * "+(File.exists?(v) ? 'use view ' : 'not found view ')+" : "+v
- end
+ e, @options[:ext] = app.engine_proc_ext @options[:engine], @options[:ext]
+ v, l, debug = view, layout, app.opt(:debug)
+ puts " * "+(File.exists?(l) ? 'use layout' : 'not found layout')+" : "+l if debug
+ puts " * "+(File.exists?(v) ? 'use view ' : 'not found view ')+" : "+v if debug
state (@options[:layout].nil? ? :partial : :view)
@content, mime = e.call v, self if e and File.exists? v
@mime = mime unless mime.nil?
diff --git a/lib/zorglub/session.rb b/lib/zorglub/session.rb
index 9e54a6b..7e91555 100644
--- a/lib/zorglub/session.rb
+++ b/lib/zorglub/session.rb
@@ -13,17 +13,18 @@ module Zorglub
end
#
def session
- @session ||= SessionHash.new @request, @response, Node.sessions
+ @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options)
end
end
#
class SessionHash < Hash
#
- def initialize req, resp, sessions
+ def initialize req, resp, sessions, options
@request = req
@response = resp
@sessions = sessions
@sid = nil
+ @options = options
super()
end
#
@@ -46,7 +47,7 @@ module Zorglub
#
def clear
load_data!
-# @response.delete_cookie Zorglub::Config.session_key
+# @response.delete_cookie @options[:session_key]
# @sessions.delete @sid
# @sid = nil
super
@@ -94,11 +95,11 @@ module Zorglub
#
def load_data!
return if loaded?
- if Zorglub::Config.session_on
- sid = @request.cookies[Zorglub::Config.session_key]
+ if @options[:session_on]
+ sid = @request.cookies[@options[:session_key]]
if sid.nil?
sid = generate_sid!
- @response.set_cookie Zorglub::Config.session_key, sid
+ @response.set_cookie @options[:session_key], sid
end
replace @sessions[sid] ||={}
@sessions[sid] = self
@@ -125,7 +126,7 @@ module Zorglub
# SecureRandom is available since Ruby 1.8.7.
# For Ruby versions earlier than that, you can require the uuidtools gem,
# which has a drop-in replacement for SecureRandom.
- def sid_algorithm; SecureRandom.hex(Zorglub::Config.session_sid_len); end
+ def sid_algorithm; SecureRandom.hex(@options[:session_sid_len]); end
rescue LoadError
require 'openssl'
# Using OpenSSL::Random for generation, this is comparable in performance
@@ -133,7 +134,7 @@ module Zorglub
# have the same behaviour as the SecureRandom::hex method of the
# uuidtools gem.
def sid_algorithm
- OpenSSL::Random.random_bytes(Zorglub::Config.session_sid_len / 2).unpack('H*')[0]
+ OpenSSL::Random.random_bytes(@options[:session_sid_len] / 2).unpack('H*')[0]
end
rescue LoadError
# Digest::SHA2::hexdigest produces a string of length 64, although
diff --git a/spec/node_spec.rb b/spec/node_spec.rb
index 72230bc..98e7f99 100644
--- a/spec/node_spec.rb
+++ b/spec/node_spec.rb
@@ -3,7 +3,7 @@
require 'spec_helper'
#
def clean_static_path
- static_base_path = Zorglub::Config.static_base_path
+ static_base_path = Node0.app.static_base_path
Dir.glob( File.join(static_base_path,'**','*') ).each do |f| File.unlink f if File.file? f end
Dir.glob( File.join(static_base_path,'*') ).each do |d| Dir.rmdir d end
Dir.rmdir static_base_path if File.directory? static_base_path
@@ -22,12 +22,11 @@ describe Zorglub do
end
#
it "engine should return default Node's engine" do
- Node0.engine.should == Zorglub::Config.engine
- Node0.engine.should == Zorglub::Config[:engine]
+ Node0.engine.should == Node0.app.opt(:engine)
end
#
it "layout should return default Node's layout" do
- Node0.layout.should == Zorglub::Config.layout
+ Node0.layout.should == Node0.app.opt(:layout)
end
#
it "engine should return class defined Node's engine" do
@@ -85,8 +84,8 @@ describe Zorglub do
r = Node0.my_call '/index'
r.status.should == 200
h = YAML.load r.body[0]
- ly = File.join Zorglub::Config.root, Zorglub::Config.layout_dir, Node0.layout
- vu = File.join Zorglub::Config.root, Zorglub::Config.view_dir, Node0.r, 'index'
+ ly = File.join Node0.app.layout_base_path, Node0.layout
+ vu = File.join Node0.app.view_base_path, Node0.r, 'index'
h[:path].should == ly
h[:layout].should == ly
h[:view].should == vu
@@ -96,8 +95,8 @@ describe Zorglub do
r = Node1.my_call '/index'
r.status.should == 200
h = YAML.load r.body[0]
- ly = File.join Zorglub::Config.root, Zorglub::Config.layout_dir, 'main.spec'
- vu = File.join Zorglub::Config.root, Zorglub::Config.view_dir, Node1.r, 'index.spec'
+ ly = File.join Node1.app.layout_base_path, 'main.spec'
+ vu = File.join Node1.app.view_base_path, Node1.r, 'index.spec'
h[:path].should == ly
h[:layout].should == ly
h[:view].should == vu
@@ -236,13 +235,13 @@ describe Zorglub do
it "view_base_path! should work" do
r = Node7.my_call '/view_path'
h = YAML.load r.body[0]
- h[:view].should == File.join(Zorglub::Config.root, 'alt','do_render')
+ h[:view].should == File.join(Node7.app.opt(:root), 'alt','do_render')
end
#
it "layout_base_path! should work" do
r = Node7.my_call '/view_path'
h = YAML.load r.body[0]
- h[:layout].should == File.join(Zorglub::Config.root, 'alt','layout','default')
+ h[:layout].should == File.join(Node7.app.opt(:root), 'alt','layout','default')
end
#
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 78ce08f..660bd39 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -28,14 +28,7 @@ RENDER_PROC = Proc.new { |path,obj|
raise Exception.new
end
}
-Zorglub::Config.register_engine 'default', nil, HASH_PROC
-Zorglub::Config.register_engine 'engine-1', 'spec', HASH_PROC
-Zorglub::Config.register_engine 'engine-2', 'spec', HASH_PROC
-Zorglub::Config.register_engine 'real', nil, RENDER_PROC
-Zorglub::Config.register_engine 'static', nil, STATIC_PROC
-#
-Zorglub::Config[:engine] = 'default'
-Zorglub::Config.root = File.join Dir.pwd, 'spec', 'data'
+APP_ROOT = File.join Dir.pwd, 'spec', 'data'
#
class Zorglub::Node
def self.my_call uri
@@ -169,15 +162,23 @@ class Node6 < Zorglub::Node
end
#
class Node7 < Zorglub::Node
- layout_base_path! File.join Zorglub::Config.root, 'alt','layout'
- view_base_path! File.join Zorglub::Config.root, 'alt'
+ layout_base_path! File.join APP_ROOT, 'alt','layout'
+ view_base_path! File.join APP_ROOT, 'alt'
def view_path
view! 'do_render'
end
end
-
#
APP = Zorglub::App.new do
+ register_engine! :file, nil, Zorglub::Engines::File.method(:proc)
+ register_engine! :haml, 'haml', Zorglub::Engines::Haml.method(:proc)
+ register_engine! 'default', nil, HASH_PROC
+ register_engine! 'engine-1', 'spec', HASH_PROC
+ register_engine! 'engine-2', 'spec', HASH_PROC
+ register_engine! 'real', nil, RENDER_PROC
+ register_engine! 'static', nil, STATIC_PROC
+ opt! :root, APP_ROOT
+ opt! :engine, 'default'
map '/node0', Node0
map '/node1', Node1
map '/node3', Node3
@@ -187,6 +188,7 @@ APP = Zorglub::App.new do
map '/node7', Node7
map '/node8', Node8
end
+#
class Node2
map APP, '/node2'
end