Class: Glue::Validation::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/more/facets/validation.rb

Overview

Encapsulates a list of validation errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = {}) ⇒ Errors

Returns a new instance of Errors.



85
86
87
# File 'lib/more/facets/validation.rb', line 85

def initialize(errors = {})
  @errors = errors
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



73
74
75
# File 'lib/more/facets/validation.rb', line 73

def errors
  @errors
end

Instance Method Details

#add(attr, message) ⇒ Object



89
90
91
# File 'lib/more/facets/validation.rb', line 89

def add(attr, message)
  (@errors[attr] ||= []) << message
end

#clearObject



115
116
117
# File 'lib/more/facets/validation.rb', line 115

def clear
  @errors.clear
end

#eachObject

Yields each attribute and associated message.



100
101
102
103
104
# File 'lib/more/facets/validation.rb', line 100

def each
  @errors.each_key do |attr| 
    @errors[attr].each { |msg| yield attr, msg }
  end
end

#empty?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/more/facets/validation.rb', line 111

def empty?
  @errors.empty?
end

#join(glue) ⇒ Object



123
124
125
# File 'lib/more/facets/validation.rb', line 123

def join(glue)
  @errors.to_a.join(glue)
end

#on(attr) ⇒ Object Also known as: []



93
94
95
# File 'lib/more/facets/validation.rb', line 93

def on(attr)
  @errors[attr]
end

#sizeObject Also known as: count



106
107
108
# File 'lib/more/facets/validation.rb', line 106

def size
  @errors.size
end

#to_aObject



119
120
121
# File 'lib/more/facets/validation.rb', line 119

def to_a
  @errors.inject([]) { |a, kv| a << kv }
end