module Type::Definition

Type::Definition is the interface for all type definitions.

Standard implementations are:

- Type::Definition::Scalar
- Type::Definition::Collection

Modifier implementations are:

- Type::Definition::Nilable, available as Type::Definition#nilable

Re-open Definition to add nilable methods

Attributes

name[R]

Public Class Methods

included(base) click to toggle source
# File lib/type/definition.rb, line 60
def self.included(base)
  base.extend(ClassMethods)
end
new(name = nil, parent = nil, &block) click to toggle source

Create a new Type::Definition

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

@param name [Symbol] (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?()`

@param parent [Symbol, Type::Definition]

A parent Type::Definition whose validation and casting is done *before*
it is done in self. See the builtin Type::Int32 for an example.
# 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

Public Instance Methods

[](input)
Alias for: cast!
cast!(input) click to toggle source

@param input [Object] @return [Object] the result of casting, guaranteed to be valid. @raise [Type::CastError]

# 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
Also aliased as: []
nilable() click to toggle source

Return a nilable representation of this type definition @return [Type::Definition::Nilable]

# File lib/type/definition/nilable.rb, line 8
def nilable
  Nilable.new(self)
end
nilable?() click to toggle source

@return [False]

# File lib/type/definition/nilable.rb, line 13
def nilable?
  false
end
refine(name = nil, &config) click to toggle source
# File lib/type/definition.rb, line 116
def refine(name = nil, &config)
  self.class.new(name, self, &config)
end
to_proc() click to toggle source

@return [Proc]

# File lib/type/definition.rb, line 121
def to_proc
  method(:cast!).to_proc
end
to_s() click to toggle source

@return [String]

Calls superclass method
# File lib/type/definition.rb, line 130
def to_s
  name ? "Type::#{name}" : super
end
valid?(input, squash_exceptions = true) click to toggle source

@param input [Object] @param squash_exceptions [Boolean] (true) @return [Boolean]

# 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

Protected Instance Methods

castors() click to toggle source

@api private @return [Array<Proc>] Allows seeding with parent's validators

# File lib/type/definition.rb, line 145
def castors
  (@castors ||= [])
end
validators() click to toggle source

@api private @return [Array<Proc>] Allows seeding with parent's validators

# File lib/type/definition.rb, line 137
def validators
  (@validators ||= [])
end

Private Instance Methods

cast(&block) click to toggle source

used for configuring, but not after set up. TODO: extract to DSL. @api private

# File lib/type/definition.rb, line 161
def cast(&block)
  castors << block
end
validate(&block) click to toggle source

used for configuring, but not after set up. TODO: extract to DSL. @api private

# File lib/type/definition.rb, line 153
def validate(&block)
  validators << block
end