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
-
#key ⇒ Symbol, String
readonly
Error key.
-
#tags ⇒ Hash<Symbol, Object>
readonly
Error tags.
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 [#item].
-
#[](tag) ⇒ Object
Fetches an option.
-
#contain?(some_key = nil, some_tags = {}) ⇒ Boolean
Checks whether the error contain given key and tags.
-
#fetch(tag, default = Dry::Initializer::UNDEFINED, &block) ⇒ Object
Fetches the tag.
-
#item ⇒ Array
(also: #to_a)
The list of arguments for [I18n.t].
-
#message ⇒ String
The text of error message translated to the current locale.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
104 105 106 |
# File 'lib/tram/policy/error.rb', line 104 def method_missing(name, *args, &block) args.any? || block ? super : [name] end |
Instance Attribute Details
#key ⇒ Symbol, String (readonly)
Returns error key.
24 25 26 |
# File 'lib/tram/policy/error.rb', line 24 def key @key end |
#tags ⇒ Hash<Symbol, Object> (readonly)
Returns error tags.
28 29 30 |
# File 'lib/tram/policy/error.rb', line 28 def @tags 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 |
# File 'lib/tram/policy/error.rb', line 18 def self.new(value, **opts) value.instance_of?(self) ? value : super end |
Instance Method Details
#==(other) ⇒ Boolean
Compares an error to another object using method [#item]
76 77 78 |
# File 'lib/tram/policy/error.rb', line 76 def ==(other) other.respond_to?(:to_a) && other.to_a == item end |
#[](tag) ⇒ Object
Fetches an option
52 53 54 |
# File 'lib/tram/policy/error.rb', line 52 def [](tag) [tag.to_sym] end |
#contain?(some_key = nil, some_tags = {}) ⇒ Boolean
Checks whether the error contain given key and tags
87 88 89 90 91 |
# File 'lib/tram/policy/error.rb', line 87 def contain?(some_key = nil, **) return false if some_key&.!= key .each { |k, v| return false unless [k] == v } true end |
#fetch(tag, default = Dry::Initializer::UNDEFINED, &block) ⇒ Object
Fetches the tag
63 64 65 66 67 68 69 |
# File 'lib/tram/policy/error.rb', line 63 def fetch(tag, default = Dry::Initializer::UNDEFINED, &block) if default == Dry::Initializer::UNDEFINED .fetch(tag.to_sym, &block) else .fetch(tag.to_sym, default, &block) end end |
#item ⇒ Array Also known as: to_a
The list of arguments for [I18n.t]
34 35 36 |
# File 'lib/tram/policy/error.rb', line 34 def item [key, ] end |
#message ⇒ String
The text of error message translated to the current locale
43 44 45 |
# File 'lib/tram/policy/error.rb', line 43 def key.is_a?(Symbol) ? I18n.t(*item) : key.to_s end |