Class: ROM::Setup

Inherits:
Object
  • Object
show all
Extended by:
Notifications
Defined in:
lib/rom/setup.rb,
lib/rom/compat/setup.rb

Constant Summary collapse

DEFAULT_CLASS_NAMESPACE =
"ROM"
CLASS_NAME_INFERRERS =
{
  relation: -> (name, type:, inflector:, class_namespace:, **) {
    [
      class_namespace,
      inflector.pluralize(inflector.camelize(type)),
      inflector.camelize(name)
    ].compact.join("::")
  },
  command: -> (name, inflector:, adapter:, command_type:, class_namespace:, **) {
    [
      class_namespace,
      inflector.classify(adapter),
      "Commands",
      "#{command_type}[#{inflector.pluralize(inflector.classify(name))}]"
    ].join("::")
  }
}.freeze
DEFAULT_CLASS_NAME_INFERRER =
-> (name, type:, **opts) {
  CLASS_NAME_INFERRERS.fetch(type).(name, type: type, **opts)
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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



78
79
80
81
# File 'lib/rom/setup.rb', line 78

def initialize(...)
  super()
  configure(...)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Gateway (private)

Deprecated.

Returns gateway if method is a name of a registered gateway

Returns:



145
146
147
# File 'lib/rom/compat/setup.rb', line 145

def method_missing(name, *)
  gateways[name] || super
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

#[](key) ⇒ Object

Deprecated.


111
112
113
# File 'lib/rom/compat/setup.rb', line 111

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

#attach_listenersObject

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.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rom/compat/setup.rb', line 40

def attach_listeners
  # Anything can attach globally to certain events, including plugins, so here
  # we're making sure that only plugins that are enabled in this configuration
  # will be triggered
  global_listeners = Notifications.listeners.to_a
    .reject { |(src, *)| plugin_registry.map(&:mod).include?(src) }.to_h

  plugin_listeners = Notifications.listeners.to_a
    .select { |(src, *)| plugins.map(&:mod).include?(src) }.to_h

  listeners.update(global_listeners).update(plugin_listeners)
end

#auto_register(directory, **options) ⇒ Configuration

Enable auto-registration

Parameters:

  • directory (String, Pathname)

    The root path to components

  • options (Hash)

Options Hash (**options):

  • :namespace (Boolean, String)

    Toggle root namespace

  • :auto_load (Boolean)

    Toggle auto-loading via Zeitwerk

Returns:



125
126
127
128
# File 'lib/rom/setup.rb', line 125

def auto_register(directory, **options)
  config.auto_register.update(root_directory: directory, **options)
  self
end

#auto_registration(directory, **options) ⇒ Setup

Deprecated.

Enable auto-registration for a given configuration object

Parameters:

  • directory (String, Pathname)

    The root path to components

  • options (Hash)

Options Hash (**options):

  • :namespace (Boolean, String)

    Toggle root namespace or provide a custom namespace name

Returns:

See Also:



78
79
80
81
82
83
84
# File 'lib/rom/compat/setup.rb', line 78

def auto_registration(directory, **options)
  auto_registration = AutoRegistration.new(directory, inflector: inflector, **options)
  auto_registration.relations.each { |r| register_relation(r) }
  auto_registration.commands.each { |r| register_command(r) }
  auto_registration.mappers.each { |r| register_mapper(r) }
  self
end

#command_classesObject

Deprecated.


99
100
101
# File 'lib/rom/compat/setup.rb', line 99

def command_classes
  components.commands.map(&:constant)
end

#configure(*args) {|_self, config| ... } ⇒ 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.

This is called internally when you pass a block to ROM.container

Yields:

  • (_self, config)

Yield Parameters:

  • _self (ROM::Setup)

    the object that the method was called on



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rom/setup.rb', line 98

def configure(*args)
  # Load config from the arguments passed to the constructor.
  # This *may* override defaults and it's a feature.
  infer_config(*args) unless args.empty?

  # Load adapters explicitly here to ensure their plugins are present for later use
  load_adapters

  # Allow customizations now
  yield(self, config) if block_given?

  # Register gateway components based on current config
  register_gateways

  self
end

#finalizeObject

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.

This is called automatically in configure block

After finalization it is no longer possible to alter the configuration



191
192
193
194
195
196
197
# File 'lib/rom/setup.rb', line 191

def finalize
  # No more config changes allowed
  config.freeze
  yield if block_given?
  loader.() if config.auto_register.key?(:root_directory)
  registry
end

#gatewaysObject Also known as: environment

Deprecated.


117
118
119
120
121
122
123
# File 'lib/rom/compat/setup.rb', line 117

def gateways
  @gateways ||=
    begin
      register_gateways
      registry.gateways.map { |gateway| [gateway.config.id, gateway] }.to_h
    end
end

#gateways_mapObject

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.

Deprecated.


128
129
130
# File 'lib/rom/compat/setup.rb', line 128

def gateways_map
  @gateways_map ||= gateways.map(&:reverse).to_h
end

#inflector=(inflector) ⇒ Object

Deprecated.


60
61
62
# File 'lib/rom/compat/setup.rb', line 60

def inflector=(inflector)
  config.inflector = inflector
end

#listenersObject

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.



54
55
56
# File 'lib/rom/compat/setup.rb', line 54

def listeners
  notifications.listeners
end

#mapper_classesObject

Deprecated.


105
106
107
# File 'lib/rom/compat/setup.rb', line 105

def mapper_classes
  components.mappers.map(&:constant)
end

#notificationsObject

Deprecated.


35
36
37
# File 'lib/rom/compat/setup.rb', line 35

def notifications
  @notifications ||= Notifications.event_bus(:configuration)
end

#register_command(*klasses) ⇒ Object

Register command class(es) explicitly

Parameters:

  • *klasses (Array)

    One or more command classes



178
179
180
181
182
183
184
# File 'lib/rom/setup.rb', line 178

def register_command(*klasses)
  klasses.each do |klass|
    register_constant(:commands, klass)
  end

  components.commands
end

#register_constant(type, constant) ⇒ 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.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rom/setup.rb', line 131

def register_constant(type, constant)
  if config.key?(constant.config.component.type)
    parent_config = config[constant.config.component.type]
    const_config = constant.config.component

    const_config.inherit!(parent_config).join!(parent_config)

    # TODO: make this work with all components
    if const_config.key?(:infer_id_from_class) && const_config.infer_id_from_class
      const_config.id = const_config.inflector.component_id(constant.name)&.to_sym
    end
  end

  components.add(type, constant: constant, config: constant.config.component)
end

#register_mapper(*klasses) ⇒ Object

Register mapper class(es) explicitly

Parameters:

  • *klasses (Array)

    One or more mapper classes



165
166
167
168
169
170
171
# File 'lib/rom/setup.rb', line 165

def register_mapper(*klasses)
  klasses.each do |klass|
    register_constant(:mappers, klass)
  end

  components[:mappers]
end

#register_relation(*klasses) ⇒ Object

Register relation class(es) explicitly

Parameters:

  • *klasses (Array<Relation>)

    One or more relation classes



152
153
154
155
156
157
158
# File 'lib/rom/setup.rb', line 152

def register_relation(*klasses)
  klasses.each do |klass|
    register_constant(:relations, klass)
  end

  components.relations
end

#registryRegistry::Root

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 Setup component registry.

Returns:

  • (Registry::Root)

    Setup component registry



85
86
87
88
89
90
91
92
93
# File 'lib/rom/setup.rb', line 85

def registry
  @registry ||=
    begin
      options = registry_options
      options[:loader] = loader if config.auto_register.auto_load

      super(**options)
    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.

Deprecated.


88
89
90
91
92
93
94
95
# File 'lib/rom/compat/setup.rb', line 88

def relation_classes(gateway = nil)
  if gateway
    gid = gateway.is_a?(Symbol) ? gateway : gateway.config.id
    components.relations.select { |r| r.config[:gateway] == gid }
  else
    components.relations
  end.map(&:constant)
end

#respond_to_missing?(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.

Returns:

  • (Boolean)


133
134
135
# File 'lib/rom/compat/setup.rb', line 133

def respond_to_missing?(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:



207
208
209
210
211
212
213
214
215
216
# File 'lib/rom/setup.rb', line 207

def use(plugin, options = {})
  case plugin
  when Array then plugin.each { |p| use(p) }
  when Hash then plugin.to_a.each { |p| use(*p) }
  else
    plugin_registry[:configuration].fetch(plugin).apply_to(self, options)
  end

  self
end