Class: Tzispa::Data::AdapterPool
- Inherits:
-
Object
- Object
- Tzispa::Data::AdapterPool
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/tzispa/data/adapter_pool.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
3600- DEFAULT_CACHING_SERVER =
'localhost:11211'
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#default_repo ⇒ Object
readonly
Returns the value of attribute default_repo.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Instance Method Summary collapse
- #[](name = nil) ⇒ Object
- #cache ⇒ Object
- #connect(config) ⇒ Object
- #default ⇒ Object
- #disconnect ⇒ Object
-
#initialize(config, default = nil) ⇒ AdapterPool
constructor
A new instance of AdapterPool.
- #setup_sequel ⇒ Object
Constructor Details
#initialize(config, default = nil) ⇒ AdapterPool
Returns a new instance of AdapterPool.
17 18 19 20 21 22 23 24 |
# File 'lib/tzispa/data/adapter_pool.rb', line 17 def initialize(config, default = nil) setup_sequel @default_repo = default || config.first[0] @config = config @pool ||= {}.tap do |hsh| config.each { |kid, vc| hsh[kid.to_sym] = connect(vc) } end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/tzispa/data/adapter_pool.rb', line 15 def config @config end |
#default_repo ⇒ Object (readonly)
Returns the value of attribute default_repo.
15 16 17 |
# File 'lib/tzispa/data/adapter_pool.rb', line 15 def default_repo @default_repo end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
15 16 17 |
# File 'lib/tzispa/data/adapter_pool.rb', line 15 def pool @pool end |
Instance Method Details
#[](name = nil) ⇒ Object
53 54 55 |
# File 'lib/tzispa/data/adapter_pool.rb', line 53 def [](name = nil) @pool[name&.to_sym || default_repo] end |
#cache ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tzispa/data/adapter_pool.rb', line 26 def cache @cache ||= {}.tap do |csh| config.each do |kid, vc| next unless vc.caching csh[kid.to_sym] = Dalli::Client.new(vc.caching_server || DEFAULT_CACHING_SERVER, namespace: kid.to_s, compress: true) end end end |
#connect(config) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tzispa/data/adapter_pool.rb', line 41 def connect(config) Sequel.connect("#{config.adapter}://#{config.database}").tap do |conn| if config.connection_validation conn.extension(:connection_validator) conn.pool.connection_validation_timeout = config.validation_timeout || DEFAULT_TIMEOUT end if config.respond_to? :extensions config.extensions.split(',').each { |ext| conn.extension ext.to_sym } end end end |
#default ⇒ Object
57 58 59 |
# File 'lib/tzispa/data/adapter_pool.rb', line 57 def default @pool[default_repo] end |
#disconnect ⇒ Object
37 38 39 |
# File 'lib/tzispa/data/adapter_pool.rb', line 37 def disconnect @pool.each_value(&:disconnect) end |
#setup_sequel ⇒ Object
61 62 63 64 65 66 |
# File 'lib/tzispa/data/adapter_pool.rb', line 61 def setup_sequel Sequel.extension :core_extensions Sequel.default_timezone = :utc Sequel.datetime_class = DateTime Sequel.split_symbols = true end |