Class: AWS::Record::Errors

Inherits:
Core::IndifferentHash show all
Includes:
Enumerable
Defined in:
lib/aws/record/errors.rb

Instance Method Summary collapse

Methods inherited from Core::IndifferentHash

#_getter, #_setter, #delete, #fetch, #has_key?, #initialize, #merge, #merge!

Constructor Details

This class inherits a constructor from AWS::Core::IndifferentHash

Instance Method Details

#[](attribute_name) ⇒ Array<String> Also known as: on

Returns the errors for the atttibute in an array.

errors.add(:name, 'may not be blank')
errors.add(:name, 'must be less than 30 characters')
errors[:name]
#=> ['may not be blank', 'must be less than 30 characters']

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute to retnr errors for. You can pass the string or symbol version.

Returns:

  • (Array<String>)

    Returns the error messages for the given attribute_name. If there are no errors on the attribute then an empty array is returned.



33
34
35
# File 'lib/aws/record/errors.rb', line 33

def [] attribute_name
  super(attribute_name) || []
end

#[]=(attribute_name, message = 'is invalid') ⇒ String Also known as: add

Adds an error message to the named attribute.

errors.add(:name, 'may not be blank')
errors.on(:name)
#=> ['may not be blank']

If you want to add a general error message, then pass :base for attribute_name, or call #add_to_base.

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute that you are adding an error to.

  • message (String) (defaults to: 'is invalid')

    (‘is invalid’) The error message (should not contain the attribute name).

Returns:

  • (String)

    Returns the message.



51
52
53
54
55
56
57
58
# File 'lib/aws/record/errors.rb', line 51

def []= attribute_name, message = 'is invalid'
  if has_key?(attribute_name)
    self[attribute_name] << message
  else
    super(attribute_name, [message])
  end
  self[attribute_name]
end

#add_to_base(message) ⇒ String

Adds a general error message (not associated with any particular attribute).

Parameters:

  • message (String)

    (‘is invalid’) The error message (should not contain the attribute name).

Returns:

  • (String)

    Returns the message.



66
67
68
# File 'lib/aws/record/errors.rb', line 66

def add_to_base message
  add(:base, message)
end

#clear!nil

Removes all error messages.

Returns:

  • (nil)


133
134
135
136
137
138
# File 'lib/aws/record/errors.rb', line 133

def clear!
  keys.each do |key|
    delete(key)
  end
  nil
end

#countInteger Also known as: size

Returns the number of error messages.

Returns:

  • (Integer)

    Returns the number of error messages.



71
72
73
# File 'lib/aws/record/errors.rb', line 71

def count
  values.flatten.length
end

#each {|attribute_name, error_message| ... } ⇒ Object

Yields once for each error message added.

An attribute_name may yield more than once if there are more than one errors associated with that attirbute.

Yields:

  • (attribute_name, error_message)

Yield Parameters:

  • attribute_name (String)

    The name of the attribute

  • error_message (String)

    The error message associated the the named attribute.



85
86
87
88
89
90
91
# File 'lib/aws/record/errors.rb', line 85

def each &block
  super do |attribute_name, error_messages|
    error_messages.each do |error_message|
      yield(attribute_name, error_message)
    end
  end
end

#full_messagesArray of Strings Also known as: to_a

Returns the errors prefixed by a humanized version of the attribute name.

errors.add(:name, 'may not be blank')
errors.full_messages 
#=> ['Name may not be blank']

Returns:

  • (Array of Strings)

    Returns an array of error messages.



101
102
103
104
105
106
107
108
109
110
# File 'lib/aws/record/errors.rb', line 101

def full_messages
  messages = []
  each do |attr_name, error_message|
    messages << case attr_name
    when 'base' then error_message.dup
    else "#{attr_name.capitalize.gsub(/_/, ' ')} #{error_message}"
    end
  end
  messages
end

#to_hashObject

Returns a hash of of errors messages. Keys are attribute names and values are arrays of error messages.

errors.add(:name, 'may not be blank')
errors.to_hash
#=> { 'name' => ['may not be blank'] }

Please note that the hash values are always arrays, even if there is only one error message for the attribute.



122
123
124
125
126
127
128
129
# File 'lib/aws/record/errors.rb', line 122

def to_hash
  hash = {}
  each do |attr_name, message|
    hash[attr_name] ||= []
    hash[attr_name] << message.dup
  end
  hash
end