Exception: NoBrainer::Error::InvalidType

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/no_brainer/error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ InvalidType

Returns a new instance of InvalidType.



33
34
35
# File 'lib/no_brainer/error.rb', line 33

def initialize(options={})
  update(options)
end

Instance Attribute Details

#attr_nameObject

Returns the value of attribute attr_name.



32
33
34
# File 'lib/no_brainer/error.rb', line 32

def attr_name
  @attr_name
end

#errorObject

Returns the value of attribute error.



32
33
34
# File 'lib/no_brainer/error.rb', line 32

def error
  @error
end

#modelObject

Returns the value of attribute model.



32
33
34
# File 'lib/no_brainer/error.rb', line 32

def model
  @model
end

#typeObject

Returns the value of attribute type.



32
33
34
# File 'lib/no_brainer/error.rb', line 32

def type
  @type
end

#valueObject

Returns the value of attribute value.



32
33
34
# File 'lib/no_brainer/error.rb', line 32

def value
  @value
end

Instance Method Details

#human_type_nameObject



42
43
44
# File 'lib/no_brainer/error.rb', line 42

def human_type_name
  type.to_s.underscore.humanize.downcase
end

#messageObject



51
52
53
54
55
56
57
58
# File 'lib/no_brainer/error.rb', line 51

def message
  return super unless model && attr_name && error
  value = self.value
  mock = model.allocate
  mock.singleton_class.send(:define_method, :read_attribute_for_validation) { |_| value }
  mock.errors.add(attr_name, :invalid_type, **error)
  mock.errors.full_messages.first
end

#update(options = {}) ⇒ Object



37
38
39
40
# File 'lib/no_brainer/error.rb', line 37

def update(options={})
  options.assert_valid_keys(:model, :attr_name, :type, :value, :error)
  options.each { |k,v| instance_variable_set("@#{k}", v) }
end