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
29
30
# File 'lib/tram/policy/errors.rb', line 24

def add(message = nil, **tags)
  message ||= tags.delete(:message)
  raise ArgumentError.new("Error message should be defined") unless message

  @set << Tram::Policy::Error.new(@policy.t(message, tags), **tags)
  self
end

#by_tags(filter) ⇒ Hash<Symbol, Object>

Selects errors filtered by tags

Parameters:

  • filter (Hash<Symbol, Object>)

    List of options to filter by

Returns:

  • (Hash<Symbol, Object>)


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

def by_tags(**filter)
  filter = filter.to_a
  reject { |error| (filter - error.to_h.to_a).any? }
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)


57
58
59
# File 'lib/tram/policy/errors.rb', line 57

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

#full_messagesArray<String>

The array of ordered error messages with error tags info

Returns:

  • (Array<String>)


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

def full_messages
  @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

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)


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

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

  self
end

#messagesArray<String>

The array of ordered error messages

Returns:

  • (Array<String>)


65
66
67
# File 'lib/tram/policy/errors.rb', line 65

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