Sha256: 63c3ab7a10b50713d1446034623d9a74dd6096fe47507519b5ac29dc0abc83a8
Contents?: true
Size: 1.13 KB
Versions: 1
Compression:
Stored size: 1.13 KB
Contents
require 'ostruct' module Tidylib class ValidationErrors include Enumerable VERSION = "0.1.2" def initialize @errors = [] end def empty? @errors.empty? end def add(topic, message, context={}) error = OpenStruct.new( topic: topic, message: message, context: context ) @errors << error end def [](topic) @errors.select do |error| error.topic == topic end.map do |error| [ error.message, error.context ] end end alias :on :[] def each(&blk) @errors.each do |error| yield error.topic, error.message, error.context end end def grouped_by_topic @errors.inject({}) do |grouped, error| grouped[error.topic] ||= [] grouped[error.topic] << [error.message, error.context] grouped end end def clear @errors = [] end def count @errors.length end def <<(error) raise ArgumentError unless error.respond_to?(:topic) && error.respond_to?(:message) && error.respond_to?(:context) @errors << error end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tidylib-validation_errors-0.1.2 | lib/tidylib/validation_errors.rb |