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, #respond_to_missing?, #valid?

Methods included from Options

#meta, #with

Constructor Details

#initialize(type, value) ⇒ Default

Returns a new instance of Default.



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

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 the value of attribute value.



18
19
20
# File 'lib/dry/types/default.rb', line 18

def value
  @value
end

Class Method Details

.[](value) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/dry/types/default.rb', line 22

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

Instance Method Details

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



47
48
49
50
51
52
53
54
# File 'lib/dry/types/default.rb', line 47

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

#constrained(*args) ⇒ Object



35
36
37
# File 'lib/dry/types/default.rb', line 35

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

#default?Boolean

Returns:

  • (Boolean)


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

def default?
  true
end

#try(input) ⇒ Object



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

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