Class: Pakyow::Data::Connection
- Inherits:
-
Object
- Object
- Pakyow::Data::Connection
- Extended by:
- Forwardable, Support::ClassState, Support::DeepFreeze
- Defined in:
- lib/pakyow/data/connection.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#failure ⇒ Object
readonly
Returns the value of attribute failure.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .adapter(type) ⇒ Object
- .parse_connection_string(connection_string) ⇒ Object
- .register_adapter(type) ⇒ Object
Instance Method Summary collapse
- #auto_migrate? ⇒ Boolean
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #failed? ⇒ Boolean
-
#initialize(type:, name:, string: nil, opts: nil, logger: nil) ⇒ Connection
constructor
A new instance of Connection.
- #migratable? ⇒ Boolean
- #types ⇒ Object
Constructor Details
#initialize(type:, name:, string: nil, opts: nil, logger: nil) ⇒ Connection
Returns a new instance of Connection.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pakyow/data/connection.rb', line 22 def initialize(type:, name:, string: nil, opts: nil, logger: nil) @type, @name, @logger, @failure = type, name, logger, nil @opts = self.class.adapter(type).build_opts( opts.is_a?(Hash) ? opts : self.class.parse_connection_string(string) ) @adapter = self.class.adapter(@type).new(@opts, logger: @logger) rescue ConnectionError, MissingAdapter => error error.context = self @failure = error end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
17 18 19 |
# File 'lib/pakyow/data/connection.rb', line 17 def adapter @adapter end |
#failure ⇒ Object (readonly)
Returns the value of attribute failure.
17 18 19 |
# File 'lib/pakyow/data/connection.rb', line 17 def failure @failure end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/pakyow/data/connection.rb', line 17 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
17 18 19 |
# File 'lib/pakyow/data/connection.rb', line 17 def opts @opts end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
17 18 19 |
# File 'lib/pakyow/data/connection.rb', line 17 def type @type end |
Class Method Details
.adapter(type) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/pakyow/data/connection.rb', line 92 def adapter(type) if @adapter_types.include?(type.to_sym) require "pakyow/data/adapters/#{type}" Adapters.const_get(Support.inflector.camelize(type)) else raise UnknownAdapter.(type: type) end end |
.parse_connection_string(connection_string) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pakyow/data/connection.rb', line 73 def parse_connection_string(connection_string) uri = URI(connection_string) { adapter: uri.scheme, path: uri.path, host: uri.host, port: uri.port, user: uri.user, password: uri.password }.merge( CGI::parse(uri.query.to_s).transform_values(&:first).indifferentize ) end |
.register_adapter(type) ⇒ Object
88 89 90 |
# File 'lib/pakyow/data/connection.rb', line 88 def register_adapter(type) (@adapter_types << type).uniq! end |
Instance Method Details
#auto_migrate? ⇒ Boolean
43 44 45 |
# File 'lib/pakyow/data/connection.rb', line 43 def auto_migrate? migratable? && @adapter.auto_migratable? end |
#connect ⇒ Object
51 52 53 |
# File 'lib/pakyow/data/connection.rb', line 51 def connect @adapter.connect end |
#connected? ⇒ Boolean
35 36 37 |
# File 'lib/pakyow/data/connection.rb', line 35 def connected? !failed? && @adapter.connected? end |
#disconnect ⇒ Object
55 56 57 |
# File 'lib/pakyow/data/connection.rb', line 55 def disconnect @adapter.disconnect end |
#failed? ⇒ Boolean
39 40 41 |
# File 'lib/pakyow/data/connection.rb', line 39 def failed? !@failure.nil? end |
#migratable? ⇒ Boolean
47 48 49 |
# File 'lib/pakyow/data/connection.rb', line 47 def migratable? connected? && @adapter.migratable? end |
#types ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/pakyow/data/connection.rb', line 59 def types if @adapter.class.const_defined?("TYPES") @adapter.class.types_for_adapter(adapter.connection.opts[:adapter]) else {} end end |