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
Public Class Methods
# File lib/type/definition.rb, line 60 def self.included(base) base.extend(ClassMethods) end
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
@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
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
@return [False]
# File lib/type/definition/nilable.rb, line 13 def nilable? false end
# File lib/type/definition.rb, line 116 def refine(name = nil, &config) self.class.new(name, self, &config) end
@return [Proc]
# File lib/type/definition.rb, line 121 def to_proc method(:cast!).to_proc end
@return [String]
# File lib/type/definition.rb, line 130 def to_s name ? "Type::#{name}" : super end
@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
@api private @return [Array<Proc>] Allows seeding with parent's validators
# File lib/type/definition.rb, line 145 def castors (@castors ||= []) end
@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
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
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