class Type::Definition::Collection::Constrained
A Constrained
collection also validates and casts the contents of the collection.
Attributes
constraints[R]
Public Class Methods
new(parent, constraint)
click to toggle source
@api private (See Type::Defintion::Collection#constrain)
Calls superclass method
Type::Definition::new
# 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
Public Instance Methods
constrained?()
click to toggle source
@return [True]
# File lib/type/definition/collection/constrained.rb, line 41 def constrained? true end
to_s()
click to toggle source
@api private
Calls superclass method
Type::Definition#to_s
# 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
Protected Instance Methods
cast_each!(enum)
click to toggle source
@api private @param enum [Enumerable] @return [Enumerable]
# File lib/type/definition/collection/constrained.rb, line 69 def cast_each!(enum) enum.map do |item| next @constraints.first.cast!(item) if @constraints.size == 1 @constraints.zip(item).map do |constraint, value| constraint.cast!(value) end end end
validate_each?(enum)
click to toggle source
@api private @param enum [Enumerable] @return [Boolean]
# File lib/type/definition/collection/constrained.rb, line 57 def validate_each?(enum) enum.all? do |item| next @constraints.first.valid?(item) if @constraints.size == 1 @constraints.zip(item).all? do |constraint, value| constraint.valid?(value) end end end