Class: Type::Definition::Nilable

Inherits:
Object
  • Object
show all
Includes:
Type::Definition
Defined in:
lib/type/definition/nilable.rb

Overview

Nilable Type::Definitions are the same as their non-nilable counterparts with the following exceptions:

  • a ‘nil` value is considered valid

  • a ‘nil` value is returned without casting

Instance Attribute Summary

Attributes included from Type::Definition

#name

Instance Method Summary collapse

Methods included from Type::Definition

included, #refine, #to_proc

Constructor Details

#initialize(parent) ⇒ Nilable

Returns a new instance of Nilable.



23
24
25
# File 'lib/type/definition/nilable.rb', line 23

def initialize(parent)
  super(nil, parent)
end

Instance Method Details

#cast!(input) ⇒ Object

Casts the input unless it is nil



33
34
35
36
# File 'lib/type/definition/nilable.rb', line 33

def cast!(input)
  return nil if input.nil?
  super
end

#nilableself

Returns:

  • (self)


44
45
46
# File 'lib/type/definition/nilable.rb', line 44

def nilable
  self
end

#nilable?True

Returns:

  • (True)


39
40
41
# File 'lib/type/definition/nilable.rb', line 39

def nilable?
  true
end

#to_sString

Returns:

  • (String)


49
50
51
52
# File 'lib/type/definition/nilable.rb', line 49

def to_s
  parent_name = @parent && @parent.name
  parent_name ? "Type::#{parent_name}(nilable)" : super
end

#valid?(input, *args) ⇒ Boolean

Returns true if input is nil or the input is valid

Returns:

  • (Boolean)


28
29
30
# File 'lib/type/definition/nilable.rb', line 28

def valid?(input, *args)
  input.nil? || super
end