module Type::Definition::ClassMethods

Public Instance Methods

generate(name = nil, &block) click to toggle source

@api protected

Public APIs are Type::scalar and Type::collection

@overload generate(name, &block)

The block is called in the context of the definition, and is expected
to call one of `#validate` or `#cast` with appropriate blocks.
@param name [Symbol, nil] (nil)
  Capital-letter symbol (e.g., `:Int32`) for which to register this
  definition globally.
@return [Type::Definition]
@example
~~~ ruby
  Type::scalar(:Integer) do
    validate do |input|
      input.kind_of?(::Integer)
    end
    cast do |input|
      Kernel::Integer(input)
    end
  end
~~~

@overload generate(name)

@param name [Symbol, nil] (nil)
  Capital-letter symbol (e.g., `:Int32`) for which to register this
  definition globally.
@return [Type::Definition::Proxy]
  You are expected to call from(type_def, &block) to finish the
  definition

@return [Type::Definition, Type::Definition::Proxy]

@example
~~~ ruby
  Type::scalar(:Int32).from(:Integer) do
    int32_range = (-1 << 31) ... (1 << 31)
    validate do |input|
      int32_range.include?(input)
    end
  end
~~~
# File lib/type/definition.rb, line 54
def generate(name = nil, &block)
  return new(name, &block) if block_given?
  Proxy.new(name, self)
end