Class: ROM::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Notifications
Includes:
ConfigurationDSL
Defined in:
lib/rom/configuration.rb

Constant Summary collapse

NoDefaultAdapterError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Configuration

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.

Initialize a new configuration


53
54
55
56
57
58
59
# File 'lib/rom/configuration.rb', line 53

def initialize(*args, &block)
  @environment = Environment.new(*args)
  @notifications = Notifications.event_bus(:configuration)
  @setup = Setup.new(notifications)

  block&.call(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Gateway (private)

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.

Returns gateway if method is a name of a registered gateway

Returns:


131
132
133
# File 'lib/rom/configuration.rb', line 131

def method_missing(name, *)
  gateways.fetch(name) { super }
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment


30
31
32
# File 'lib/rom/configuration.rb', line 30

def environment
  @environment
end

#notificationsObject (readonly)

Returns the value of attribute notifications


38
39
40
# File 'lib/rom/configuration.rb', line 38

def notifications
  @notifications
end

#setupObject (readonly)

Returns the value of attribute setup


34
35
36
# File 'lib/rom/configuration.rb', line 34

def setup
  @setup
end

Class Method Details

.register_event(id, info = EMPTY_HASH) ⇒ Object Originally defined in module Notifications

Register an event

Parameters:

  • id (String)

    A unique event key

  • info (Hash) (defaults to: EMPTY_HASH)

Instance Method Details

#[](name) ⇒ Gateway

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.

Return gateway identified by name

Returns:


86
87
88
# File 'lib/rom/configuration.rb', line 86

def [](name)
  gateways.fetch(name)
end

#adapter_for_gateway(gateway) ⇒ Object

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.


103
104
105
106
107
# File 'lib/rom/configuration.rb', line 103

def adapter_for_gateway(gateway)
  ROM.adapters.select do |_key, value|
    value.const_defined?(:Gateway) && gateway.is_a?(value.const_get(:Gateway))
  end.keys.first
end

#commands(name, &block) ⇒ Object Originally defined in module ConfigurationDSL

Command definition DSL

Examples:

setup.commands(:users) do
  define(:create) do
    input NewUserParams
    result :one
  end

  define(:update) do
    input UserParams
    result :many
  end

  define(:delete) do
    result :many
  end
end

#default_adapterObject

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.


120
121
122
# File 'lib/rom/configuration.rb', line 120

def default_adapter
  @default_adapter ||= adapter_for_gateway(default_gateway) || ROM.adapters.keys.first
end

#default_gatewayObject

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.


98
99
100
# File 'lib/rom/configuration.rb', line 98

def default_gateway
  @default_gateway ||= gateways[:default]
end

#mappers(&block) ⇒ Object Originally defined in module ConfigurationDSL

Mapper definition DSL

#plugin(adapter, spec, &block) ⇒ Plugin Originally defined in module ConfigurationDSL

Configures a plugin for a specific adapter to be enabled for all relations

Examples:

config = ROM::Configuration.new(:sql, 'sqlite::memory')

config.plugin(:sql, relations: :instrumentation) do |p|
  p.notifications = MyNotificationsBackend
end

config.plugin(:sql, relations: :pagination)

Parameters:

  • adapter (Symbol)

    The adapter identifier

  • spec (Hash<Symbol=>Symbol>)

    Component identifier => plugin identifier

Returns:

#plugin_registryObject Originally defined in module ConfigurationDSL

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.

#relation(name, options = EMPTY_HASH, &block) ⇒ Object Originally defined in module ConfigurationDSL

Relation definition DSL

Examples:

setup.relation(:users) do
  def names
    project(:name)
  end
end

#relation_classes(gateway = nil) ⇒ Object

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.


110
111
112
113
114
115
116
117
# File 'lib/rom/configuration.rb', line 110

def relation_classes(gateway = nil)
  if gateway
    gw_name = gateway.is_a?(Symbol) ? gateway : gateways_map[gateway]
    setup.relation_classes.select { |rel| rel.gateway == gw_name }
  else
    setup.relation_classes
  end
end

#respond_to?(name, include_all = false) ⇒ Boolean

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.

Hook for respond_to? used internally

Returns:

  • (Boolean)

93
94
95
# File 'lib/rom/configuration.rb', line 93

def respond_to?(name, include_all = false)
  gateways.key?(name) || super
end

#use(plugin, options = {}) ⇒ Configuration

Apply a plugin to the configuration

Parameters:

  • plugin (Mixed)

    The plugin identifier, usually a Symbol

  • options (Hash) (defaults to: {})

    Plugin options

Returns:


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rom/configuration.rb', line 69

def use(plugin, options = {})
  if plugin.is_a?(Array)
    plugin.each { |p| use(p) }
  elsif plugin.is_a?(Hash)
    plugin.to_a.each { |p| use(*p) }
  else
    ROM.plugin_registry[:configuration].fetch(plugin).apply_to(self, options)
  end

  self
end