class Hobbit::Base

Constants

CONTROLLERS_PATH
INITIALIZERS_PATH
MODELS_PATH
VIEWS_PATH

Public Class Methods

any(path, verbs, &block) click to toggle source
# File lib/halfling/base.rb, line 61
def any(path, verbs, &block)
  verbs.each do |verb|
    routes[verb.to_s.upcase] << compile_route(path, &block)
  end
end
config() click to toggle source
# File lib/halfling/base.rb, line 57
def config
  @@config
end
inherited(sub) click to toggle source
# File lib/halfling/base.rb, line 18
def inherited(sub)
  if sub.superclass == Base
    #config stuff
    if File.exists?('config/env.yml')
      all_conf=YAML.load_file 'config/env.yml'
      conf=all_conf[ENV['RACK_ENV']]
      @@templates={}
      @@config=conf
      @@application=sub
      use Rack::Config do |env|
        conf.each do |cf, val|
          env[cf.to_sym]=val
        end
      end
    end
   
    class << sub
      def inherited(sub)
        name=sub.name.match(".*?::(.*)Controller")[1].downcase!
        if name == "root"
          @@root_controller = sub
        else
          @@controller_routes ||= {}
          @@controller_routes["/#{name}"]=sub
        end
      end

      #standard paths and stuff
      paths = [INITIALIZERS_PATH, MODELS_PATH, CONTROLLERS_PATH]
      paths.each do |path|
        Dir[path].sort!.each {|f| require File.expand_path(f) }
      end
    end
    
    #templates
    reload_templates
  end
end
new() click to toggle source
Calls superclass method
# File lib/halfling/base.rb, line 98
def initialize
  if self.class.class_variable_defined?(:@@controller_routes) && @@controller_routes
    @@controller_routes.each do |route, controller|
      @@application.map(route) do
        run controller.new
      end
    end
    @@controller_routes = nil
  end
  if self.class.class_variable_defined?(:@@root_controller) && @@root_controller
    root_controller = @@root_controller
    @@application.map("/") do
      run root_controller.new
    end
    @@root_controller = nil
  end
  @render_nesting_level = 0
  super
end
reload_templates() click to toggle source
# File lib/halfling/base.rb, line 67
def reload_templates
  #find and remember all known templates
  @@templates={}
  Dir["#{VIEWS_PATH}/**/*.*"].each do |f|
    m=f.match(/^#{VIEWS_PATH}\/(?<tmpl>.*)\.\w+/)
    k=m[:tmpl].to_sym
    if @@templates[k]
      raise "Template for #{k} already present, should not overwrite. (wanted to replace #{@@templates[k]} with #{f})"
    end
    @@templates[k]=f
  end
end

Public Instance Methods

default_layout() click to toggle source
# File lib/halfling/base.rb, line 124
def default_layout
  if @render_nesting_level <= 1
    find_template :"layouts/application"
  else
    #with nested rendering, default layout is nil.
    nil
  end
end
find_template(template) click to toggle source

template stuff

# File lib/halfling/base.rb, line 119
def find_template(template)
  tmpl_path=@@templates[template.to_sym]
  raise "template \"#{template}\" not found" unless tmpl_path
  tmpl_path
end
js_response!() click to toggle source
# File lib/halfling/base.rb, line 150
def js_response!
  set_content_type "application/javascript"
end
json_response!() click to toggle source
# File lib/halfling/base.rb, line 147
def json_response!
  set_content_type "application/json"
end
param(name) click to toggle source
# File lib/halfling/base.rb, line 156
def param(name)
  params[name]
end
params() click to toggle source
# File lib/halfling/base.rb, line 153
def params
  request.params
end
render(*args) click to toggle source
Calls superclass method
# File lib/halfling/base.rb, line 136
def render(*args)
  @render_nesting_level += 1
  ret = super
  @render_nesting_level -= 1
  ret
end
set_content_type(val) click to toggle source

convenience stuff

# File lib/halfling/base.rb, line 144
def set_content_type (val)
  response.headers["Content-Type"]=val
end
template_engine() click to toggle source
# File lib/halfling/base.rb, line 132
def template_engine
  raise "template_engine shouldn't be called"
end