Class: Pakyow::Data::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Support::ClassState, Support::DeepFreeze
Defined in:
lib/pakyow/data/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#adapterObject (readonly)

Returns the value of attribute adapter.



17
18
19
# File 'lib/pakyow/data/connection.rb', line 17

def adapter
  @adapter
end

#failureObject (readonly)

Returns the value of attribute failure.



17
18
19
# File 'lib/pakyow/data/connection.rb', line 17

def failure
  @failure
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/pakyow/data/connection.rb', line 17

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



17
18
19
# File 'lib/pakyow/data/connection.rb', line 17

def opts
  @opts
end

#typeObject (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.new_with_message(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

Returns:

  • (Boolean)


43
44
45
# File 'lib/pakyow/data/connection.rb', line 43

def auto_migrate?
  migratable? && @adapter.auto_migratable?
end

#connectObject



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

def connect
  @adapter.connect
end

#connected?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/pakyow/data/connection.rb', line 35

def connected?
  !failed? && @adapter.connected?
end

#disconnectObject



55
56
57
# File 'lib/pakyow/data/connection.rb', line 55

def disconnect
  @adapter.disconnect
end

#failed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/pakyow/data/connection.rb', line 39

def failed?
  !@failure.nil?
end

#migratable?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/pakyow/data/connection.rb', line 47

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

#typesObject



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