# frozen_string_literal: true require "rom/setup" require "rom/support/notifications" module ROM # @api public class Setup extend Notifications register_event("configuration.relations.class.ready") register_event("configuration.relations.object.registered") register_event("configuration.relations.registry.created") register_event("configuration.relations.schema.allocated") register_event("configuration.relations.schema.set") register_event("configuration.relations.dataset.allocated") register_event("configuration.commands.class.before_build") mod = Module.new do # @api public def finalize super { attach_listeners } end # @api private def registry_options super.merge(notifications: notifications) end end prepend(mod) # @api public # @deprecated def notifications @notifications ||= Notifications.event_bus(:configuration) end # @api private 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 # @api private def listeners notifications.listeners end # @api public # @deprecated def inflector=(inflector) config.inflector = inflector end # Enable auto-registration for a given configuration object # # @param [String, Pathname] directory The root path to components # @param [Hash] options # @option options [Boolean, String] :namespace Toggle root namespace # or provide a custom namespace name # # @return [Setup] # # @deprecated # # @see Configuration#auto_register # # @api public 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 # @api private # @deprecated 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 # @api public # @deprecated def command_classes components.commands.map(&:constant) end # @api public # @deprecated def mapper_classes components.mappers.map(&:constant) end # @api public # @deprecated def [](key) gateways.fetch(key) end # @api public # @deprecated def gateways @gateways ||= begin register_gateways registry.gateways.map { |gateway| [gateway.config.id, gateway] }.to_h end end alias_method :environment, :gateways # @api private # @deprecated def gateways_map @gateways_map ||= gateways.map(&:reverse).to_h end # @api private def respond_to_missing?(name, include_all = false) gateways.key?(name) || super end private # Returns gateway if method is a name of a registered gateway # # @return [Gateway] # # @api public # @deprecated def method_missing(name, *) gateways[name] || super end end Configuration = Setup end