Sha256: eefba66d463cd1fce892765f4b6e460b725837c102bd3ad83be3f7691c367867

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'rom/support/inflector'

module ROM
  module ConfigurationDSL
    # Setup DSL-specific command extensions
    #
    # @private
    class Command
      # Generate a command subclass
      #
      # This is used by Setup#commands DSL and its `define` block
      #
      # @api private
      def self.build_class(name, relation, options = EMPTY_HASH, &block)
        type = options.fetch(:type) { name }
        command_type = Inflector.classify(type)
        adapter = options.fetch(:adapter)
        parent = ROM::Command.adapter_namespace(adapter).const_get(command_type)
        class_name = generate_class_name(adapter, command_type, relation)

        Dry::Core::ClassBuilder.new(name: class_name, parent: parent).call do |klass|
          klass.register_as(name)
          klass.relation(relation)
          klass.class_eval(&block) if block
        end
      end

      # Create a command subclass name based on adapter, type and relation
      #
      # @api private
      def self.generate_class_name(adapter, command_type, relation)
        pieces = ['ROM']
        pieces << Inflector.classify(adapter)
        pieces << 'Commands'
        pieces << "#{command_type}[#{Inflector.classify(relation)}s]"
        pieces.join('::')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-core-5.3.2 lib/rom/configuration_dsl/command.rb
rom-core-5.3.1 lib/rom/configuration_dsl/command.rb
rom-core-5.3.0 lib/rom/configuration_dsl/command.rb