Sha256: e39d30bc580629c8ba9d7466235351425e37d40fd6cbc669f89e03963bd03e94

Contents?: true

Size: 1.41 KB

Versions: 25

Compression:

Stored size: 1.41 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

      @@connection = nil
      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

25 entries across 25 versions & 1 rubygems

Version Path
sessionm-cassandra_object-4.0.32 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.31 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.30 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.29 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.28 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.27 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.26 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.25 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.24 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.23 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.22 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.21 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.20 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.18 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.17 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.16 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.15 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.14 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.13 lib/cassandra_object/connection.rb
sessionm-cassandra_object-4.0.12 lib/cassandra_object/connection.rb