Class: ROM::Setup
- Inherits:
-
Object
- Object
- ROM::Setup
- 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
-
.register_event(id, info = EMPTY_HASH) ⇒ Object
extended
from Notifications
Register an event.
Instance Method Summary collapse
- #[](key) ⇒ Object deprecated Deprecated.
- #attach_listeners ⇒ Object private
-
#auto_register(directory, **options) ⇒ Configuration
Enable auto-registration.
- #auto_registration(directory, **options) ⇒ Setup deprecated Deprecated.
- #command_classes ⇒ Object deprecated Deprecated.
-
#configure(*args) {|_self, config| ... } ⇒ Object
private
This is called internally when you pass a block to ROM.container.
-
#finalize ⇒ Object
private
This is called automatically in configure block.
- #gateways ⇒ Object (also: #environment) deprecated Deprecated.
- #gateways_map ⇒ Object deprecated private Deprecated.
- #inflector=(inflector) ⇒ Object deprecated Deprecated.
-
#initialize ⇒ Configuration
constructor
private
Initialize a new configuration.
- #listeners ⇒ Object private
- #mapper_classes ⇒ Object deprecated Deprecated.
- #notifications ⇒ Object deprecated Deprecated.
-
#register_command(*klasses) ⇒ Object
Register command class(es) explicitly.
- #register_constant(type, constant) ⇒ Object private
-
#register_mapper(*klasses) ⇒ Object
Register mapper class(es) explicitly.
-
#register_relation(*klasses) ⇒ Object
Register relation class(es) explicitly.
-
#registry ⇒ Registry::Root
private
Setup component registry.
- #relation_classes(gateway = nil) ⇒ Object deprecated private Deprecated.
- #respond_to_missing?(name, include_all = false) ⇒ Boolean private
-
#use(plugin, options = {}) ⇒ Configuration
Apply a plugin to the configuration.
Constructor Details
#initialize ⇒ 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
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)
Returns gateway if method is a name of a registered gateway
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
Instance Method Details
#[](key) ⇒ Object
111 112 113 |
# File 'lib/rom/compat/setup.rb', line 111 def [](key) gateways.fetch(key) end |
#attach_listeners ⇒ 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.
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
125 126 127 128 |
# File 'lib/rom/setup.rb', line 125 def auto_register(directory, **) config.auto_register.update(root_directory: directory, **) self end |
#auto_registration(directory, **options) ⇒ Setup
Enable auto-registration for a given configuration object
78 79 80 81 82 83 84 |
# File 'lib/rom/compat/setup.rb', line 78 def auto_registration(directory, **) auto_registration = AutoRegistration.new(directory, inflector: inflector, **) 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_classes ⇒ Object
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
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 |
#finalize ⇒ 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 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 |
#gateways ⇒ Object Also known as: environment
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_map ⇒ 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.
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
60 61 62 |
# File 'lib/rom/compat/setup.rb', line 60 def inflector=(inflector) config.inflector = inflector end |
#listeners ⇒ 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.
54 55 56 |
# File 'lib/rom/compat/setup.rb', line 54 def listeners notifications.listeners end |
#mapper_classes ⇒ Object
105 106 107 |
# File 'lib/rom/compat/setup.rb', line 105 def mapper_classes components.mappers.map(&:constant) end |
#notifications ⇒ Object
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
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
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
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 |
#registry ⇒ Registry::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.
85 86 87 88 89 90 91 92 93 |
# File 'lib/rom/setup.rb', line 85 def registry @registry ||= begin = [:loader] = loader if config.auto_register.auto_load super(**) 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.
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.
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
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rom/setup.rb', line 207 def use(plugin, = {}) 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, ) end self end |