Module: Monoriel::InstanceMethods
- Defined in:
- lib/monoriel.rb
Constant Summary collapse
- DEV_ENV =
[nil,'development','dev']
Instance Method Summary collapse
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
- #erb(template, &block) ⇒ Object
- #error(e, *args) ⇒ Object
- #haml(template, &block) ⇒ Object
- #initialize(app = nil) ⇒ Object
- #not_found(*args) ⇒ Object
- #scss(template, &block) ⇒ Object
- #slim(template, &block) ⇒ Object
- #tpl(template, extention, &block) ⇒ Object
Instance Method Details
#call(env) ⇒ Object
52 |
# File 'lib/monoriel.rb', line 52 def call(env); dup.call!(env); end |
#call!(env) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/monoriel.rb', line 54 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, &block) ⇒ Object
98 99 100 101 102 |
# File 'lib/monoriel.rb', line 98 def erb(template,&block) tpl(template,'.erb') do yield if block_given? end end |
#error(e, *args) ⇒ Object
75 76 77 78 79 |
# File 'lib/monoriel.rb', line 75 def error(e, *args) puts "\n", e.class, e., e.backtrace # Log the error anyway @res.status = 500 "ERROR 500" end |
#haml(template, &block) ⇒ Object
110 111 112 113 114 |
# File 'lib/monoriel.rb', line 110 def haml(template,&block) tpl(template,'.haml',block) do yield if block_given? end end |
#initialize(app = nil) ⇒ Object
51 |
# File 'lib/monoriel.rb', line 51 def initialize(app=nil); @app = app; end |
#not_found(*args) ⇒ Object
69 70 71 72 73 |
# File 'lib/monoriel.rb', line 69 def not_found(*args) @res.status = 404 @res.headers['X-Cascade']='pass' "NOT FOUND: #{@r.path_info}" end |
#scss(template, &block) ⇒ Object
116 117 118 119 120 |
# File 'lib/monoriel.rb', line 116 def scss(template,&block) tpl(template,'.scss',block) do yield if block_given? end end |
#slim(template, &block) ⇒ Object
104 105 106 107 108 |
# File 'lib/monoriel.rb', line 104 def slim(template,&block) tpl(template,'.slim') do yield if block_given? end end |
#tpl(template, extention, &block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/monoriel.rb', line 81 def tpl(template, extention,&block) 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) do yield if block_given? end end |