Sha256: fcdd21d55e4101a6aec773907e736c3ddf4216ebba50338ac6275190fd0b6fb2

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

require 'active_support/core_ext/class/attribute'
require 'active_support/concern'
require_relative '../connection'
require_relative '../configuration'

module ActiveHouse
  module Modeling
    module Connection
      extend ActiveSupport::Concern

      included do
        class_attribute :_connection_class, instance_accessor: false
        self._connection_class = ActiveHouse::Connection
      end

      class_methods do
        def _connection
          Thread.current[name]
        end

        def _connection=(value)
          Thread.current[name] = value
        end

        def ensure_connection
          establish_connection if _connection.nil?
        end

        def establish_connection(name_or_config = nil)
          config = if name_or_config.is_a?(Hash)
                     name_or_config.symbolize_keys
                   else
                     ActiveHouse.configuration.connection_config_for(name_or_config)
                   end
          self._connection = _connection_class.new(config)
        end

        def connection
          ensure_connection
          _connection
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_house-0.8.0 lib/active_house/modeling/connection.rb
active_house-0.7.0 lib/active_house/modeling/connection.rb
active_house-0.6.6 lib/active_house/modeling/connection.rb