Sha256: 5e1928125e81de9e12b0c4f89c410050053b390a2ebe56f5a3b83b6e4c44ee83

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

require_relative 'interface'


module Pod4


  class Connection

    attr_reader   :interface_class
    attr_accessor :data_layer_options

    ##
    # 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)`
    #
    def initialize(args)
      raise ArgumentError, "Connection#new needs a Hash" unless args.is_a? Hash
      raise ArgumentError, "You must pass a Pod4::Interface" \
        unless args[:interface] \
            && args[:interface].is_a?(Class) \
            && args[:interface].ancestors.include?(Interface)

      @interface_class    = args[:interface]
      @data_layer_options = nil
      @client             = nil
      @options            = nil
    end

    ##
    # 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`.
    #
    def client(interface)
      fail_bad_interfaces(interface)
      @client ||= interface.new_connection(@data_layer_options)
      @client
    end

    ##
    # Close the connection.  
    # In the case of a single connection, this is probably not going to get used much. But.
    #
    def close(interface)
      fail_bad_interfaces(interface)
      interface.close_connection 
      @client = nil
      return self
    end

    private

    def fail_bad_interfaces(f)
      raise ArgumentError, "That's not a #@interface_class", caller \
        unless f.kind_of?(@interface_class)

    end

  end # of Connection


end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pod4-1.0.1 lib/pod4/connection.rb
pod4-1.0.0 lib/pod4/connection.rb