Sha256: 29335c76a5f81793f256d366460cc09041cff14e67487c308a756b4573a6aa2e

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Light
  module Services
    class Messages
      def initialize
        @storage = {}
      end

      def add(key, message)
        storage[key] ||= []
        storage[key] << message
      end

      def from_record(record)
        record.errors.to_h.each do |key, message|
          add(key, message)
        end
      end

      def from_service(service)
        service.errors.each do |key, message|
          add(key, message)
        end
      end

      def delete(key)
        storage.delete(key)
      end

      def blank?
        storage.empty?
      end

      def any?
        !blank?
      end

      def to_hash
        storage
      end

      def flatten
        to_hash.flat_map do |key, messages|
          messages.map do |message|
            [key, message]
          end
        end
      end

      def each
        flatten.each do |key, message|
          yield key, message
        end
      end

      alias to_h to_hash

      private

      # Getters/Setters
      attr_accessor :storage
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
light-services-0.5.4 lib/light/services/messages.rb