Module: Kernel

Defined in:
lib/pebkac.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) pebkac(*args)

Raise an exception but process the message with I18n

Accepts the same parameters as Kernel#raise but the string (message) may be followed by any options to be passed to I18n, additionally the string parameter may be a symbol instead of a string

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pebkac.rb', line 9

def pebkac *args
  # Extract exception class, default to RuntimeError
  exception_class = args.shift if args.first.respond_to? :exception
  exception_class ||= RuntimeError

  # Extract message key, default to exception class name
  if args.first.is_a?(Symbol) || args.first.is_a?(String)
    msg_key = args.shift
  elsif exception_class == RuntimeError
    msg_key = :default
  else
    # Compute the default message key
    # Based on ActiveSupport's underscore method
    msg_key = exception_class.name.split("::").last
    msg_key.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    msg_key.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    msg_key.tr!("-", "_")
    msg_key.downcase!
  end

  # Extract I18n options
  options = args.shift if args.first.is_a?(Hash)
  options ||= {}

  # Extract stack tract, default to caller
  trace = args.shift
  trace ||= caller

  # Raise an error if there are leftover arguments
  raise ArgumentError unless args.empty?

  # Apply default options for I18n
  options = {:scope => :pebkac}.merge options

  # Call I18n
  message = I18n.t msg_key, options

  # Finally, raise the exception!
  raise exception_class, message, trace
end