Class: Keymap::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
ActiveSupport::Callbacks
Defined in:
lib/keymap/base.rb,
lib/keymap/connection_adapters/redis_adapter.rb,
lib/keymap/connection_adapters/abstract/connection_specification.rb

Defined Under Namespace

Classes: ConnectionSpecification

Constant Summary collapse

@@configurations =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, options = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

  • _self (Keymap::Base)

    the object that the method was called on



47
48
49
50
# File 'lib/keymap/base.rb', line 47

def initialize(attributes = nil, options = {})
  yield self if block_given?
  #run_callbacks :initialize
end

Class Method Details

.clear_active_connections!Object



150
151
152
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 150

def clear_active_connections!
  connection_handler.clear_active_connections!
end

.connected?Boolean

Returns true if Keymap is connected.

Returns:

  • (Boolean)


142
143
144
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 142

def connected?
  connection_handler.connected?(self)
end

.connectionObject

Returns the connection currently associated with the class. This can also be used to “borrow” the connection to do database work unrelated to any of the specific Keymap collections.



111
112
113
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 111

def connection
  retrieve_connection
end

.connection_configObject

Returns the configuration of the associated connection as a hash:

Keymap::Base.connection_config
# => {:pool=>5, :timeout=>5000, :adapter=>"redis"}

Please use only for reading.



129
130
131
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 129

def connection_config
  connection_pool.spec.config
end

.connection_idObject



115
116
117
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 115

def connection_id
  Thread.current['Keymap::Base.connection_id']
end

.connection_id=(connection_id) ⇒ Object



119
120
121
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 119

def connection_id=(connection_id)
  Thread.current['Keymap::Base.connection_id'] = connection_id
end

.connection_poolObject



133
134
135
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 133

def connection_pool
  connection_handler.retrieve_connection_pool(self) or raise ConnectionNotEstablished
end

.establish_connection(spec = ENV["DATABASE_URL"]) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 95

def self.establish_connection(spec = ENV["DATABASE_URL"])
  resolver = Keymap::Base::ConnectionSpecification::Resolver.new spec, configurations
  spec = resolver.spec

  unless respond_to?(spec.adapter_method)
    raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
  end

  remove_connection
  connection_handler.establish_connection name, spec
end

.redis_connection(config) ⇒ Object



7
8
9
10
11
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 7

def self.redis_connection(config)
  config = config.symbolize_keys
  config[:port] ||= 6379
  ConnectionAdapters::RedisAdapter.new(nil, nil, config)
end

.remove_connection(klass = self) ⇒ Object



146
147
148
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 146

def remove_connection(klass = self)
  connection_handler.remove_connection(klass)
end

.retrieve_connectionObject



137
138
139
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 137

def retrieve_connection
  connection_handler.retrieve_connection(self)
end

Instance Method Details

#configurationsObject

:singleton-method: Contains the database configuration - as is typically stored in config/database.yml - as a Hash.

For example, the following database.yml…

development:
  adapter: redis

production:
  adapter: redis

…would result in Keymap::Base.configurations to look like this:

{
   'development' => {
      'adapter'  => 'redis',
   },
   'production' => {
      'adapter'  => 'redis',
   }
}


42
# File 'lib/keymap/base.rb', line 42

cattr_accessor :configurations, :instance_writer => false

#connectionObject



91
92
93
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 91

def connection
  self.class.connection
end

#connection_handlerObject

:singleton-method: The connection handler



88
# File 'lib/keymap/connection_adapters/abstract/connection_specification.rb', line 88

class_attribute :connection_handler, :instance_writer => false

#loggerObject

:singleton-method: Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed on to any new database connections made and which can be retrieved on both a class and instance level by calling logger.



17
# File 'lib/keymap/base.rb', line 17

cattr_accessor :logger, :instance_writer => false