Class: Vanity::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/vanity/connection.rb

Defined Under Namespace

Classes: InvalidSpecification, Specification

Constant Summary

DEFAULT_SPECIFICATION =
{ adapter: "redis" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification = nil) ⇒ Connection

With no argument, uses the connection specified in the configuration file, or defaults to Redis on localhost, port 6379. If the argument is a string, it is processed as a URL. If the argument is a Hash, and contains a key `:redis` the value is used as a 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.

Examples:

Vanity::Connection.new
Vanity::Connection.new("redis://redis.local/5")
$shared_redis_connection = Redis.new
Vanity::Connection.new(adapter: :redis, redis: $shared_redis_connection)
Vanity::Connection.new(
  :adapter=>:redis,
  :host=>"redis.local"
)

Since:

  • 2.0.0



33
34
35
36
37
38
39
# 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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter



7
8
9
# File 'lib/vanity/connection.rb', line 7

def adapter
  @adapter
end

#specificationObject (readonly)

Returns the value of attribute specification



7
8
9
# File 'lib/vanity/connection.rb', line 7

def specification
  @specification
end

Instance Method Details

#connected?Boolean

Returns true if connection is open.

Returns:

  • (Boolean)

Since:

  • 2.0.0



51
52
53
# File 'lib/vanity/connection.rb', line 51

def connected?
  @adapter && @adapter.active?
end

#disconnect!Object

Closes the current connection.

Since:

  • 2.0.0



44
45
46
# File 'lib/vanity/connection.rb', line 44

def disconnect!
  @adapter.disconnect! if connected?
end