Class: Pod4::Connection

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

Direct Known Subclasses

ConnectionPool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Connection

Intitialise a Connection. You must pass a Pod4::Interface class. The connection object will only accept calls from instances of this class.

‘conn = Pod4::Connection.new(interface: MyInterface)`



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pod4/connection.rb', line 18

def initialize(args=nil)
  if args
    raise ArgumentError, "Connection#new argument needs to be a Hash" unless args.is_a? Hash

    if args[:interface]
      raise ArgumentError, "You must pass a Pod4::Interface" \
        unless args[:interface] \
            && args[:interface].is_a?(Class) \
            && args[:interface].ancestors.include?(Interface)
    end

    @interface_class = args[:interface]
  end

  @data_layer_options = nil
  @client             = nil
  @options            = nil
end

Instance Attribute Details

#data_layer_optionsObject

Returns the value of attribute data_layer_options.



10
11
12
# File 'lib/pod4/connection.rb', line 10

def data_layer_options
  @data_layer_options
end

#interface_classObject (readonly)

Returns the value of attribute interface_class.



9
10
11
# File 'lib/pod4/connection.rb', line 9

def interface_class
  @interface_class
end

Instance Method Details

#client(interface) ⇒ Object

When an interface wants a connection, it calls connection.client. If the connection does not have one, it asks the interface for one.…

Interface is an instance of whatever class you passed to Connection when you initialised it. That is: when an interface wants a connection, it passes ‘self`.



44
45
46
47
48
# File 'lib/pod4/connection.rb', line 44

def client(interface)
  fail_bad_interfaces(interface)
  @client ||= interface.new_connection(@data_layer_options)
  @client
end

#close(interface) ⇒ Object

Close the connection.



53
54
55
56
57
58
# File 'lib/pod4/connection.rb', line 53

def close(interface)
  fail_bad_interfaces(interface)
  interface.close_connection 
  @client = nil
  return self
end