Exception: Currency::Exception::Base

Inherits:
Exception
  • Object
show all
Defined in:
lib/currency/exception.rb

Overview

Base class for all Currency::Exception objects.

raise Currency::Exception [ "msg", :opt1, 1, :opt2, 2 ]

Constant Summary collapse

EMPTY_HASH =
{ }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(arg1, *args) ⇒ Base



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
# File 'lib/currency/exception.rb', line 12

def initialize(arg1, *args)
  case arg1
  # [ description, ... ]
  when Array
    @opts = arg1
    arg1 = arg1.shift
  else
    @opts = nil
  end

  case @opts
  when Array
    if @opts.size == 1 && @opts.first.kind_of?(Hash)
      # [ description, { ... } ]
      @opts = @opts.first
    else
      # [ description, :key, value, ... ]
      @opts = Hash[*@opts]
    end
  end

  case @opts
  when Hash
    @opts = @opts.dup.freeze
  else
    @opts = { :info => @opts }.freeze
  end

  @opts ||= EMPTY_HASH

  super(arg1, *args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sel, *args, &blk) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/currency/exception.rb', line 46

def method_missing(sel, *args, &blk)
  sel = sel.to_sym
  if args.empty? && ! block_given? && @opts.key?(sel) 
    return @opts[sel]
  end
  super
end

Instance Method Details

#to_sObject



54
55
56
# File 'lib/currency/exception.rb', line 54

def to_s
  super + ": #{@opts.inspect}"
end