Exception: NRSER::AttrError

Inherits:
ValueError show all
Defined in:
lib/nrser/errors/attr_error.rb

Overview

Raised when we expected ‘#count` to be something it’s not.

Extends ValueError, and the #value must be the instance that

Direct Known Subclasses

CountError

Instance Attribute Summary collapse

Attributes inherited from ValueError

#subject

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, symbol:, subject:, **options) ⇒ AttrError

Returns a new instance of AttrError.

Parameters:

  • subject: (Object)

    The object that has the invalid attribute value.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nrser/errors/attr_error.rb', line 36

def initialize message = nil, symbol:, subject:, **options
  @symbol = symbol.to_sym
  
  @actual = if options.key?( :actual )
    options[:actual]
  else
    value.send @symbol
  end
  
  @has_expected = options.key? :expected
  @expected = options[:expected]
  
  super message, subject: subject
end

Instance Attribute Details

#actualObject (readonly)

Actual invalid value of the subject’s attribute.

If not provided at construction, will be retrieved by sending #symbol to ValueError#subject in #initialize.

Returns:



23
24
25
# File 'lib/nrser/errors/attr_error.rb', line 23

def actual
  @actual
end

#expectedObject (readonly)

An optional expected value to use in #build_message.

Returns:



30
31
32
# File 'lib/nrser/errors/attr_error.rb', line 30

def expected
  @expected
end

#symbolSymbol (readonly)

Name of attribute that has invalid value.

Returns:



13
14
15
# File 'lib/nrser/errors/attr_error.rb', line 13

def symbol
  @symbol
end

Instance Method Details

#build_messageObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nrser/errors/attr_error.rb', line 55

def build_message
  headline = if has_expected?
    "#{ subject.class } object has invalid ##{ symbol }: " +
      "expected #{ expected }, found #{ actual }"
  else
    "#{ subject.class } object has invalid ##{ symbol } (found #{ actual })"
  end
  
  binding.erb <<-END
    <%= headline  %>
    
    Subject:
    
        <%= subject.pretty_inspect %>
    
  END
end

#has_expected?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/nrser/errors/attr_error.rb', line 51

def has_expected?
  @has_expected
end