Class: Tram::Policy::Errors

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#scopeArray<String> (readonly)

Returns the scope for error messages’ translation.

Returns:

  • (Array<String>)

    the scope for error messages’ translation



14
15
16
# File 'lib/tram/policy/errors.rb', line 14

def scope
  @scope
end

Instance Method Details

#add(message, tags) ⇒ self

Adds error message to the collection

Parameters:

  • message (#to_s)

    Either a message, or a symbolic key for translation

  • tags (Hash<Symbol, Object>)

    Tags to be attached to the message

Returns:

  • (self)

    the collection

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/tram/policy/errors.rb', line 23

def add(message, **tags)
  raise ArgumentError.new("Error message should be defined") unless message

  tap do
    tags = { scope: scope }.merge(tags) if message.is_a?(Symbol)
    @set << Tram::Policy::Error.new(message, **tags)
  end
end

#eachEnumerator<Tram::Policy::Error>

Iterates by collected errors

Returns:



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

Returns:

  • (Boolean)


60
61
62
# File 'lib/tram/policy/errors.rb', line 60

def empty?(&block)
  block ? !any?(&block) : !any?
end

#filter(key = nil, tags) ⇒ Tram::Policy::Errors

Filter errors by optional key and tags

Parameters:

  • key (#to_s) (defaults to: nil)

    The key to filter errors by

  • tags (Hash<Symbol, Object>)

    The list of tags to filter errors by

Returns:



48
49
50
51
52
53
# File 'lib/tram/policy/errors.rb', line 48

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(scope: scope, errors: list)
end

#itemsArray<Array>

The array of error items for translation

Returns:

  • (Array<Array>)


68
69
70
# File 'lib/tram/policy/errors.rb', line 68

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

Examples:

Add some tag to merged errors

policy.merge(other) { |err| err[:source] = "other" }

Parameters:

  • other (Tram::Policy::Errors)

    Collection to be merged

  • options (Hash<Symbol, Object>)

    Options to be added to merged errors

Yield Parameters:

  • hash (Hash<Symbol, Object>)

    of error options

Returns:

  • (self)


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tram/policy/errors.rb', line 92

def merge(other, **options)
  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(options)
  end

  self
end

#messagesArray<String>

The array of ordered error messages

Returns:

  • (Array<String>)


76
77
78
# File 'lib/tram/policy/errors.rb', line 76

def messages
  @set.map(&:message).sort
end