Module: Type::Definition

Included in:
Collection, Nilable, Scalar
Defined in:
lib/type/definition.rb,
lib/type/definition/proxy.rb,
lib/type/definition/scalar.rb,
lib/type/definition/nilable.rb,
lib/type/definition/collection.rb,
lib/type/definition/collection/constrained.rb

Overview

Re-open Definition to add nilable methods

Defined Under Namespace

Modules: ClassMethods Classes: Collection, Nilable, Proxy, Scalar

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



89
90
91
# File 'lib/type/definition.rb', line 89

def name
  @name
end

Class Method Details

.included(base) ⇒ Object



60
61
62
# File 'lib/type/definition.rb', line 60

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

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

Returns the result of casting, guaranteed to be valid.

Parameters:

  • input (Object)

Returns:

  • (Object)

    the result of casting, guaranteed to be valid.

Raises:



104
105
106
107
108
109
110
111
112
113
# File 'lib/type/definition.rb', line 104

def cast!(input)
  return input if valid?(input)
  castors.reduce(input) do |intermediate, castor|
    castor[intermediate]
  end.tap do |output|
    raise ValidationError.new(output, self) unless valid?(output, false)
  end
rescue
  raise CastError.new(input, self)
end

#initialize(name = nil, parent = nil, &block) ⇒ Object

Create a new Type::Definition

You should never have to use Type::Definition#initialize directly;
instead use Type::Definition::generate()

Parameters:

  • name (Symbol) (defaults to: nil)

    (nil) Capital-letter symbol (e.g., ‘:Int32`) for which to register this definition globally. If defining a `Type::Definition` with name `:FooBar`, the following are registerde:

    - `Type::FooBar`: a reference to the `Type::Definition`
    - `Type::FooBar()`: an alias to `Type::FooBar::cast!()`
    - `Type::FooBar?()`: an alias to `Type::FooBar::validate?()`
    
  • parent (Symbol, Type::Definition) (defaults to: nil)

    A parent Type::Definition whose validation and casting is done before it is done in self. See the builtin Type::Int32 for an example.



79
80
81
82
83
84
85
86
87
88
# File 'lib/type/definition.rb', line 79

def initialize(name = nil, parent = nil, &block)
  @name = name && name.to_sym
  if parent
    @parent = Type.find(parent)
    validators.concat @parent.validators.dup
    castors.concat @parent.castors.dup
  end
  Type.register(self)
  instance_exec(&block) if block_given?
end

#nilableType::Definition::Nilable

Return a nilable representation of this type definition



8
9
10
# File 'lib/type/definition/nilable.rb', line 8

def nilable
  Nilable.new(self)
end

#nilable?False

Returns:

  • (False)


13
14
15
# File 'lib/type/definition/nilable.rb', line 13

def nilable?
  false
end

#refine(name = nil, &config) ⇒ Object



116
117
118
# File 'lib/type/definition.rb', line 116

def refine(name = nil, &config)
  self.class.new(name, self, &config)
end

#to_procProc

Returns:

  • (Proc)


121
122
123
# File 'lib/type/definition.rb', line 121

def to_proc
  method(:cast!).to_proc
end

#to_sString

Returns:

  • (String)


130
131
132
# File 'lib/type/definition.rb', line 130

def to_s
  name ? "Type::#{name}" : super
end

#valid?(input, squash_exceptions = true) ⇒ Boolean

Parameters:

  • input (Object)
  • squash_exceptions (Boolean) (defaults to: true)

    (true)

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/type/definition.rb', line 94

def valid?(input, squash_exceptions = true)
  validators.all? { |proc| proc[input] }
rescue Exception
  raise unless squash_exceptions
  false
end