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

#options, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

#constrained, #constructor, #default, #enum, #optional, #safe, #|

Methods included from Decorator

#constructor, #respond_to_missing?, #valid?, #with

Constructor Details

#initialize(type, options) ⇒ Default

Returns a new instance of Default.



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

def initialize(type, options)
  super
  @value = options.fetch(: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.



15
16
17
# File 'lib/dry/types/default.rb', line 15

def value
  @value
end

Class Method Details

.[](value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/dry/types/default.rb', line 19

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

Instance Method Details

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



32
33
34
35
36
37
38
39
# File 'lib/dry/types/default.rb', line 32

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