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 messages in that scope.
Instance Attribute Summary collapse
-
#policy ⇒ Tram::Policy
readonly
The poplicy errors provided by.
Instance Method Summary collapse
-
#add(message, 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? ⇒ Boolean
Checks whether a collection is empty.
-
#full_messages ⇒ Array<String>
The array of ordered error messages with error tags info.
-
#merge(other, options) {|hash| ... } ⇒ self
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, tags) ⇒ self
Adds error message to the collection
24 25 26 27 28 29 30 |
# File 'lib/tram/policy/errors.rb', line 24 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
47 48 49 50 |
# File 'lib/tram/policy/errors.rb', line 47 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
37 38 39 |
# File 'lib/tram/policy/errors.rb', line 37 def each @set.each { |error| yield(error) } end |
#empty? ⇒ Boolean
Checks whether a collection is empty
57 58 59 |
# File 'lib/tram/policy/errors.rb', line 57 def empty?(&block) block ? !any?(&block) : !any? end |
#full_messages ⇒ Array<String>
The array of ordered error messages with error tags info
73 74 75 |
# File 'lib/tram/policy/errors.rb', line 73 def @set.map(&:full_message).sort end |
#merge(other, options) {|hash| ... } ⇒ self
Merges other collection to the current one and returns new collection with the current scope
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tram/policy/errors.rb', line 89 def merge(other, **) return self unless other.is_a?(self.class) other.each do |err| new_err = block_given? ? yield(err.to_h) : err.to_h add new_err.merge() end self end |
#messages ⇒ Array<String>
The array of ordered error messages
65 66 67 |
# File 'lib/tram/policy/errors.rb', line 65 def @set.map(&:message).sort end |