Sha256: dedc6a5ac3e0b233bd223683be93ddcb238322a563ec5b30c9ac65bdebd20ddb

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

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

      def add(key, message, rollback: true)
        storage[key] ||= []
        storage[key] << message

        rollback! if rollback
      end

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

        rollback! if rollback
      end

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

        rollback! if rollback
      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

      def rollback!
        raise ActiveRecord::Rollback if defined?(ActiveRecord::Rollback)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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