Class: Ward::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ward/errors.rb

Overview

Holds errors associated with a valid? call.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrors

Creates a new Errors instance.



153
154
155
# File 'lib/ward/errors.rb', line 153

def initialize
  @errors = {}
end

Class Method Details

.error_for(matcher, negative, key = nil) ⇒ String

Returns the unformatted error message for a matcher.

Parameters:

  • matcher (Ward::Matchers::Matcher)

    The matcher.

  • negative (Boolean)

    Whether to return a negative message, rather than a positive.

  • key (nil, Symbol, String) (defaults to: nil)

    If a string is supplied, error_for will assume that the string should be used as the error. A symbol will be assumed to be a ‘key’ from the language file, while nil will result in the validator using the default error message for the matcher.

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ward/errors.rb', line 49

def error_for(matcher, negative, key = nil)
  return key if key.is_a?(String)

  language_key = if key.nil?
    "#{matcher.class.error_id}."
  else
    "#{matcher.class.error_id}.#{key}."
  end

  language_key << (negative ? 'negative' : 'positive')

  message(language_key) || '%{context} is invalid'
end

.format_exclusive_list(list) ⇒ String

Receives an array and formats it nicely, assuming that only one value is expected.

Examples:

One member

format_exclusive_list([1]) # => '1'

Two members

format_exclusive_list([1, 2]) # => '1 or 2'

Many members

format_exclusive_list([1, 2, 3]) # => '1, 2, or 3'

Parameters:

  • list (Enumerable)

    The list to be formatted.

Returns:

  • (String)


80
81
82
# File 'lib/ward/errors.rb', line 80

def format_exclusive_list(list)
  format_list(list, message('generic.exclusive_conjunction'))
end

.format_inclusive_list(list) ⇒ String

Receives an array and formats it nicely, assuming that all values are expected.

Examples:

One member

format_inclusive_list([1]) # => '1'

Two members

format_inclusive_list([1, 2]) # => '1 and 2'

Many members

format_inclusive_list([1, 2, 3]) # => '1, 2, and 3'

Parameters:

  • list (Enumerable)

    The list to be formatted.

Returns:

  • (String)


101
102
103
# File 'lib/ward/errors.rb', line 101

def format_inclusive_list(list)
  format_list(list, message('generic.inclusive_conjunction'))
end

.message(*keys) ⇒ String?

Returns a localisation message.

Examples:


Ward::Errors.message('has.eql.positive')
# => '%{context} should have %{expected} %{collection}'

Passing multiple keys as fallbacks.


Ward::Errors.message(
  'does.not.exist', 'has.eql.negative', 'has.eql.positive')

# => '%{context} should not have %{expected} %{collection}'

Parameters:

  • String (keys)

    The key of the message to be returned from the current language file.

Returns:

  • (String, nil)

    Returns the message or nil if it couldn’t be found.



31
32
33
# File 'lib/ward/errors.rb', line 31

def message(*keys)
  messages[ keys.detect { |key| messages.has_key?(key) } ]
end

Instance Method Details

#add(attribute, message) ⇒ String

TODO:

Support symbols for i18n.

Adds an error message to the instance.

Parameters:

Returns:

  • (String)

    Returns the error message which was set.



170
171
172
173
174
175
176
177
178
# File 'lib/ward/errors.rb', line 170

def add(attribute, message)
  if attribute.kind_of?(Context) or attribute.kind_of?(ContextChain)
    attribute = attribute.attribute
  end

  @errors[attribute] ||= []
  @errors[attribute] << message
  message
end

#each {|attribute, messages| ... } ⇒ Object

Iterates through each attribute and the errors.

Yield Parameters:

  • attribute (Symbol)

    The attribute name.

  • messages (Array, nil)

    An array with each error message for the attribute, or nil if the attribute has no errors.



200
201
202
# File 'lib/ward/errors.rb', line 200

def each(&block)
  @errors.each(&block)
end

#empty?Boolean

Returns if there are no errors contained.

Returns:

  • (Boolean)


208
209
210
# File 'lib/ward/errors.rb', line 208

def empty?
  @errors.empty?
end

#on(attribute) ⇒ Array

Returns an array of the errors present on an an attribute.

Parameters:

  • attribute (Symbol)

    The attribute whose errors you wish to retrieve.

Returns:

  • (Array)

    Returns the error messages for an attribute, or nil if there are none.



188
189
190
# File 'lib/ward/errors.rb', line 188

def on(attribute)
  @errors[attribute]
end