Sha256: fb8ee7a867c4ab2f264b7e24b611452b674004508a7747c985c900c8ee89f10f

Contents?: true

Size: 959 Bytes

Versions: 3

Compression:

Stored size: 959 Bytes

Contents

require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/object/blank'

module EbisuConnection
  class Slave
    attr_reader :hostname, :weight

    def initialize(conf, spec)
      case conf
      when String
        host, weight = conf.split(/\s*,\s*/)
        @hostname, port = host.split(/\s*:\s*/)
      when Hash
        conf.stringify_keys!
        @hostname = conf["host"]
        weight = conf["weight"]
        port = conf["port"]
      else
        raise ArgumentError, "slaves config is invalid"
      end

      modify_spec = {"host" => @hostname}
      modify_spec["port"] = port.to_i if port.present?

      @spec = spec.merge(modify_spec)
      @weight = (weight || 1).to_i
    end

    def connection
      @connection ||= ActiveRecord::Base.send("mysql2_connection", @spec)
    end

    def disconnect!
      if @connection
        @connection.disconnect!
        @connection = nil
      end
    rescue
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ebisu_connection-0.2.0 lib/ebisu_connection/slave.rb
ebisu_connection-0.1.1 lib/ebisu_connection/slave.rb
ebisu_connection-0.1.0 lib/ebisu_connection/slave.rb