Class: Errors
- Inherits:
-
Object
- Object
- Errors
- Defined in:
- lib/yodel/models/core/validations/errors.rb
Instance Method Summary collapse
-
#<<(error) ⇒ Object
errors on a collection.
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
- #inspect ⇒ Object
- #key?(name) ⇒ Boolean
- #summarise ⇒ Object
- #to_json(*a) ⇒ Object
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
2 3 4 |
# File 'lib/yodel/models/core/validations/errors.rb', line 2 def initialize @errors = {} end |
Instance Method Details
#<<(error) ⇒ Object
errors on a collection
34 35 36 |
# File 'lib/yodel/models/core/validations/errors.rb', line 34 def <<(error) self['_'] << error end |
#[](name) ⇒ Object
10 11 12 13 |
# File 'lib/yodel/models/core/validations/errors.rb', line 10 def [](name) @errors[name] ||= [] @errors[name] end |
#[]=(name, value) ⇒ Object
15 16 17 18 |
# File 'lib/yodel/models/core/validations/errors.rb', line 15 def []=(name, value) return if value.nil? @errors[name] = value end |
#clear ⇒ Object
28 29 30 31 |
# File 'lib/yodel/models/core/validations/errors.rb', line 28 def clear @errors.clear @summary = nil end |
#empty? ⇒ Boolean
24 25 26 |
# File 'lib/yodel/models/core/validations/errors.rb', line 24 def empty? @errors.empty? end |
#inspect ⇒ Object
6 7 8 |
# File 'lib/yodel/models/core/validations/errors.rb', line 6 def inspect @errors.inspect end |
#key?(name) ⇒ Boolean
20 21 22 |
# File 'lib/yodel/models/core/validations/errors.rb', line 20 def key?(name) @errors.key?(name) end |
#summarise ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/yodel/models/core/validations/errors.rb', line 38 def summarise @summary ||= @errors.each_with_object({}) do |(field, errors), hash| if errors.respond_to?(:summarise) hash[field.to_s] = errors.summarise.values.to_sentence else hash[field.to_s] = "#{field.to_s.humanize} #{errors.collect(&:describe).to_sentence}" end end end |
#to_json(*a) ⇒ Object
48 49 50 |
# File 'lib/yodel/models/core/validations/errors.rb', line 48 def to_json(*a) summarise.to_json(*a) end |