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.
-
#each ⇒ Enumerator<Tram::Policy::Error>
Iterates by collected errors.
-
#empty? ⇒ Boolean
Checks whether a collection is empty.
-
#filter(key = nil, tags) ⇒ Tram::Policy::Errors
Filter errors by optional key and tags.
-
#items ⇒ Array<Array>
The array of error items for translation.
-
#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 |
# File 'lib/tram/policy/errors.rb', line 24 def add(, **) = .merge(scope: policy.scope) unless .key?(:scope) raise ArgumentError.new("Error message should be defined") unless tap { @set << Tram::Policy::Error.new(, **) } end |
#each ⇒ Enumerator<Tram::Policy::Error>
Iterates by collected errors
35 36 37 |
# File 'lib/tram/policy/errors.rb', line 35 def each @set.each { |error| yield(error) } end |
#empty? ⇒ Boolean
Checks whether a collection is empty
58 59 60 |
# File 'lib/tram/policy/errors.rb', line 58 def empty?(&block) block ? !any?(&block) : !any? end |
#filter(key = nil, tags) ⇒ Tram::Policy::Errors
Filter errors by optional key and tags
46 47 48 49 50 51 |
# File 'lib/tram/policy/errors.rb', line 46 def filter(key = nil, **) list = each_with_object(Set.new) do |error, obj| obj << error if error.contain?(key, ) end self.class.new(policy, list) end |
#items ⇒ Array<Array>
The array of error items for translation
66 67 68 |
# File 'lib/tram/policy/errors.rb', line 66 def items @set.map(&:item) end |
#merge(other, options) {|hash| ... } ⇒ self
Merges other collection to the current one and returns new collection with the current scope
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/tram/policy/errors.rb', line 90 def merge(other, **) return self unless other.is_a?(self.class) other.each do |err| key, opts = err.item opts = yield(opts) if block_given? add key, opts.merge() end self end |
#messages ⇒ Array<String>
The array of ordered error messages
74 75 76 |
# File 'lib/tram/policy/errors.rb', line 74 def @set.map(&:message).sort end |