Sha256: c1cb4e339158ab7daf16029fb2c5366fb8e63dd2135d5a023511c79cb79a8c12

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

# frozen_string_literal: true

require 'singleton'

require 'ez/registry/store'
require 'ez/registry/record'

module Ez
  class Registry
    include Singleton

    class << self
      def in(key, by:)
        get_or_initialize_store_by(key)

        add!(yield(Registry::Store.new), to: store[key], by: by)
      end

      def store(key = nil)
        @store ||= {}

        if key
          @store[key] || Registry::Store.new
        else
          @store
        end
      end

      def data(key)
        store(key).map(&:data)
      end

      private

      def get_or_initialize_store_by(key)
        store[key] || store[key] = Registry::Store.new
      end

      def add!(*collection, to:, by:)
        collection.flatten.each do |record|
          record.by = by
          to.push(record)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ez-core-0.2.0 lib/ez/registry.rb