Class: Type::Definition::Collection::Constrained
- Inherits:
-
Type::Definition::Collection
- Object
- Type::Definition::Collection
- Type::Definition::Collection::Constrained
- Defined in:
- lib/type/definition/collection/constrained.rb
Overview
A Constrained collection also validates and casts the contents of the collection.
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
Attributes included from Type::Definition
Instance Method Summary collapse
- #constrained? ⇒ True
-
#initialize(parent, constraint) ⇒ Constrained
constructor
A new instance of Constrained.
- #to_s ⇒ Object private
Methods inherited from Type::Definition::Collection
Methods included from Type::Definition
#cast!, included, #nilable, #nilable?, #refine, #to_proc, #valid?
Constructor Details
#initialize(parent, constraint) ⇒ Constrained
Returns a new instance of Constrained.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/type/definition/collection/constrained.rb', line 28 def initialize(parent, constraint) @constraints = Array(constraint).flatten.map { |c| Type.find(c) } validators << method(:validate_each?) castors << method(:cast_each!) super(nil, parent) @name = "#{parent.name}(#{@constraints.join('=>')})" end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
38 39 40 |
# File 'lib/type/definition/collection/constrained.rb', line 38 def constraints @constraints end |
Instance Method Details
#constrained? ⇒ True
41 42 43 |
# File 'lib/type/definition/collection/constrained.rb', line 41 def constrained? true end |
#to_s ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
46 47 48 49 50 |
# File 'lib/type/definition/collection/constrained.rb', line 46 def to_s parent_name = @parent && @parent.name return super unless parent_name "Type::#{parent_name}(#{@constraints.join('=>')})" end |