Class: Tidylib::ValidationErrors
- Inherits:
-
Object
- Object
- Tidylib::ValidationErrors
- Includes:
- Enumerable
- Defined in:
- lib/tidylib/validation_errors.rb
Constant Summary collapse
- VERSION =
"0.1.2"
Instance Method Summary collapse
- #<<(error) ⇒ Object
- #[](topic) ⇒ Object (also: #on)
- #add(topic, message, context = {}) ⇒ Object
- #clear ⇒ Object
- #count ⇒ Object
- #each(&blk) ⇒ Object
- #empty? ⇒ Boolean
- #grouped_by_topic ⇒ Object
-
#initialize ⇒ ValidationErrors
constructor
A new instance of ValidationErrors.
Constructor Details
#initialize ⇒ ValidationErrors
Returns a new instance of ValidationErrors.
9 10 11 |
# File 'lib/tidylib/validation_errors.rb', line 9 def initialize @errors = [] end |
Instance Method Details
#<<(error) ⇒ Object
59 60 61 62 |
# File 'lib/tidylib/validation_errors.rb', line 59 def <<(error) raise ArgumentError unless error.respond_to?(:topic) && error.respond_to?(:message) && error.respond_to?(:context) @errors << error end |
#[](topic) ⇒ Object Also known as: on
27 28 29 30 31 32 33 |
# File 'lib/tidylib/validation_errors.rb', line 27 def [](topic) @errors.select do |error| error.topic == topic end.map do |error| [ error., error.context ] end end |
#add(topic, message, context = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/tidylib/validation_errors.rb', line 17 def add(topic, , context={}) error = OpenStruct.new( topic: topic, message: , context: context ) @errors << error end |
#clear ⇒ Object
51 52 53 |
# File 'lib/tidylib/validation_errors.rb', line 51 def clear @errors = [] end |
#count ⇒ Object
55 56 57 |
# File 'lib/tidylib/validation_errors.rb', line 55 def count @errors.length end |
#each(&blk) ⇒ Object
36 37 38 39 40 |
# File 'lib/tidylib/validation_errors.rb', line 36 def each(&blk) @errors.each do |error| yield error.topic, error., error.context end end |
#empty? ⇒ Boolean
13 14 15 |
# File 'lib/tidylib/validation_errors.rb', line 13 def empty? @errors.empty? end |
#grouped_by_topic ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/tidylib/validation_errors.rb', line 42 def grouped_by_topic @errors.inject({}) do |grouped, error| grouped[error.topic] ||= [] grouped[error.topic] << [error., error.context] grouped end end |