Class: Type::Definition::Collection::Constrained

Inherits:
Type::Definition::Collection show all
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

Attributes included from Type::Definition

#name

Instance Method Summary collapse

Methods inherited from Type::Definition::Collection

#cast!, #constrain, #valid?

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

#constraintsObject (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

Returns:

  • (True)


41
42
43
# File 'lib/type/definition/collection/constrained.rb', line 41

def constrained?
  true
end

#to_sObject

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