Exception: ArgumentError

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

Class Method Summary collapse

Class Method Details

.block_required!(block) ⇒ Object

Raises:

  • (self)


106
107
108
# File 'lib/gorillib/exception/raisers.rb', line 106

def self.block_required!(block)
  raise self.new("Block is required") unless block
end

.check_type!(obj, types, *args) ⇒ Object

Examples:

simple

TypeMismatchError.mismatched!(:foo)
  #=> "TypeMismatchError: :foo has mismatched type

Can supply the types or duck-types that are expected:

TypeMismatchError.mismatched!(:foo, [:to_str, Integer])
  #=> "TypeMismatchError: :foo has mismatched type; expected #to_str or Integer"

Parameters:



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gorillib/exception/raisers.rb', line 122

def self.check_type!(obj, types, *args)
  types = Array(types)
  return true if types.any? do |type|
    case type
    when Module then obj.is_a?(type)
    when Symbol then obj.respond_to?(type)
    else raise StandardError, "Can't check type #{type} -- this is an error in the call to the type-checker, not in the object the type-checker is checking"
    end
  end
  self.mismatched!(obj, types, *args)
end

.mismatched!(obj, types = [], msg = nil, *args) ⇒ Object

Examples:

simple

TypeMismatchError.mismatched!(:foo)
  #=> "TypeMismatchError: :foo has mismatched type

Can supply the types or duck-types that are expected:

TypeMismatchError.mismatched!(:foo, [:to_str, Integer])
  #=> "TypeMismatchError: :foo has mismatched type; expected #to_str or Integer"

Parameters:

Raises:

  • (self)


94
95
96
97
98
99
100
101
102
103
# File 'lib/gorillib/exception/raisers.rb', line 94

def self.mismatched!(obj, types=[], msg=nil, *args)
  types = Array(types)
  message = (obj.inspect rescue '(uninspectable object)')
  message << " has mismatched type"
  message << ': ' << msg if msg
  unless types.empty?
    message << '; expected ' << types.map{|type| type.is_a?(Symbol) ? "##{type}" : type.to_s }.join(" or ")
  end
  raise self, message, *args
end