Sha256: 4eabf26454585ffbdfce6fcd5ae28a92f154d271835d123eda671c1e7ad72dbd

Contents?: true

Size: 816 Bytes

Versions: 3

Compression:

Stored size: 816 Bytes

Contents

module SimpleCommand
  class NotImplementedError < ::StandardError; end

  class Errors < Hash
    def add(key, value, _opts = {})
      self[key] ||= []
      self[key] << value
      self[key].uniq!
    end

    def add_multiple_errors(errors_hash)
      errors_hash.each do |key, values|
        SimpleCommand::Utils.array_wrap(values).each { |value| add key, value }
      end
    end

    def each
      each_key do |field|
        self[field].each { |message| yield field, message }
      end
    end

    def full_messages
      map { |attribute, message| full_message(attribute, message) }
    end

    private
    def full_message(attribute, message)
      return message if attribute == :base
      attr_name = attribute.to_s.tr('.', '_').capitalize
      "%s %s" % [attr_name, message]
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_command-1.0.1 lib/simple_command/errors.rb
simple_command-1.0.0 lib/simple_command/errors.rb
simple_command-0.2.1 lib/simple_command/errors.rb