Module: Getto::Roda::HttpErrorHelper

Defined in:
lib/getto/roda/http_error_helper.rb

Instance Method Summary collapse

Instance Method Details

#error(code, name, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/getto/roda/http_error_helper.rb', line 4

def error(code, name, &block)
  error_class_name = :"E#{code}"
  unless self.const_defined? error_class_name
    self.const_set(error_class_name, Class.new(self).tap{|klass|
      klass.class_eval{ define_method(:status){code} }
    })
  end

  self.singleton_class.class_eval do
    define_method(:"#{name}!") do |*args|
      args.unshift ":" unless args.empty?
      raise self.const_get(error_class_name), "#{name}#{args.join " "}"
    end
  end
end