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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#cast!(input) ⇒ Object
(also: #[])
The result of casting, guaranteed to be valid.
-
#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().
-
#nilable ⇒ Type::Definition::Nilable
Return a nilable representation of this type definition.
- #nilable? ⇒ False
- #refine(name = nil, &config) ⇒ Object
- #to_proc ⇒ Proc
- #to_s ⇒ String
- #valid?(input, squash_exceptions = true) ⇒ Boolean
Instance Attribute Details
#name ⇒ Object (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.
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()
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 |
#nilable ⇒ Type::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
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_proc ⇒ Proc
121 122 123 |
# File 'lib/type/definition.rb', line 121 def to_proc method(:cast!).to_proc end |
#to_s ⇒ 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
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 |