class Femto::Base

Attributes

content_type[RW]
env[RW]
layout[RW]
layout_block[RW]
render[RW]
request[RW]
response[RW]
routes[RW]
template_dir[RW]

Public Class Methods

builder() click to toggle source
# File lib/femto.rb, line 104
def builder
  @builder = Rack::Builder.new unless @builder
  @builder
end
call(env) click to toggle source
# File lib/femto.rb, line 109
def call(env)
  dup.call! env
end
call!(env) click to toggle source
# File lib/femto.rb, line 113
def call!(env)
  env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
  @request = Rack::Request.new env
  @response = Rack::Response.new
  @render = nil
  @content_type = nil
  @env = env
  handle_request
  @response.finish
end
handle_request() click to toggle source
# File lib/femto.rb, line 38
def handle_request
  catch :stop do
    verb = request.request_method.downcase
    request_path = request.path_info
    routes = @routes[verb]
    if routes
      routes.each do |group|
        path = group[0]
        options = group[1]
        block = group[2]
        if request_path == path
          instance_eval(&block)
          if options[:template]
            render options[:template]
          elsif options[:view]
            render options[:view]
          end
          if @layout and @content_type == 'text/html'
            if @layout_block
              @layout_block.call
            end
            @render = render_template(@layout) { @render }
          end
          if @render == nil
            raise TemplateMissingException.new
          end
          response.write @render
          response['Content-Type'] = content_type
          return
        end
      end
    end
    stop
  end
end
model(name, &block) click to toggle source
# File lib/femto.rb, line 87
def model(name, &block)
  Femto::Model.create_model name.to_s, &block
end
render_template(template, &block) click to toggle source
# File lib/femto.rb, line 95
def render_template(template, &block)
  @content_type = 'text/html'
  Tilt.new(template).render self, {}, &block
end
resolve_template(file) click to toggle source
# File lib/femto.rb, line 91
def resolve_template(file)
  Dir[File.join(@template_dir, file + '.*')][0]
end
set_route(verb, path, options, block) click to toggle source
# File lib/femto.rb, line 32
def set_route(verb, path, options, block)
  @routes = {} unless @routes
  @routes[verb] = [] unless @routes[verb]
  @routes[verb] << [path, options, block]
end
stop() click to toggle source
# File lib/femto.rb, line 100
def stop
  throw :stop
end