module Sinatra::Bells::Helpers::Controller

Public Class Methods

included(base) click to toggle source
# File lib/sinatra/bells/helpers/controller.rb, line 36
def self.included(base)
  error_code = Rack::Utils::HTTP_STATUS_CODES

  base.class_eval {
    [400, 401, 403, 404, 500].each { |code|
      error(code) {
        @code, @error = code, error_code[code]
        render_error
      }
    }

    [400, 401, 403].each { |code|
      name = error_code[code].downcase.tr(' ', '_')
      define_method(name) { |body = nil| error(code, body) }
    }
  }
end

Protected Instance Methods

render_error() click to toggle source
# File lib/sinatra/bells/helpers/controller.rb, line 56
def render_error
  erb ''
end
render_json() click to toggle source
# File lib/sinatra/bells/helpers/controller.rb, line 65
def render_json
  JSON.fast_generate(to_render_hash)
end
send_file(file, options = {}) click to toggle source
Calls superclass method
# File lib/sinatra/bells/helpers/controller.rb, line 60
def send_file(file, options = {})
  options.is_a?(Hash) ? super :
    File.readable?(file) ? super(file, type: options) : not_found
end

Private Instance Methods

to_render_hash(hash = {}, compact = true) click to toggle source
# File lib/sinatra/bells/helpers/controller.rb, line 71
def to_render_hash(hash = {}, compact = true)
  if hash.is_a?(Array)
    ary, hash, at = hash, {}, '@'

    ary.each { |key| hash[key] = instance_variable_get(
      (name = key.to_s).start_with?(at) ? name : "#{at}#{name}") }
  end

  compact ? hash.delete_if { |_, value| !value } : hash
end