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 ValueError#value must be the instance that
Direct Known Subclasses
Constant Summary
Constants included from NicerError
NicerError::DEFAULT_COLUMN_WIDTH
Instance Method Summary collapse
-
#actual ⇒ nil, Object
Get an optional actual value for the attribute, from ‘context` if it exists or by sending #name to ValueError#value if it works.
-
#actual? ⇒ Boolean
Tests if an ‘actual’ value was provided in the NicerError#context.
-
#default_message ⇒ String
Create a default message if none was provided.
-
#expected ⇒ Object
Optional information about what the attribute value was expected to be, which can be provided via the ‘:expected` key in NicerError#context.
-
#expected? ⇒ Boolean
Is there an ‘:expected` key in NicerError#context?.
-
#initialize(*message, **kwds) ⇒ Object
constructor
Create a new AttrError.
-
#name ⇒ Symbol | String
Name of attribute that has invalid value, which can be provided via the ‘:name` key in the NicerError#context.
-
#name? ⇒ Boolean
Is there an ‘:name` key in NicerError#context?.
Methods inherited from ValueError
Methods included from NicerError
#add_extended_message?, column_width, #context, #context_section, #details, #details_section, #extended_message, #format_message, #format_message_segment, included, #to_s
Constructor Details
#initialize(*message, **kwds) ⇒ Object
If you provide the ‘:name` and `:value` keyword arguments, but not `:actual` then #actual will attempt to retrieve the attribute’s value by
value.public_send name
This really *shouldn’t* be problematic - if attempting to access a public attribute can cause serious side-effects, you may want to re-think your design. However, I still felt like I should note it here.
The call is wrapped in a ‘rescue StandardError`, so you **don’t** need to worry about anything mundane like an error being raised.
Create a new NRSER::AttrError.
This method does nothing but call ‘super`. It’s here only for doc’s sake.
|
# File 'lib/nrser/errors/attr_error.rb', line 50
|
Instance Method Details
#actual ⇒ nil, Object
Get an optional actual value for the attribute, from ‘context` if it exists or by sending #name to ValueError#value if it works.
119 120 121 122 123 124 125 126 127 |
# File 'lib/nrser/errors/attr_error.rb', line 119 def actual if context.key? :actual context[ :actual ] elsif value? && name? value.public_send name end rescue StandardError => error nil end |
#actual? ⇒ Boolean
Tests if an ‘actual’ value was provided in the NicerError#context.
101 102 103 104 105 |
# File 'lib/nrser/errors/attr_error.rb', line 101 def actual? context.key?( :actual ) || ( value? && name? && value.respond_to?( name ) ) rescue StandardError => error false end |
#default_message ⇒ String
Create a default message if none was provided.
Uses whatever recognized NicerError#context values are present, falling back to NicerError#default_message if none are.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/nrser/errors/attr_error.rb', line 137 def = [] if value? && name? << ( value.class, "object", value.inspect, "has invalid ##{ name } attribute" ) end if expected? << ( "expected", expected ) end if actual? << ( "found", actual ) end if .empty? super else .join ', ' end end |
#expected ⇒ Object
Optional information about what the attribute value was expected to be, which can be provided via the ‘:expected` key in NicerError#context.
47 |
# File 'lib/nrser/errors/attr_error.rb', line 47 def_context_delegator keys: :expected |
#expected? ⇒ Boolean
Is there an ‘:expected` key in NicerError#context?
47 |
# File 'lib/nrser/errors/attr_error.rb', line 47 def_context_delegator keys: :expected |
#name ⇒ Symbol | String
Name of attribute that has invalid value, which can be provided via the ‘:name` key in the NicerError#context.
33 |
# File 'lib/nrser/errors/attr_error.rb', line 33 def_context_delegator keys: :name |
#name? ⇒ Boolean
Is there an ‘:name` key in NicerError#context?
33 |
# File 'lib/nrser/errors/attr_error.rb', line 33 def_context_delegator keys: :name |