Sha256: ff55dcd6cb1175607c0cab63b20ba713358be803db2aef6f0b1df6e16cbe31cf

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "dry/schema/constants"
require "dry/schema/path"

module Dry
  module Schema
    # @api private
    class Step
      # @api private
      attr_reader :name

      # @api private
      attr_reader :type

      # @api private
      attr_reader :executor

      # @api private
      attr_reader :path

      # @api private
      def initialize(type:, name:, executor:, path: Path::EMPTY)
        @type = type
        @name = name
        @executor = executor
        @path = path
        validate_name(name)
      end

      # @api private
      def call(result)
        scoped_result = path.equal?(Path::EMPTY) ? result : result.at(path)

        output = executor.(scoped_result)
        scoped_result.replace(output) if output.is_a?(Hash)
        output
      end

      # @api private
      def scoped(parent_path)
        self.class.new(
          type: type,
          name: name,
          executor: executor,
          path: Path.new([*parent_path, *path])
        )
      end

      private

      # @api private
      def validate_name(name)
        return if STEPS_IN_ORDER.include?(name)

        raise ArgumentError, "Undefined step name #{name}. Available names: #{STEPS_IN_ORDER}"
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-schema-1.10.6 lib/dry/schema/step.rb
dry-schema-1.10.5 lib/dry/schema/step.rb
dry-schema-1.10.4 lib/dry/schema/step.rb
dry-schema-1.10.3 lib/dry/schema/step.rb
dry-schema-1.10.2 lib/dry/schema/step.rb
dry-schema-1.10.1 lib/dry/schema/step.rb
dry-schema-1.9.3 lib/dry/schema/step.rb
dry-schema-1.9.2 lib/dry/schema/step.rb
dry-schema-1.9.1 lib/dry/schema/step.rb
dry-schema-1.9.0 lib/dry/schema/step.rb
dry-schema-1.8.0 lib/dry/schema/step.rb
dry-schema-1.7.1 lib/dry/schema/step.rb
dry-schema-1.7.0 lib/dry/schema/step.rb