Exception: SuperModel::Error::Type

Inherits:
SuperModel::Error show all
Defined in:
lib/super_model/error/type.rb

Direct Known Subclasses

HashType, IntegerType, ProcType

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check(object, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/super_model/error/type.rb', line 20

def check(object, options={})
  type_assertion = valid_types.any? { |type| object.is_a?(type) } unless valid_types.empty?
  method_assertion = valid_methods.any? { |type| object.respond_to?(type) } unless valid_methods.empty?
  
  if type_assertion && method_assertion
    raise self unless type_assertion || method_assertion
  elsif type_assertion && !method_assertion
    raise self unless type_assertion
  elsif !type_assertion && method_assertion
    raise self unless method_assertion
  else
    # TODO
  end
  
  object
end

.valid_methods(*method_names) ⇒ Object



15
16
17
18
# File 'lib/super_model/error/type.rb', line 15

def valid_methods(*method_names)
  @valid_methods = method_names unless method_names.empty?
  @valid_methods ||= []
end

.valid_types(*types) ⇒ Object



10
11
12
13
# File 'lib/super_model/error/type.rb', line 10

def valid_types(*types)
  @valid_types = types unless types.empty?
  @valid_types ||= []
end

Instance Method Details

#to_sObject



39
40
41
42
43
44
45
46
47
# File 'lib/super_model/error/type.rb', line 39

def to_s
  messages = []
  messages << self.class.valid_types.collect { |type| "be a #{type}" }
  messages << self.class.valid_methods.collect { |method| "respond to #{method}" }
  messages.flatten!
  messages.last = "or #{messages.last}"
  
  message = "must #{messages.join(', ')}"
end