Sha256: 817d91367c81e628ca21384368b31380422643ab8c487dfdca57d254077c0f96

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

require 'active_record/connection_adapters/connection_specification'

module CassandraObject
  module Connection
    extend ActiveSupport::Concern
    
    included do
      class_attribute :connection_spec

      def connection
        self.class.connection
      end

      def connection?
        self.class.connection?
      end

      def disconnect!
        self.class.disconnect!
      end

      def with_connection(*args)
        yield
      end
    end

    module ClassMethods
      DEFAULT_OPTIONS = {
        servers: "127.0.0.1:9160",
      }
      DEFAULT_THRIFT_OPTIONS = {
        exception_class_overrides: [],
      }

      # This doesn't open a connection. It merely conifgures the connection object.
      def establish_connection(config)
        spec = config.reverse_merge(DEFAULT_OPTIONS)

        spec[:thrift] = (spec[:thrift] || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS)
        spec[:thrift][:exception_class_overrides] = spec[:thrift][:exception_class_overrides].map(&:constantize)

        self.connection_spec = spec
      end

      def connection
        @connection ||= CassandraObject::Adapters::CassandraDriver.new(self.connection_spec).client
      end

      def connection?
        !! @connection
      end

      def disconnect!
        @connection.try(:close)
        @connection = nil
      end

      def with_connection(*args)
        yield
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sessionm-cassandra_object-4.0.6 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.5 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.4 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.3 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.2 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.1 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.0 lib/cassandra_object/connection.rb