|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "core" |
| 4 | + |
| 5 | +module ROM |
| 6 | + module Components |
| 7 | + # @api public |
| 8 | + class Relation < Core |
| 9 | + id :relation |
| 10 | + |
| 11 | + # @!attribute [r] key |
| 12 | + # @return [Symbol] The relation identifier |
| 13 | + # @api public |
| 14 | + option :key, type: Types.Instance(Symbol), default: -> { |
| 15 | + # TODO: another workaround for auto_register specs not using actual rom classes |
| 16 | + constant.respond_to?(:relation_name) ? constant.relation_name.to_sym : constant.name.to_sym |
| 17 | + } |
| 18 | + alias_method :id, :key |
| 19 | + |
| 20 | + # @api public |
| 21 | + def adapter |
| 22 | + constant.adapter |
| 23 | + end |
| 24 | + |
| 25 | + # @return [ROM::Relation] |
| 26 | + # |
| 27 | + # @api public |
| 28 | + def build |
| 29 | + unless adapter |
| 30 | + raise MissingAdapterIdentifierError, |
| 31 | + "Relation class +#{constant}+ is missing the adapter identifier" |
| 32 | + end |
| 33 | + |
| 34 | + relation_names = components.relations.map(&:key) |
| 35 | + |
| 36 | + # TODO: this should become a built-in feature, no neeed to use a plugin |
| 37 | + constant.use(:registry_reader, relations: relation_names) |
| 38 | + |
| 39 | + trigger("relations.class.ready", relation: constant, adapter: adapter) |
| 40 | + |
| 41 | + schema = finalize_schema |
| 42 | + |
| 43 | + apply_plugins |
| 44 | + |
| 45 | + dataset = gateway.dataset(schema.name.dataset).instance_exec(constant, &constant.dataset) |
| 46 | + |
| 47 | + trigger("relations.dataset.allocated", dataset: dataset, relation: constant, adapter: adapter) |
| 48 | + |
| 49 | + # TODO: this will be removed once mapper registry is lazy-by-default |
| 50 | + mappers = finalize_mappers |
| 51 | + |
| 52 | + options = { |
| 53 | + __registry__: relations, |
| 54 | + mappers: mappers, |
| 55 | + schema: schema, |
| 56 | + inflector: configuration.inflector, |
| 57 | + **plugin_options |
| 58 | + } |
| 59 | + |
| 60 | + relation = constant.new(dataset, **options) |
| 61 | + |
| 62 | + # TODO: this will be removed once command registry is lazy-by-default |
| 63 | + finalize_commands(relation) |
| 64 | + |
| 65 | + relation |
| 66 | + end |
| 67 | + |
| 68 | + private |
| 69 | + |
| 70 | + # @api private |
| 71 | + def finalize_schema |
| 72 | + components.schemas |
| 73 | + .select { |schema| schema.relation == constant } |
| 74 | + .last # TODO: relation DSL auto-defines an empty schema so this is a workaround |
| 75 | + .build |
| 76 | + end |
| 77 | + |
| 78 | + # @api private |
| 79 | + def finalize_mappers |
| 80 | + mappers = components[:mappers] |
| 81 | + .map { |mapper| [mapper.key, mapper.build] if mapper.base_relation == key } |
| 82 | + .compact |
| 83 | + .to_h |
| 84 | + |
| 85 | + constant.mapper_registry(cache: configuration.cache).merge(mappers) |
| 86 | + end |
| 87 | + |
| 88 | + # @api private |
| 89 | + def finalize_commands(relation) |
| 90 | + commands = components.commands |
| 91 | + .select { |command| command.relation_name == constant.relation_name.relation } |
| 92 | + .map { |command| command.build(relation: relation) } |
| 93 | + |
| 94 | + commands.each do |command| |
| 95 | + identifier = command.class.register_as || command.class.default_name |
| 96 | + relation.commands.elements[identifier] = command |
| 97 | + end |
| 98 | + |
| 99 | + command_compiler.commands.elements[constant.relation_name.relation] = relation.commands |
| 100 | + |
| 101 | + relation.commands.set_compiler(command_compiler) |
| 102 | + relation.commands.set_mappers(relation.mappers) |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | +end |
0 commit comments