Module: Contracts::Constraints

Defined in:
lib/contracts.rb,
lib/contracts/structured.rb

Defined Under Namespace

Classes: All, Anything, ArrayOf, Base, DuckType, HashOf, Length, Nilable, Nothing, OneOf, Predicate, Range, Regex, RespondTo, Shape, Tuple, Type, Union

Class Method Summary collapse

Class Method Details

.coerce(value) ⇒ Object



340
# File 'lib/contracts.rb', line 340

def coerce(value) = value.respond_to?(:matches?) && value.respond_to?(:description) ? value : Type.new(value)

.shape(required: {}, optional: {}, allow_extra: false) ⇒ Object

Creates a required/optional Hash shape constraint.



80
81
82
# File 'lib/contracts/structured.rb', line 80

def shape(required: {}, optional: {}, allow_extra: false)
  Shape.new(required:, optional:, allow_extra:)
end

.tuple(*items) ⇒ Object

Creates a fixed-length heterogeneous Array constraint.



75
76
77
# File 'lib/contracts/structured.rb', line 75

def tuple(*items)
  Tuple.new(*items)
end