Sha256: 7bd4830aa102757c6496dd59ef213ffe2fce32e449f05cfed383f61eb97bb02b

Contents?: true

Size: 1.21 KB

Versions: 33

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module PgEventstore
  module Extensions
    # Extension that implements creating of a subclass of the class it is used in. The point of creating a subclass is
    # to bound it to the specific connection. This way the specific connection will be available within tha class and
    # all its instances without affecting on the original class.
    # @!visibility private
    module UsingConnectionExtension
      def self.included(klass)
        klass.extend(ClassMethods)
      end

      module ClassMethods
        def connection
          raise("No connection was set. Are you trying to manipulate #{name} outside of its lifecycle?")
        end

        # @param config_name [Symbol]
        # @return [Class<PgEventstore::Subscription>]
        def using_connection(config_name)
          original_class = self
          Class.new(original_class).tap do |klass|
            klass.define_singleton_method(:connection) { PgEventstore.connection(config_name) }
            klass.class_eval do
              [:to_s, :inspect, :name].each do |m|
                define_singleton_method(m, &original_class.method(m))
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
pg_eventstore-1.9.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.8.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.7.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.6.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.5.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.4.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.3.4 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.3.3 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.3.2 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.3.1 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.3.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.2.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.1.5 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.1.4 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.1.3 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.1.2 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.1.1 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.1.0 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.0.4 lib/pg_eventstore/extensions/using_connection_extension.rb
pg_eventstore-1.0.0.rc2 lib/pg_eventstore/extensions/using_connection_extension.rb