diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/zorglub.rb | 6 | ||||
-rw-r--r-- | lib/zorglub/app.rb | 38 | ||||
-rw-r--r-- | lib/zorglub/engines/file.rb | 6 | ||||
-rw-r--r-- | lib/zorglub/engines/haml.rb | 6 | ||||
-rw-r--r-- | lib/zorglub/engines/sass.rb | 6 | ||||
-rw-r--r-- | lib/zorglub/node.rb | 144 | ||||
-rw-r--r-- | lib/zorglub/rack_session.rb | 10 | ||||
-rw-r--r-- | lib/zorglub/session.rb | 60 |
8 files changed, 137 insertions, 139 deletions
diff --git a/lib/zorglub.rb b/lib/zorglub.rb index ba3fbc9..4f4fdc8 100644 --- a/lib/zorglub.rb +++ b/lib/zorglub.rb @@ -1,11 +1,11 @@ #! /usr/bin/env ruby # -*- coding: UTF-8 -*- -# + module Zorglub VERSION = '0.0.9' end -# + require 'zorglub/node' require 'zorglub/app' -# + # EOF diff --git a/lib/zorglub/app.rb b/lib/zorglub/app.rb index 3efad1b..43b03b7 100644 --- a/lib/zorglub/app.rb +++ b/lib/zorglub/app.rb @@ -1,11 +1,11 @@ # -*- coding: UTF-8 -*- -# + require 'rack' -# + module Zorglub - # + class App < Rack::URLMap - # + def initialize map={}, &block super @map = map @@ -39,9 +39,9 @@ module Zorglub instance_eval &block if block_given? remap @map end - # + attr_reader :engines_cache - # + def map location, object return unless location and object raise Exception.new "#{@map[location]} already mapped to #{location}" if @map.has_key? location @@ -49,34 +49,34 @@ module Zorglub @map.merge! location.to_s=>object remap @map end - # + def delete location @map.delete location remap @map end - # + def at location @map[location] end - # + def to object @map.invert[object] end - # + def to_hash @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? @@ -86,30 +86,28 @@ module Zorglub 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 # # EOF diff --git a/lib/zorglub/engines/file.rb b/lib/zorglub/engines/file.rb index 75f6f30..dbf579d 100644 --- a/lib/zorglub/engines/file.rb +++ b/lib/zorglub/engines/file.rb @@ -1,8 +1,8 @@ # -*- coding: UTF-8 -*- -# + require 'haml/util' require 'haml/engine' -# + module Zorglub module Engines module File @@ -14,5 +14,5 @@ module Zorglub end end end -# + # EOF diff --git a/lib/zorglub/engines/haml.rb b/lib/zorglub/engines/haml.rb index 9325d19..382645e 100644 --- a/lib/zorglub/engines/haml.rb +++ b/lib/zorglub/engines/haml.rb @@ -1,8 +1,8 @@ # -*- coding: UTF-8 -*- -# + require 'haml/util' require 'haml/engine' -# + module Zorglub module Engines module Haml @@ -19,5 +19,5 @@ module Zorglub end end end -# + # EOF diff --git a/lib/zorglub/engines/sass.rb b/lib/zorglub/engines/sass.rb index d302aec..bc6b759 100644 --- a/lib/zorglub/engines/sass.rb +++ b/lib/zorglub/engines/sass.rb @@ -1,7 +1,7 @@ # -*- coding: UTF-8 -*- -# + require 'sass' -# + module Zorglub module Engines module Sass @@ -18,5 +18,5 @@ module Zorglub end end end -# + # EOF diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 0e4ef20..42b0304 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -1,175 +1,175 @@ # -*- coding: UTF-8 -*- -# + require 'fileutils' -# + module Zorglub - # + class Node - # + UNDEFINED=-1 - # + # class level engine, layout, static, layout_base_path, view_base_path configuration - # + class << self - # + attr_reader :static, :cache_lifetime - # + def engine! engine @engine = engine end - # + def engine @engine = @app.opt(:engine) if @engine==UNDEFINED and @app @engine end - # + def no_layout! 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, lifetime=0 @static = ( (val==true or val==false) ? val : false ) @cache_lifetime = lifetime end - # + def layout_base_path! path @layout_base_path = path end - # + def layout_base_path @layout_base_path ||= @app.layout_base_path end - # + def view_base_path! path @view_base_path = path end - # + def view_base_path @view_base_path ||= @app.view_base_path end end - # + # instance level engine, layout, view, static configuration - # + def engine! engine @engine = engine end - # + def no_layout! layout! nil end - # + def layout! layout @layout = layout end - # + def layout return nil if @layout.nil? File.join(self.class.layout_base_path, @layout)+ext end - # + def no_view! view! nil end - # + def view! view @view = view end - # + def view return nil if @view.nil? File.join(self.class.view_base_path, @view)+ext end - # + def static! val, lifetime=0 @static = ( (val==true or val==false) ? val : false ) @cache_lifetime = lifetime end - # + def static return nil if not @static or @view.nil? File.join(app.static_base_path, @view)+ext end - # + def ext! ext @ext = ( (ext.nil? or ext.empty?) ? nil : (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext) ) end - # + def ext @ext || '' end - # + def mime! mime @mime = mime end - # + # class level basic node functions - # + class << self - # + attr_accessor :app def map app, location @app = app @app.map location, self end - # + def r *args @r ||= @app.to self (args.empty? ? @r : File.join( @r, args.map { |x| x.to_s } ) ) end - # + end - # + # instance level basic node functions - # + def app self.class.app end - # + def map self.class.r end - # + def r *args File.join map, (args.empty? ? meth : args.map { |x| x.to_s } ) end - # + def html [ :map, :r, :args, :engine, :layout, :view ].inject('') { |s,sym| s+="<p>#{sym} => #{self.send sym}</p>"; s } end - # + def redirect target, options={}, &block status = options[:status] || 302 body = options[:body] || redirect_body(target) header = response.header.merge('Location' => target.to_s) throw :stop_realize, Rack::Response.new(body, status, header, &block) end - # + def redirect_body target "You are being redirected, please follow this link to: <a href='#{target}'>#{target}</a>!" end - # + # class level inherited values are key=>array, copied at inheritance # so they can be extanded at class level # values are copied from class into instance at object creation # so that can be extanded without modifying class level values # typical usage are css or js inclusions - # + @cli_vals = { } - # + class << self - # + attr_reader :cli_vals - # + def cli_val sym, *args vals = @cli_vals[sym] ||= [] unless args.empty? @@ -178,9 +178,9 @@ module Zorglub end vals end - # + end - # + def cli_val sym, *args vals = @cli_vals[sym] ||= [] unless args.empty? @@ -189,44 +189,44 @@ module Zorglub end vals end - # + # before_all and after_all hooks - # + @cli_vals[:before_all] = [] @cli_vals[:after_all] = [] class << self - # + def call_before_hooks obj @cli_vals[:before_all].each do |blk| blk.call obj end end - # + def before_all meth=nil, &blk @cli_vals[:before_all]<< ( meth.nil? ? blk : meth ) @cli_vals[:before_all].uniq! end - # + def call_after_hooks obj @cli_vals[:after_all].each do |blk| blk.call obj end end - # + def after_all meth=nil, &blk @cli_vals[:after_all]<< ( meth.nil? ? blk : meth ) @cli_vals[:after_all].uniq! end - # + end - # + # rack entry point, page computation methods - # + class << self - # + def inherited sub sub.engine! ( engine || (self==Zorglub::Node ? UNDEFINED : nil ) ) sub.layout! ( layout || (self==Zorglub::Node ? UNDEFINED : nil ) ) sub.instance_variable_set :@cli_vals, {} @cli_vals.each do |s,v| sub.cli_val s, *v end end - # + def call env meth, *args = env['PATH_INFO'].sub(/^\/+/,'').split(/\//) meth ||= 'index' @@ -235,14 +235,14 @@ module Zorglub return error_404 node, meth if not node.respond_to? meth node.realize! end - # + def partial env, meth, *args node = self.new env, meth.to_s, args, true return error_404 node, meth if not node.respond_to? meth node.feed! env[:no_hooks] node.content end - # + def error_404 node, meth $stderr << " !! #{node.class.name}::#{meth} not found\n" if app.opt :debug resp = node.response @@ -251,11 +251,11 @@ module Zorglub resp.write "%s mapped at %p can't respond to : %p" % [ node.class.name, node.map, node.meth ] resp end - # + end - # + attr_reader :request, :response, :content, :mime, :state, :engine, :meth, :args - # + def initialize env, meth, args, partial=false @meth = meth @args = args @@ -271,7 +271,7 @@ module Zorglub @cache_lifetime = self.class.cache_lifetime self.class.cli_vals.each do |s,v| cli_val s, *v end end - # + def realize! catch(:stop_realize) { feed! @@ -281,7 +281,7 @@ module Zorglub response } end - # + def feed! no_hooks=false @state = :pre_cb self.class.call_before_hooks self unless no_hooks @@ -298,7 +298,7 @@ module Zorglub @state = :finished return @content, @mime end - # + def static_page! path if File.exists?(path) and ( @cache_lifetime.nil? or @cache_lifetime==0 or ( (Time.now-File.stat(path).mtime) < @cache_lifetime ) ) $stderr << " * use cache file : #{path}\n" if @debug @@ -312,7 +312,7 @@ module Zorglub $stderr << " * cache file created : #{path}\n" if @debug end end - # + def compile_page! e, @ext = app.engine_proc_ext @engine, @ext v, l = view, layout @@ -328,9 +328,9 @@ module Zorglub @content, mime = e.call l, self if e and l and File.exists? l @mime = mime if @mime.nil? and not mime.nil? end - # + end - # + end -# + # EOF diff --git a/lib/zorglub/rack_session.rb b/lib/zorglub/rack_session.rb index 7e2bda7..b7c19fc 100644 --- a/lib/zorglub/rack_session.rb +++ b/lib/zorglub/rack_session.rb @@ -1,13 +1,13 @@ # -*- coding: UTF-8 -*- -# + module Zorglub - # + class Node - # + def session @request.session end - # + end - # + end diff --git a/lib/zorglub/session.rb b/lib/zorglub/session.rb index 7351a48..db36fd2 100644 --- a/lib/zorglub/session.rb +++ b/lib/zorglub/session.rb @@ -1,24 +1,24 @@ # -*- coding: UTF-8 -*- -# + require 'securerandom' -# + module Zorglub - # + class Node - # + @sessions = {} - # + class << self attr_reader :sessions end - # + def session @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options) end end - # + class SessionHash < Hash - # + def initialize req, resp, sessions, options @request = req @response = resp @@ -27,49 +27,49 @@ module Zorglub @options = options super() end - # + def [] key load_data! super key end - # + def has_key? key load_data! super key end alias :key? :has_key? alias :include? :has_key? - # + def []= key, value load_data! super key, value end - # + def clear load_data! -# @response.delete_cookie @options[:key] -# @sessions.delete @sid -# @sid = nil + # @response.delete_cookie @options[:key] + # @sessions.delete @sid + # @sid = nil super end - # + def to_hash load_data! h = {}.replace(self) h.delete_if { |k,v| v.nil? } h end - # + def update hash load_data! super stringify_keys(hash) end - # + def delete key load_data! super key end - # + def inspect if loaded? super @@ -77,22 +77,22 @@ module Zorglub "#<#{self.class}:0x#{self.object_id.to_s(16)} not yet loaded>" end end - # + def exists? ( loaded? ? @sessions.has_key?(@sid) : false ) end - # + def loaded? not @sid.nil? end - # + def empty? load_data! super end - # + private - # + def load_data! return if loaded? if @options[:enabled] @@ -106,7 +106,7 @@ module Zorglub @sid = sid end end - # + def stringify_keys other hash = {} other.each do |key, value| @@ -114,12 +114,12 @@ module Zorglub end hash end - # + def generate_sid! begin sid = sid_algorithm end while @sessions.has_key? sid sid end - # + begin require 'securerandom' # Using SecureRandom, optional length. @@ -148,9 +148,9 @@ module Zorglub Digest::SHA2.hexdigest(entropy.join) end end - # + end - # + end -# + # EOF |