Class: ActiveModel::BetterErrors::ErrorCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_model/better_errors/error_collection.rb

Overview

ErrorCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ ErrorCollection

Returns a new instance of ErrorCollection.



12
13
14
15
# File 'lib/active_model/better_errors/error_collection.rb', line 12

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



11
12
13
# File 'lib/active_model/better_errors/error_collection.rb', line 11

def base
  @base
end

Instance Method Details

#[](attribute) ⇒ Object



38
39
40
# File 'lib/active_model/better_errors/error_collection.rb', line 38

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

#[]=(attribute, error) ⇒ Object



42
43
44
# File 'lib/active_model/better_errors/error_collection.rb', line 42

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

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



84
85
86
# File 'lib/active_model/better_errors/error_collection.rb', line 84

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

#added?(attribute, message = nil, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/active_model/better_errors/error_collection.rb', line 88

def added?(attribute, message = nil, options = {})
  message = ErrorMessage.build(base, attribute, message, options)
  self[attribute].include? message
end

#clearObject



17
18
19
# File 'lib/active_model/better_errors/error_collection.rb', line 17

def clear
  @collection.clear
end

#delete(attribute) ⇒ Object



34
35
36
# File 'lib/active_model/better_errors/error_collection.rb', line 34

def delete(attribute)
  @collection.delete attribute
end

#eachObject



46
47
48
49
50
51
52
# File 'lib/active_model/better_errors/error_collection.rb', line 46

def each
  @collection.each_key do |attribute|
    self[attribute].each do |error_message|
      yield attribute, error_message
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/active_model/better_errors/error_collection.rb', line 80

def empty?
  size == 0
end

#get(attribute) ⇒ Object



25
26
27
# File 'lib/active_model/better_errors/error_collection.rb', line 25

def get(attribute)
  @collection[attribute]
end

#include?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/active_model/better_errors/error_collection.rb', line 21

def include?(attribute)
  ( v = @collection[attribute]) && v.any?
end

#keysObject



63
64
65
# File 'lib/active_model/better_errors/error_collection.rb', line 63

def keys
  @collection.keys
end

#set(attribute, errors) ⇒ Object



29
30
31
32
# File 'lib/active_model/better_errors/error_collection.rb', line 29

def set(attribute, errors)
  return delete attribute if errors.nil?
  @collection[attribute] = ErrorMessageSet.new(base, attribute, errors)
end

#sizeObject Also known as: count



54
55
56
# File 'lib/active_model/better_errors/error_collection.rb', line 54

def size
  values.reduce(0) { |a, e| a + e.size }
end

#to_aObject



67
68
69
70
71
72
73
74
# File 'lib/active_model/better_errors/error_collection.rb', line 67

def to_a
  array = []
  @collection.each_key do |attribute|
    self[attribute].each { |error_message| array << error_message }
  end

  array
end

#to_hashObject



76
77
78
# File 'lib/active_model/better_errors/error_collection.rb', line 76

def to_hash
  @collection.dup
end

#valuesObject



59
60
61
# File 'lib/active_model/better_errors/error_collection.rb', line 59

def values
  @collection.values
end