lib/tram/policy/errors.rb in tram-policy-0.3.1 vs lib/tram/policy/errors.rb in tram-policy-0.4.0
- old
+ new
@@ -19,16 +19,14 @@
#
# @param [#to_s] message Either a message, or a symbolic key for translation
# @param [Hash<Symbol, Object>] tags Tags to be attached to the message
# @return [self] the collection
#
- def add(message = nil, **tags)
- message ||= tags.delete(:message)
+ def add(message, **tags)
+ tags = tags.merge(scope: policy.scope) unless tags.key?(:scope)
raise ArgumentError.new("Error message should be defined") unless message
-
- @set << Tram::Policy::Error.new(@policy.t(message, tags), **tags)
- self
+ tap { @set << Tram::Policy::Error.new(message, **tags) }
end
# Iterates by collected errors
#
# @yeldparam [Tram::Policy::Error]
@@ -36,44 +34,73 @@
#
def each
@set.each { |error| yield(error) }
end
- # @!method by_tags(filter)
- # Selects errors filtered by tags
+ # @!method filter(key = nil, tags)
+ # Filter errors by optional key and tags
#
- # @param [Hash<Symbol, Object>] filter List of options to filter by
- # @return [Hash<Symbol, Object>]
+ # @param [#to_s] key The key to filter errors by
+ # @param [Hash<Symbol, Object>] tags The list of tags to filter errors by
+ # @return [Tram::Policy::Errors]
#
- def by_tags(**filter)
- filter = filter.to_a
- reject { |error| (filter - error.to_h.to_a).any? }
+ def filter(key = nil, **tags)
+ list = each_with_object(Set.new) do |error, obj|
+ obj << error if error.contain?(key, tags)
+ end
+ self.class.new(policy, list)
end
+ # @deprecated
+ # @!method by_tags(tags)
+ # Selects errors filtered by key and tags
+ #
+ # @param [Hash<Symbol, Object>] tags List of options to filter by
+ # @return [Array<Tram::Policy::Error>]
+ #
+ def by_tags(**tags)
+ warn "[DEPRECATED] The method Tram::Policy::Errors#by_tags" \
+ " will be removed in the v1.0.0. Use method #filter instead."
+
+ filter(tags).to_a
+ end
+
# @!method empty?
# Checks whether a collection is empty
#
# @return [Boolean]
#
def empty?(&block)
block ? !any?(&block) : !any?
end
+ # The array of error items for translation
+ #
+ # @return [Array<Array>]
+ #
+ def items
+ @set.map(&:item)
+ end
+
# The array of ordered error messages
#
# @return [Array<String>]
#
def messages
@set.map(&:message).sort
end
- # The array of ordered error messages with error tags info
+ # @deprecated
+ # List of error descriptions
#
# @return [Array<String>]
#
def full_messages
- @set.map(&:full_message).sort
+ warn "[DEPRECATED] The method Tram::Policy::Errors#full_messages" \
+ " will be removed in the v1.0.0."
+
+ map(&:full_message)
end
# @!method merge(other, options)
# Merges other collection to the current one and returns new collection
# with the current scope
@@ -88,20 +115,21 @@
#
def merge(other, **options)
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(options)
+ key, opts = err.item
+ opts = yield(opts) if block_given?
+ add key, opts.merge(options)
end
self
end
private
- def initialize(policy, errors = Set.new)
+ def initialize(policy, errors = [])
@policy = policy
- @set = errors
+ @set = Set.new(errors)
end
end
end