Class: Dry::Types::Option

Inherits:
Object
  • Object
show all
Includes:
Decorator, Builder, Printable, Type
Defined in:
lib/dry/types/fear/option.rb

Instance Method Summary collapse

Methods included from Builder

#option

Instance Method Details

#call_safe(input = Undefined) ⇒ Fear::Option

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • input (Fear::Option, Object) (defaults to: Undefined)

Returns:

  • (Fear::Option)


33
34
35
36
37
38
39
40
41
42
# File 'lib/dry/types/fear/option.rb', line 33

def call_safe(input = Undefined)
  case input
  when ::Fear::Option
    input
  when Undefined
    Fear.none
  else
    Fear.option(type.call_safe(input) { |output = input| return yield(output) })
  end
end

#call_unsafe(input = Undefined) ⇒ Fear::Option

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • input (Fear::Option, Object) (defaults to: Undefined)

Returns:

  • (Fear::Option)


17
18
19
20
21
22
23
24
25
26
# File 'lib/dry/types/fear/option.rb', line 17

def call_unsafe(input = Undefined)
  case input
  when ::Fear::Option
    input
  when Undefined
    Fear.none
  else
    Fear.option(type.call_unsafe(input))
  end
end

#default(value) ⇒ Object

Parameters:

  • value (Object)

Raises:

  • (ArgumentError)

    if nil provided as default value

See Also:

  • Builder#default


73
74
75
76
77
# File 'lib/dry/types/fear/option.rb', line 73

def default(value)
  raise ArgumentError, "nil cannot be used as a default of a maybe type" if value.nil?

  super
end

#default?true

Returns:

  • (true)


62
63
64
# File 'lib/dry/types/fear/option.rb', line 62

def default?
  true
end

#try(input = Undefined) ⇒ Result::Success

Parameters:

  • input (Object) (defaults to: Undefined)

Returns:

  • (Result::Success)


49
50
51
52
53
54
55
56
57
# File 'lib/dry/types/fear/option.rb', line 49

def try(input = Undefined)
  result = type.try(input)

  if result.success?
    Result::Success.new(Fear.option(result.input))
  else
    result
  end
end