module Sinatra::Bells::Helpers::Controller

Public Class Methods

included(base) click to toggle source
   # File lib/sinatra/bells/helpers/controller.rb
37 def self.included(base)
38   error_code = Rack::Utils::HTTP_STATUS_CODES
39 
40   base.class_eval {
41     [400, 401, 403, 404, 500].each { |code|
42       error(code) {
43         @code, @error = code, error_code[code]
44         render_error
45       }
46     }
47 
48     [400, 401, 403].each { |code|
49       name = error_code[code].downcase.tr(' ', '_')
50       define_method(name) { |body = nil| error(code, body) }
51     }
52   }
53 end

Protected Instance Methods

render_error() click to toggle source
   # File lib/sinatra/bells/helpers/controller.rb
57 def render_error
58   erb ''
59 end
render_json() click to toggle source
   # File lib/sinatra/bells/helpers/controller.rb
66 def render_json
67   JSON.fast_generate(to_render_hash)
68 end
send_file(file, options = {}) click to toggle source
Calls superclass method
   # File lib/sinatra/bells/helpers/controller.rb
61 def send_file(file, options = {})
62   options.is_a?(Hash) ? super :
63     File.readable?(file) ? super(file, type: options) : not_found
64 end

Private Instance Methods

to_render_hash(hash = {}, compact = true) click to toggle source
   # File lib/sinatra/bells/helpers/controller.rb
72 def to_render_hash(hash = {}, compact = true)
73   if hash.is_a?(Array)
74     ary, hash, at = hash, {}, '@'
75 
76     ary.each { |key| hash[key] = instance_variable_get(
77       (name = key.to_s).start_with?(at) ? name : "#{at}#{name}") }
78   end
79 
80   compact ? hash.delete_if { |_, value| !value } : hash
81 end