Class: Poncho::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/poncho/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

Returns a new instance of Errors.



5
6
7
8
# File 'lib/poncho/errors.rb', line 5

def initialize(base)
  @base     = base
  @messages = {}
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



3
4
5
# File 'lib/poncho/errors.rb', line 3

def messages
  @messages
end

Instance Method Details

#[](attribute) ⇒ Object

When passed a symbol or a name of a method, returns an array of errors for the method.

p.errors[:name]   # => ["can not be nil"]
p.errors['name']  # => ["can not be nil"]


41
42
43
# File 'lib/poncho/errors.rb', line 41

def [](attribute)
  get(attribute.to_sym) || set(attribute.to_sym, [])
end

#[]=(attribute, error) ⇒ Object

Adds to the supplied attribute the supplied error message.

p.errors[:name] = "must be set"
p.errors[:name] # => ['must be set']


49
50
51
# File 'lib/poncho/errors.rb', line 49

def []=(attribute, error)
  self[attribute] << error
end

#add(attribute, message = nil, options = {}) ⇒ Object



129
130
131
# File 'lib/poncho/errors.rb', line 129

def add(attribute, message = nil, options = {})
  self[attribute] << message
end

#as_json(options = nil) ⇒ Object

Return the first error we get



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/poncho/errors.rb', line 107

def as_json(options=nil)
  return {} if messages.empty?
  attribute, types = messages.first
  type             = types.first

  {
    :error => {
      :param => attribute,
      :type  => type,
      :message => nil
    }
  }
end

#clearObject

Clear the messages



11
12
13
# File 'lib/poncho/errors.rb', line 11

def clear
  messages.clear
end

#countObject

Returns the number of error messages.

p.errors.add(:name, "can't be blank")
p.errors.count # => 1
p.errors.add(:name, "must be specified")
p.errors.count # => 2


95
96
97
# File 'lib/poncho/errors.rb', line 95

def count
  to_a.size
end

#delete(key) ⇒ Object

Delete messages for key



32
33
34
# File 'lib/poncho/errors.rb', line 32

def delete(key)
  messages.delete(key)
end

#eachObject



53
54
55
# File 'lib/poncho/errors.rb', line 53

def each
  [to_s]
end

#empty?Boolean Also known as: blank?

Returns true if no errors are found, false otherwise. If the error message is a string it can be empty.

Returns:

  • (Boolean)


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

def empty?
  messages.all? { |k, v| v && v == "" && !v.is_a?(String) }
end

#full_message(attribute, message) ⇒ Object



137
138
139
140
# File 'lib/poncho/errors.rb', line 137

def full_message(attribute, message)
  return message if attribute == :base
  "#{attribute} #{message.join(', ')}"
end

#full_messagesObject



133
134
135
# File 'lib/poncho/errors.rb', line 133

def full_messages
  messages.map { |attribute, message| full_message(attribute, message) }
end

#get(key) ⇒ Object

Get messages for key



22
23
24
# File 'lib/poncho/errors.rb', line 22

def get(key)
  messages[key]
end

#include?(error) ⇒ Boolean Also known as: has_key?

Do the error messages include an error with key error?

Returns:

  • (Boolean)


16
17
18
# File 'lib/poncho/errors.rb', line 16

def include?(error)
  (v = messages[error]) && v.any?
end

#keysObject

Returns all message keys



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

def keys
  messages.keys
end

#set(key, value) ⇒ Object

Set messages for key to value



27
28
29
# File 'lib/poncho/errors.rb', line 27

def set(key, value)
  messages[key] = value
end

#sizeObject

Returns the number of error messages.

p.errors.add(:name, "can't be blank")
p.errors.size # => 1
p.errors.add(:name, "must be specified")
p.errors.size # => 2


63
64
65
# File 'lib/poncho/errors.rb', line 63

def size
  values.flatten.size
end

#to_aObject

Returns an array of error messages, with the attribute name included

p.errors.add(:name, "can't be blank")
p.errors.add(:name, "must be specified")
p.errors.to_a # => ["name can't be blank", "name must be specified"]


86
87
88
# File 'lib/poncho/errors.rb', line 86

def to_a
  full_messages
end

#to_hashObject



125
126
127
# File 'lib/poncho/errors.rb', line 125

def to_hash
  messages.dup
end

#to_jsonObject



121
122
123
# File 'lib/poncho/errors.rb', line 121

def to_json(*)
  as_json.to_json
end

#to_sObject



77
78
79
# File 'lib/poncho/errors.rb', line 77

def to_s
  "Validation errors:\n " + full_messages.join(', ')
end

#valuesObject

Returns all message values



68
69
70
# File 'lib/poncho/errors.rb', line 68

def values
  messages.values
end