Class: Hobbit::Base

Inherits:
Object
  • Object
show all
Includes:
Environment, Render
Defined in:
lib/halfling/base.rb

Constant Summary collapse

INITIALIZERS_PATH =
'config/initializers/**/*.rb'
MODELS_PATH =
'app/models/**/*.rb'
VIEWS_PATH =
'app/views'
CONTROLLERS_PATH =
'app/controllers/**/*.rb'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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
  super
end

Class Method Details

.any(path, verbs, &block) ⇒ Object



61
62
63
64
65
# 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

.configObject



57
58
59
# File 'lib/halfling/base.rb', line 57

def config
  @@config
end

.inherited(sub) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

.reload_templatesObject



67
68
69
70
71
72
73
74
75
76
77
78
# 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

Instance Method Details

#default_layoutObject



123
124
125
# File 'lib/halfling/base.rb', line 123

def default_layout
  find_template :"layouts/application"
end

#find_template(template) ⇒ Object

template stuff



118
119
120
121
122
# File 'lib/halfling/base.rb', line 118

def find_template(template)
  tmpl_path=@@templates[template.to_sym]
  raise "template #{template} not found" unless tmpl_path
  tmpl_path
end

#js_response!Object



138
139
140
# File 'lib/halfling/base.rb', line 138

def js_response!
  set_content_type "application/javascript"
end

#json_response!Object



135
136
137
# File 'lib/halfling/base.rb', line 135

def json_response!
  set_content_type "application/json"
end

#param(name) ⇒ Object



144
145
146
# File 'lib/halfling/base.rb', line 144

def param(name)
  params[name]
end

#paramsObject



141
142
143
# File 'lib/halfling/base.rb', line 141

def params
  request.params
end

#set_content_type(val) ⇒ Object

convenience stuff



132
133
134
# File 'lib/halfling/base.rb', line 132

def set_content_type (val)
  response.headers["Content-Type"]=val
end

#template_engineObject



126
127
128
# File 'lib/halfling/base.rb', line 126

def template_engine
  raise "template_engine shouldn't be called"
end