Sha256: 5682959562e816acdc788f6d0a4466e07294a163223836336051b7be5d9d6902

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'dry/schema/macros/dsl'

module Dry
  module Schema
    module Macros
      # A macro used for specifying predicates to be applied to values from a hash
      #
      # @api public
      class Value < DSL
        # @api private
        def call(*predicates, **opts, &block)
          schema = predicates.detect { |predicate| predicate.is_a?(Processor) }

          if schema
            current_type = schema_dsl.types[name]
            updated_type = current_type.respond_to?(:of) ? current_type.of(schema.type_schema) : schema.type_schema

            schema_dsl.set_type(name, updated_type)
          end

          trace.evaluate(*predicates, **opts, &block)

          trace.append(new(chain: false).instance_exec(&block)) if block

          if trace.captures.empty?
            raise ArgumentError, 'wrong number of arguments (given 0, expected at least 1)'
          end

          self
        end

        # @api private
        def respond_to_missing?(meth, include_private = false)
          super || meth.to_s.end_with?(QUESTION_MARK)
        end

        private

        # @api private
        def method_missing(meth, *args, &block)
          if meth.to_s.end_with?(QUESTION_MARK)
            trace.__send__(meth, *args, &block)
          else
            super
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-0.2.0 lib/dry/schema/macros/value.rb