Module: Minitrain::InstanceMethods

Defined in:
lib/minitrain.rb

Constant Summary collapse

DEV_ENV =
[nil,'development','dev']

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



50
# File 'lib/minitrain.rb', line 50

def call(env); dup.call!(env); end

#call!(env) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/minitrain.rb', line 52

def call!(env)
  catch(:response) {
    @r = ::Rack::Request.new(env)
    @res = ::Rack::Response.new
    @session = env['rack.session'] || {}
    begin
      instance_eval(&self.class.dispatcher_block)
    rescue => e
      raise if DEV_ENV.include?(ENV['RACK_ENV'])
      @res.write(self.__send__('error', e, @path_atoms))
    end
    @res.status==404&&!@app.nil? ? @app.call(env) : @res.finish
  }
end

#erb(template) ⇒ Object



94
95
96
# File 'lib/minitrain.rb', line 94

def erb(template)
	tpl(template,'.erb')
end

#error(e, *args) ⇒ Object



73
74
75
76
77
# File 'lib/minitrain.rb', line 73

def error(e, *args)
  puts "\n", e.class, e.message, e.backtrace # Log the error anyway
  @res.status = 500
  "ERROR 500"
end

#haml(template) ⇒ Object



102
103
104
# File 'lib/minitrain.rb', line 102

def haml(template)
	tpl(template,'.haml')
end

#initialize(app = nil) ⇒ Object



49
# File 'lib/minitrain.rb', line 49

def initialize(app=nil); @app = app; end

#not_found(*args) ⇒ Object



67
68
69
70
71
# File 'lib/minitrain.rb', line 67

def not_found(*args)
  @res.status = 404
  @res.headers['X-Cascade']='pass'
  "NOT FOUND: #{@r.path_info}"
end

#scss(template) ⇒ Object



106
107
108
# File 'lib/minitrain.rb', line 106

def scss(template)
	tpl(template,'.scss')
end

#slim(template) ⇒ Object



98
99
100
# File 'lib/minitrain.rb', line 98

def slim(template)
	tpl(template,'.slim')
end

#tpl(template, extention) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/minitrain.rb', line 79

def tpl(template, extention)
			key = (template.to_s + extention.gsub(/[.]/,"_")).to_sym
  @@tilt_cache ||= {}
  if @@tilt_cache.has_key?(key)
    template_obj = @@tilt_cache[key]
  else
    views_location = @r.env['views'] || ::Dir.pwd+'/views/'
    views_location << '/' unless views_location[-1]==?/
    template_obj = Tilt.new("#{views_location}#{template}#{extention}")
    @@tilt_cache.store(key,template_obj) if ENV['RACK_ENV']=='production'
  end
  @res['Content-Type'] = "text/html;charset=utf-8"
  template_obj.render(self)
end