Sha256: c39a011432c2736f103cef74ab5b08d74cfea7d6ea123e66dc171bd85407c7fe

Contents?: true

Size: 619 Bytes

Versions: 3

Compression:

Stored size: 619 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

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-3.0.2 lib/dry/initializer/dispatchers/wrap_type.rb
dry-initializer-3.0.1 lib/dry/initializer/dispatchers/wrap_type.rb
dry-initializer-3.0.0 lib/dry/initializer/dispatchers/wrap_type.rb