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

#policyTram::Policy (readonly)

Returns the poplicy errors provided by.

Returns:



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

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)


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

def add(message, **tags)
  tags = tags.merge(scope: policy.scope) unless tags.key?(:scope)
  raise ArgumentError.new("Error message should be defined") unless message
  tap { @set << Tram::Policy::Error.new(message, **tags) }
end

#eachEnumerator<Tram::Policy::Error>

Iterates by collected errors

Returns:



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

Returns:

  • (Boolean)


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

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:



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

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

#itemsArray<Array>

The array of error items for translation

Returns:

  • (Array<Array>)


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

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)


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

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>)


74
75
76
# File 'lib/tram/policy/errors.rb', line 74

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