Class: ActiveSms::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/activesms/validations.rb

Overview

Active SMS validation is reported to and from this object, which is used by Base to determine whether the message is in a valid state to be sent.

Constant Summary collapse

@@default_error_messages =

Holds a hash with all the default error message. This can be replaced by your own copy or localizations.

{
  :invalid => "is invalid"
}

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

:nodoc:



24
25
26
27
# File 'lib/activesms/validations.rb', line 24

def initialize(base) #:nodoc:
  @base = base
  @errors = {}
end

Instance Method Details

#add(attribute, msg = @@default_error_messages[:invalid]) ⇒ Object

Adds an error message msg to the attribute, which will be returned on a call to on(attribute) for the same attribute and ensure that this error object returns false when asked if empty?. More than one error can be added to the same attribute, in which case an array will be returned on a call to on(attribute). If no msg is supplied, “invalid” is assumed.



44
45
46
47
# File 'lib/activesms/validations.rb', line 44

def add(attribute, msg = @@default_error_messages[:invalid])
  @errors[attribute.to_s] ||= []
  @errors[attribute.to_s] << msg
end

#add_to_base(msg) ⇒ Object

Adds an error message to the base object instead of any particular attribute for the object. This is used to report errors that don’t tie to any specific attribute, but to the object as a whole. These error messages don’t get prepended with any field name when iterating with each_full, so they should be complete sentences.



34
35
36
# File 'lib/activesms/validations.rb', line 34

def add_to_base(msg)
  add(:base, msg)
end