Class: Tram::Policy::Errors
- Inherits:
-
Object
- Object
- Tram::Policy::Errors
- Includes:
- Enumerable
- Defined in:
- lib/tram/policy/errors.rb
Overview
Enumerable collection of unique unordered validation errors
Notice: A collection is context-dependent;
it knows about a scope of policy it belongs to,
and how to translate error in that scope.
Instance Attribute Summary collapse
-
#policy ⇒ Tram::Policy
readonly
The poplicy errors provided by.
Instance Method Summary collapse
-
#add(message = nil, **tags) ⇒ self
Adds error message to the collection.
-
#by_tags(**filter) ⇒ Hash<Symbol, Object>
Selects errors filtered by tags.
-
#each ⇒ Enumerator<Tram::Policy::Error>
Iterates by collected errors.
-
#empty?(&block) ⇒ Boolean
Checks whether a collection is empty.
-
#full_messages ⇒ Array<String>
The array of ordered error messages with error tags info.
-
#merge(other) ⇒ Object
Merges other collection to the current one and returns new collection with the current scope.
-
#messages ⇒ Array<String>
The array of ordered error messages.
Instance Attribute Details
#policy ⇒ Tram::Policy (readonly)
Returns the poplicy errors provided by.
15 16 17 |
# File 'lib/tram/policy/errors.rb', line 15 def policy @policy end |
Instance Method Details
#add(message = nil, **tags) ⇒ self
Adds error message to the collection
23 24 25 26 27 28 29 |
# File 'lib/tram/policy/errors.rb', line 23 def add( = nil, **) ||= .delete(:message) raise ArgumentError.new("Error message should be defined") unless @set << Tram::Policy::Error.new(@policy.t(, ), **) self end |
#by_tags(**filter) ⇒ Hash<Symbol, Object>
Selects errors filtered by tags
45 46 47 48 |
# File 'lib/tram/policy/errors.rb', line 45 def (**filter) filter = filter.to_a reject { |error| (filter - error.to_h.to_a).any? } end |
#each ⇒ Enumerator<Tram::Policy::Error>
Iterates by collected errors
36 37 38 |
# File 'lib/tram/policy/errors.rb', line 36 def each @set.each { |error| yield(error) } end |
#empty?(&block) ⇒ Boolean
Checks whether a collection is empty
54 55 56 |
# File 'lib/tram/policy/errors.rb', line 54 def empty?(&block) block ? !any?(&block) : !any? end |
#full_messages ⇒ Array<String>
The array of ordered error messages with error tags info
70 71 72 |
# File 'lib/tram/policy/errors.rb', line 70 def @set.map(&:full_message).sort end |
#merge(other) ⇒ Object
Merges other collection to the current one and returns new collection with the current scope
param [Tram::Policy::Errors] other Collection to be merged yieldparam [Hash<Symbol, Object>]
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tram/policy/errors.rb', line 83 def merge(other) return self unless other.is_a?(self.class) && other.any? if block_given? other.each { |err| add yield(err.to_h) } else @set |= other.to_a end self end |
#messages ⇒ Array<String>
The array of ordered error messages
62 63 64 |
# File 'lib/tram/policy/errors.rb', line 62 def @set.map(&:message).sort end |