Sha256: 1d565ba022a6432510d4683544dfef6176b44ff1ea6f9edae1ba091d08a3d0df

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

class InputSanitizer::V2::ErrorCollection
  include Enumerable

  attr_reader :error_codes, :messages

  def initialize(errors)
    @error_codes = {}
    @messages = {}
    @error_details = {}

    errors.each do |error|
      (@error_codes[error.field] ||= []) << error.code
      (@messages[error.field] ||= []) << error.message
      error_value_hash = { :value => error.value }
      (@error_details[error.field] ||= []) << error_value_hash
    end
  end

  def [](attribute)
    @error_codes[attribute]
  end

  def each
    @error_codes.each_key do |attribute|
      self[attribute].each { |error| yield attribute, error }
    end
  end

  def size
    @error_codes.size
  end
  alias_method :length, :size
  alias_method :count, :size

  def empty?
    @error_codes.empty?
  end

  def add(attribute, code = :invalid, options = {})
    (@error_codes[attribute] ||= []) << code
    (@messages[attribute] ||= []) << options.delete(:messages)
    (@error_details[attribute] ||= []) << options
  end

  def to_hash
    messages.dup
  end
  alias_method :full_messages, :to_hash
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
input_sanitizer-0.6.0 lib/input_sanitizer/v2/error_collection.rb
input_sanitizer-0.5.0 lib/input_sanitizer/v2/error_collection.rb
input_sanitizer-0.4.1 lib/input_sanitizer/v2/error_collection.rb
input_sanitizer-0.4.0 lib/input_sanitizer/v2/error_collection.rb
input_sanitizer-0.3.33 lib/input_sanitizer/v2/error_collection.rb