Sha256: 5a7bc5eef568b85818b7d5b1cf4e0c8ef9f2985b55caa72cfdc45882e4d3acf3

Contents?: true

Size: 620 Bytes

Versions: 2

Compression:

Stored size: 620 Bytes

Contents

#
# Takes `:type` and `:wrap` to construct the final value coercer
#
module Dry::Initializer::Dispatchers::WrapType
  extend self

  def call(type: nil, wrap: 0, **options)
    { type: wrapped_type(type, wrap), **options }
  end

  private

  def wrapped_type(type, count)
    return type if count.zero?

    ->(value) { wrap_value(value, count, type) }
  end

  def wrap_value(value, count, type)
    if count.zero?
      type ? type.call(value) : value
    else
      return [wrap_value(value, count - 1, type)] unless value.is_a?(Array)

      value.map { |item| wrap_value(item, count - 1, type) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-3.0.4 lib/dry/initializer/dispatchers/wrap_type.rb
dry-initializer-3.0.3 lib/dry/initializer/dispatchers/wrap_type.rb