Sha256: 084823b140da086b005626c45635ba4f6568274fb5bb271432afc8a716d0c7ea

Contents?: true

Size: 1.38 KB

Versions: 13

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "dry/schema/macros/dsl"

module Dry
  module Schema
    module Macros
      # Macro used to specify predicates for each element of an array
      #
      # @api private
      class Array < DSL
        # @api private
        def value(*args, **opts, &block)
          type(:array)

          extract_type_spec(*args, set_type: false) do |*predicates, type_spec:, type_rule:|
            type(schema_dsl.array[type_spec]) if type_spec

            is_hash_block = type_spec.equal?(:hash)

            if predicates.any? || opts.any? || !is_hash_block
              super(
                *predicates, type_spec: type_spec, type_rule: type_rule, **opts,
                &(is_hash_block ? nil : block)
              )
            end

            is_op = args.size.equal?(2) && args[1].is_a?(Logic::Operations::Abstract)

            if is_hash_block && !is_op
              hash(&block)
            elsif is_op
              hash = Value.new(schema_dsl: schema_dsl.new, name: name).hash(args[1])

              trace.captures.concat(hash.trace.captures)

              type(schema_dsl.types[name].of(hash.schema_dsl.types[name]))
            end
          end

          self
        end

        # @api private
        def to_ast(*)
          [:and, [trace.array?.to_ast, [:each, trace.to_ast]]]
        end
        alias_method :ast, :to_ast
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-schema-1.8.0 lib/dry/schema/macros/array.rb
dry-schema-1.7.1 lib/dry/schema/macros/array.rb
dry-schema-1.7.0 lib/dry/schema/macros/array.rb
dry-schema-1.6.2 lib/dry/schema/macros/array.rb
dry-schema-1.6.1 lib/dry/schema/macros/array.rb
dry-schema-1.6.0 lib/dry/schema/macros/array.rb
dry-schema-1.5.6 lib/dry/schema/macros/array.rb
dry-schema-1.5.5 lib/dry/schema/macros/array.rb
dry-schema-1.5.4 lib/dry/schema/macros/array.rb
dry-schema-1.5.3 lib/dry/schema/macros/array.rb
dry-schema-1.5.2 lib/dry/schema/macros/array.rb
dry-schema-1.5.1 lib/dry/schema/macros/array.rb
dry-schema-1.5.0 lib/dry/schema/macros/array.rb