Class: Dry::Types::Default

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator, Type
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, #pristine, #with

Constructor Details

#initialize(type, value) ⇒ Default

Returns a new instance of Default.

Parameters:

  • type (Type)
  • value (Object)


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

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)


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

def value
  @value
end

Class Method Details

.[](value) ⇒ Class

Parameters:

  • value (Object, #call)

Returns:



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

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:



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

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

#constrained(*args) ⇒ Default

Parameters:

Returns:



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

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

#default?true

Returns:

  • (true)


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

def default?
  true
end

#try(input) ⇒ Result::Success

Parameters:

  • input (Object)

Returns:



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

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