at posts/single.html

フレームワーク Sizuku

かんさんが作られた軽量フレームワークの「Nagisa」をベースにして、シンプルなフレームワークを作ってみた。 「Nagisa」にちなんで名前は「Sizuku」。 「Nagisa」と同じく、PATH_INFOでアクションを切り分けるようになっている。

Sizuku::Handlerを継承して使う感じ。サンプルは後ほど。

module Sizuku
 class Request
   def initialize(cgi)
     @cgi = cgi
     @cgi.params.each do |key, val|
       unless instance_variable_get("@#{key}")
         val = val[0] if val.size == 1
         instance_variable_set("@#{key}", val)
       end
     end
   end

   attr_reader :cgi
 end

 class Response
   def initialize(cgi)
     @header = {}
     @cgi = cgi
   end

   def set_redirect(uri)
     @header['status']   = '301 Moved Permanently'
     @header['Location'] = uri
   end

   def output(body = nil)
     print @cgi.header(@header)
     print body
   end

   attr_accessor :header
 end

 class Handler
   def initialize(conf)
   end

   def handle(cgi)
     req = Request.new(cgi)
     res = Response.new(cgi)

     if cgi.path_info =~ /\/?(.*)/
       method = $1.gsub('/','_')
     end

     method ||= 'main'
     content = __send__('handle_' + method, req, res)
     if content
       content.template_id ||= method
       content.template_path = 'template/'
       res.output(content.output)
     else
       res.output
     end
   end
 end

 # Template modules

 module BaseTemplate
   attr_accessor :template_id, :template_path

   def output
     @template_path ||= '.'
     template = "#{@template_path}/#{@template_id}"
     _output(template)
   end
 end

 begin
   require 'amrita2'
   require 'amrita2/template'
   require 'stringio'

   module Amrita2Template
     include BaseTemplate
     include Amrita2

     def _output(template)
       out = StringIO.new
       t = Amrita2::TemplateFile.new("#{template}.html")
       t.amrita_id = 'amrita_id'
       t.expand(out, self)
       out.string
     end
   end
 rescue LoadError
   # STDERR.puts 'Amrita2 is not found'
 end

 begin
   require 'erb'

   module ErbTemplate
     include BaseTemplate

     def _output(template)
       ERB.new(File.open("#{template}.rhtml"){|f| f.read}).result(binding())
     end
   end
 rescue LoadError
   # STDERR.puts 'ERB is not found'
 end
end

関連する日記