Class: Dry::Types::Default

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator
Defined in:
lib/dry/types/default.rb

Direct Known Subclasses

Callable

Defined Under Namespace

Classes: Callable

Instance Attribute Summary collapse

Attributes included from Decorator

#type

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

#constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Decorator

#constrained?, #constructor, #decorate?, #method_missing, #respond_to_missing?, #valid?

Methods included from Options

#meta, #with

Constructor Details

#initialize(type, value) ⇒ Default

Returns a new instance of Default.

Parameters:



37
38
39
40
# File 'lib/dry/types/default.rb', line 37

def initialize(type, value, *)
  super
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator

Instance Attribute Details

#valueObject (readonly) Also known as: evaluate

Returns:

  • (Object)


21
22
23
# File 'lib/dry/types/default.rb', line 21

def value
  @value
end

Class Method Details

.[](value) ⇒ Default, Dry::Types::Default::Callable

Parameters:

  • value (Object, #call)

Returns:



27
28
29
30
31
32
33
# File 'lib/dry/types/default.rb', line 27

def self.[](value)
  if value.respond_to?(:call)
    Callable
  else
    self
  end
end

Instance Method Details

#call(input) ⇒ Object Also known as: []

Returns value passed through Dry::Types::Decorator#type or Builder#default value.

Parameters:

  • input (Object)

Returns:



61
62
63
64
65
66
67
68
# File 'lib/dry/types/default.rb', line 61

def call(input)
  if input.nil?
    evaluate
  else
    output = type[input]
    output.nil? ? evaluate : output
  end
end

#constrained(*args) ⇒ Default

Parameters:

Returns:



44
45
46
# File 'lib/dry/types/default.rb', line 44

def constrained(*args)
  type.constrained(*args).default(value)
end

#default?true

Returns:

  • (true)


49
50
51
# File 'lib/dry/types/default.rb', line 49

def default?
  true
end

#try(input) ⇒ Success

Parameters:

  • input (Object)

Returns:

  • (Success)


55
56
57
# File 'lib/dry/types/default.rb', line 55

def try(input)
  success(call(input))
end