summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-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
7 files changed, 106 insertions, 118 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