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) ⇒ 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.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/type/definition.rb', line 102 def cast!(input) input = yield if block_given? raise CastError.new(input, self) if input.nil? castors.reduce(input) do |intermediate, castor| castor[intermediate] end.tap do |output| raise ValidationError.new(output, self) unless valid?(output) 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
115 116 117 |
# File 'lib/type/definition.rb', line 115 def refine(name = nil, &config) self.class.new(name, self, &config) end |
#to_proc ⇒ Proc
120 121 122 |
# File 'lib/type/definition.rb', line 120 def to_proc method(:cast!).to_proc end |
#to_s ⇒ String
129 130 131 |
# File 'lib/type/definition.rb', line 129 def to_s name ? "Type::#{name}" : super end |
#valid?(input) ⇒ Boolean
93 94 95 96 97 |
# File 'lib/type/definition.rb', line 93 def valid?(input) validators.all? { |proc| proc[input] } rescue false end |