Class: Tram::Policy::Error
- Inherits:
-
Object
- Object
- Tram::Policy::Error
- Defined in:
- lib/tram/policy/error.rb
Overview
Validation error with message and assigned tags
Notice: an error is context-independent; it knows nothing about
a collection it is placed to; it can be safely moved
from one collection of [Tram::Policy::Errors] to another.
Instance Attribute Summary collapse
-
#message ⇒ String
readonly
The error message text.
Class Method Summary collapse
-
.new(value, opts) ⇒ Tram::Policy::Error
Builds an error.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares an error to another object using method [#to_h].
-
#[](tag) ⇒ Object
Fetches either message or a tag.
-
#fetch(tag, default, &block) ⇒ Object
Fetches either message or a tag.
-
#full_message ⇒ String
The full message (message and tags info).
-
#to_h ⇒ Hash<Symbol, Object>
Converts the error to a simple hash with message and tags.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
85 86 87 |
# File 'lib/tram/policy/error.rb', line 85 def method_missing(name, *args, &block) args.any? || block ? super : @tags[name] end |
Instance Attribute Details
#message ⇒ String (readonly)
Returns The error message text.
27 28 29 |
# File 'lib/tram/policy/error.rb', line 27 def @message end |
Class Method Details
.new(value, opts) ⇒ Tram::Policy::Error
Builds an error
If another error is send to the constructor, the error returned unchanged
18 19 20 21 |
# File 'lib/tram/policy/error.rb', line 18 def self.new(value, **opts) return value if value.is_a? self super end |
Instance Method Details
#==(other) ⇒ Boolean
Compares an error to another object using method [#to_h]
70 71 72 |
# File 'lib/tram/policy/error.rb', line 70 def ==(other) other.respond_to?(:to_h) && other.to_h == to_h end |
#[](tag) ⇒ Object
Fetches either message or a tag
50 51 52 |
# File 'lib/tram/policy/error.rb', line 50 def [](tag) to_h[tag.to_sym] end |
#fetch(tag, default, &block) ⇒ Object
Fetches either message or a tag
61 62 63 |
# File 'lib/tram/policy/error.rb', line 61 def fetch(tag, default, &block) to_h.fetch(tag.to_sym, default, &block) end |
#full_message ⇒ String
The full message (message and tags info)
33 34 35 |
# File 'lib/tram/policy/error.rb', line 33 def [, @tags].reject(&:empty?).join(" ") end |
#to_h ⇒ Hash<Symbol, Object>
Converts the error to a simple hash with message and tags
41 42 43 |
# File 'lib/tram/policy/error.rb', line 41 def to_h @tags.merge(message: ) end |