class Vanity::Connection

Constants

DEFAULT_SPECIFICATION

Attributes

adapter[R]
specification[R]

Public Class Methods

new(specification=nil) click to toggle source

With no argument, uses the connection specified in the configuration file, or defaults to Redis on localhost, port 6379. @example

Vanity::Connection.new

If the argument is a string, it is processed as a URL. @example

Vanity::Connection.new("redis://redis.local/5")

If the argument is a Hash, and contains a key `:redis` the value is used as a redis connection. @example

$shared_redis_connection = Redis.new
Vanity::Connection.new(adapter: :redis, redis: $shared_redis_connection)

Otherwise, the argument is a hash and specifies the adapter name and any additional options understood by that adapter (as with config/vanity.yml). Note that all keys are expected to be symbols. @example

Vanity::Connection.new(
  :adapter=>:redis,
  :host=>"redis.local"
)

@since 2.0.0

# File lib/vanity/connection.rb, line 33
def initialize(specification=nil)
  @specification = Specification.new(specification || DEFAULT_SPECIFICATION).to_h

  if Autoconnect.playground_should_autoconnect?
    @adapter = setup_connection(@specification)
  end
end

Public Instance Methods

connected?() click to toggle source

Returns true if connection is open.

@since 2.0.0

# File lib/vanity/connection.rb, line 51
def connected?
  @adapter && @adapter.active?
end
disconnect!() click to toggle source

Closes the current connection.

@since 2.0.0

# File lib/vanity/connection.rb, line 44
def disconnect!
  @adapter.disconnect! if connected?
end

Private Instance Methods

establish_connection(spec) click to toggle source
# File lib/vanity/connection.rb, line 61
def establish_connection(spec)
  Adapters.establish_connection(spec)
end
setup_connection(specification) click to toggle source
# File lib/vanity/connection.rb, line 57
def setup_connection(specification)
  establish_connection(specification)
end