Skip to content

Support of wrapped and nested types

Compare
Choose a tag to compare
@nepalez nepalez released this 14 Apr 17:23
class Test
  extend Dry::Initializer

  # Wrapped type
  option :foo, [proc(&:to_s)]

  # Nested type
  option :bar do
    option :baz, proc(&:to_s)
  end

  # Both wrapped and nested type
  option :qux, [] do
    option :sax, proc(&:to_s)
  end
end

test = Test.new foo: 4, bar: { baz: 5 }, qux: { sax: 6 }

# Wrapped type wraps a coerced value to array
test.foo # => ["4"]

# Nested type builds the nested structure
test.bar.baz # => "5"

# Their composition wraps the result of nesting
test.qux.first.sax # => 6