Method: Type::Definition#initialize

Defined in:
lib/type/definition.rb

#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