Sha256: 1aabf4df3293339c53b2e2a179e23f25a78358258fdb682b3b53b3df14f09060

Contents?: true

Size: 1.21 KB

Versions: 10

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require "dry/schema/constants"

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

10 entries across 10 versions & 1 rubygems

Version Path
dry-schema-1.14.0 lib/dry/schema/step.rb
dry-schema-1.13.3 lib/dry/schema/step.rb
dry-schema-1.13.2 lib/dry/schema/step.rb
dry-schema-1.13.1 lib/dry/schema/step.rb
dry-schema-1.13.0 lib/dry/schema/step.rb
dry-schema-1.12.0 lib/dry/schema/step.rb
dry-schema-1.11.3 lib/dry/schema/step.rb
dry-schema-1.11.2 lib/dry/schema/step.rb
dry-schema-1.11.1 lib/dry/schema/step.rb
dry-schema-1.11.0 lib/dry/schema/step.rb