Module: Explicit::Type::Modifiers::Nilable

Extended by:
Nilable
Included in:
Nilable
Defined in:
lib/explicit/type/modifiers/nilable.rb

Instance Method Summary collapse

Instance Method Details

#apply(type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/explicit/type/modifiers/nilable.rb', line 6

def apply(type)
  Explicit::Type.build(type).tap do |type|
    type.nilable = true

    original_validate = type.method(:validate)

    type.define_singleton_method(:validate, lambda do |value|
      return [:ok, nil] if value.nil?

      original_validate.(value)
    end)
  end
end