Class: ROM::Gateway Abstract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes, Notifications::Listener
Defined in:
lib/rom/gateway.rb

Overview

This class is abstract.

Abstract gateway class

Every adapter needs to inherit from this class and implement required interface

Direct Known Subclasses

Memory::Gateway

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)


39
40
41
# File 'lib/rom/gateway.rb', line 39

def connection
  @connection
end

Class Method Details

.adapterSymbol .gateway(adapter) ⇒ Object

Get or set gateway's adapter identifier

Overloads:

  • .adapterSymbol

    Return adapter identifier

    Returns:

    • (Symbol)
  • .gateway(adapter) ⇒ Object

    Examples:

    class MyGateway < ROM::Gateway
      adapter :my_adapter
    end

    Parameters:

    • adapter (Symbol)

      The adapter identifier


35
# File 'lib/rom/gateway.rb', line 35

defines :adapter

.class_from_symbol(type) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get gateway subclass for a specific adapter

Parameters:

  • type (Symbol)

    Adapter identifier

Returns:

  • (Class)

116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rom/gateway.rb', line 116

def self.class_from_symbol(type)
  adapter = ROM.adapters.fetch(type) do
    begin
      require "rom/#{type}"
    rescue LoadError
      raise AdapterLoadError, "Failed to load adapter rom/#{type}"
    end

    ROM.adapters.fetch(type)
  end

  adapter.const_get(:Gateway)
end

.setup(type, *args) ⇒ Gateway .setup(gateway) ⇒ Gateway

Set up a gateway

Overloads:

  • .setup(type, *args) ⇒ Gateway

    Sets up a single-gateway given a gateway type. For custom gateways, create an instance and pass it directly.

    Examples:

    module SuperDB
      class Gateway < ROM::Gateway
        def initialize(options)
        end
      end
    end
    
    ROM.register_adapter(:super_db, SuperDB)
    
    Gateway.setup(:super_db, some: 'options')
    # SuperDB::Gateway.new(some: 'options') is called

    Parameters:

    • type (Symbol)

      Registered gateway identifier

    • args (Array)

      Additional gateway options

  • .setup(gateway) ⇒ Gateway

    Set up a gateway instance

    Examples:

    module SuperDB
      class Gateway < ROM::Gateway
        def initialize(options)
        end
      end
    end
    
    ROM.register_adapter(:super_db, SuperDB)
    
    Gateway.setup(SuperDB::Gateway.new(some: 'options'))

    Parameters:

Returns:

  • (Gateway)

    a specific gateway subclass


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rom/gateway.rb', line 83

def self.setup(gateway_or_scheme, *args)
  case gateway_or_scheme
  when String
    raise ArgumentError, <<-STRING.gsub(/^ {10}/, "")
      URIs without an explicit scheme are not supported anymore.
      See https://github.com/rom-rb/rom/blob/master/CHANGELOG.md
    STRING
  when Symbol
    klass = class_from_symbol(gateway_or_scheme)

    if klass.instance_method(:initialize).arity.zero?
      klass.new
    else
      klass.new(*args)
    end
  else
    raise ArgumentError, "Can't accept arguments when passing an instance" unless args.empty?

    gateway_or_scheme
  end
end

.subscribe(event_id, query = EMPTY_HASH, &block) ⇒ Object Originally defined in module Notifications::Listener

Subscribe to events

Parameters:

  • event_id (String)

    The event key

  • query (Hash) (defaults to: EMPTY_HASH)

    An optional event filter

Returns:

  • (Object)

    self

Instance Method Details

#adapterSymbol

Returns the adapter, defined for the class

Returns:

  • (Symbol)

135
136
137
138
139
140
# File 'lib/rom/gateway.rb', line 135

def adapter
  self.class.adapter || raise(
    MissingAdapterIdentifierError,
    "gateway class +#{self}+ is missing the adapter identifier"
  )
end

#disconnectObject

Disconnect is optional and it's a no-op by default


168
169
170
# File 'lib/rom/gateway.rb', line 168

def disconnect
  # noop
end

#loggerNilClass

A generic interface for returning default logger

Adapters should implement this method as handling loggers is different across adapters. This is a no-op by default and returns nil.

Returns:

  • (NilClass)

161
162
163
# File 'lib/rom/gateway.rb', line 161

def logger
  # noop
end

#transaction(**opts, &block) ⇒ Object

Runs a block inside a transaction. The underlying transaction engine is adapter-specific

Parameters:

  • opts (Hash)

    Transaction options

Returns:

  • The result of yielding the block or +nil+ if the transaction was rolled back


181
182
183
# File 'lib/rom/gateway.rb', line 181

def transaction(**opts, &block)
  transaction_runner(**opts).run(**opts, &block)
end

#use_loggerObject

This method is abstract.

A generic interface for setting up a logger

This is not a required interface, it's a no-op by default


149
150
151
# File 'lib/rom/gateway.rb', line 149

def use_logger(*)
  # noop
end