Exception: NRSER::AttrError
- Inherits:
-
ValueError
- Object
- StandardError
- ValueError
- NRSER::AttrError
- 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
Instance Attribute Summary collapse
-
#actual ⇒ Object
readonly
Actual invalid value of the subject’s attribute.
-
#expected ⇒ Object
readonly
An optional expected value to use in #build_message.
-
#symbol ⇒ Symbol
readonly
Name of attribute that has invalid value.
Attributes inherited from ValueError
Instance Method Summary collapse
- #build_message ⇒ Object
- #has_expected? ⇒ Boolean
-
#initialize(message = nil, symbol:, subject:, **options) ⇒ AttrError
constructor
A new instance of AttrError.
Constructor Details
#initialize(message = nil, symbol:, subject:, **options) ⇒ AttrError
Returns a new instance of AttrError.
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 = nil, symbol:, subject:, ** @symbol = symbol.to_sym @actual = if .key?( :actual ) [:actual] else value.send @symbol end @has_expected = .key? :expected @expected = [:expected] super , subject: subject end |
Instance Attribute Details
#actual ⇒ Object (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.
23 24 25 |
# File 'lib/nrser/errors/attr_error.rb', line 23 def actual @actual end |
#expected ⇒ Object (readonly)
An optional expected value to use in #build_message.
30 31 32 |
# File 'lib/nrser/errors/attr_error.rb', line 30 def expected @expected end |
#symbol ⇒ Symbol (readonly)
Name of attribute that has invalid value.
13 14 15 |
# File 'lib/nrser/errors/attr_error.rb', line 13 def symbol @symbol end |
Instance Method Details
#build_message ⇒ Object
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 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
51 52 53 |
# File 'lib/nrser/errors/attr_error.rb', line 51 def has_expected? @has_expected end |