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 ValueError#value must be the instance that

Direct Known Subclasses

CountError

Constant Summary

Constants included from NicerError

NicerError::DEFAULT_COLUMN_WIDTH

Instance Method Summary collapse

Methods inherited from ValueError

#value, #value?

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

Note:

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.

Parameters:

Options Hash (**kwds):

  • :name (Symbol | String)

    The name of the attribute in question.

  • :value (Object)

    The value that has the bad attribute.

  • :expected (Object | NRSER::Types::Type | String)

    Encouraged to be one of:

    1. The Object you wanted the attribute to respond with.

    2. A Types::Type satisfied by what you would have been satisfied with.

    3. A String explanation of the condition.

  • :actual (Object)

    The actual attribute value.



# File 'lib/nrser/errors/attr_error.rb', line 50

Instance Method Details

#actualnil, Object

Get an optional actual value for the attribute, from ‘context` if it exists or by sending #name to ValueError#value if it works.

Returns:



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.

Returns:

  • (Boolean)


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_messageString

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.

Returns:



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 default_message
  message = []

  if value? && name?
    message << format_message(  value.class, "object", value.inspect,
                                "has invalid ##{ name } attribute" )
  end

  if expected?
    message << format_message( "expected", expected )
  end

  if actual?
    message << format_message( "found", actual )
  end

  if message.empty?
    super
  else
    message.join ', '
  end
end

#expectedObject

Optional information about what the attribute value was expected to be, which can be provided via the ‘:expected` key in NicerError#context.

Returns:



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?

Returns:

  • (Boolean)


47
# File 'lib/nrser/errors/attr_error.rb', line 47

def_context_delegator keys: :expected

#nameSymbol | String

Name of attribute that has invalid value, which can be provided via the ‘:name` key in the NicerError#context.

Returns:



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?

Returns:

  • (Boolean)


33
# File 'lib/nrser/errors/attr_error.rb', line 33

def_context_delegator keys: :name