Class: Dry::Types::Constrained

Inherits:
Object
  • Object
show all
Includes:
Builder, Decorator, Printable, Type
Defined in:
lib/dry/types/constrained.rb,
lib/dry/types/constrained/coercible.rb

Overview

Constrained types apply rules to the input

Direct Known Subclasses

Coercible

Defined Under Namespace

Classes: Coercible

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, **options) ⇒ Constrained

Returns a new instance of Constrained.

Parameters:



23
24
25
26
# File 'lib/dry/types/constrained.rb', line 23

def initialize(type, **options)
  super
  @rule = options.fetch(:rule)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Types::Decorator

Instance Attribute Details

#optionsHash (readonly) Originally defined in module Options

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.

Returns:

#ruleDry::Logic::Rule (readonly)

Returns:

  • (Dry::Logic::Rule)


16
17
18
# File 'lib/dry/types/constrained.rb', line 16

def rule
  @rule
end

#typeType (readonly) Originally defined in module Decorator

Returns:

Instance Method Details

#&(other) ⇒ Intersection, Intersection::Constrained Originally defined in module Builder

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.

Compose two types into an Intersection type

Parameters:

Returns:

#===(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


110
111
112
# File 'lib/dry/types/constrained.rb', line 110

def ===(value)
  valid?(value)
end

#>(other) ⇒ Implication, Implication::Constrained Originally defined in module Builder

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.

Compose two types into an Implication type

Parameters:

Returns:

#call(input = Undefined) ⇒ Object #call(input = Undefined) {|output| ... } ⇒ Object Also known as: [] Originally defined in module Type

Apply type to a value

Overloads:

  • #call(input = Undefined) ⇒ Object

    Possibly unsafe coercion attempt. If a value doesn't match the type, an exception will be raised.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Returns:

    • (Object)
  • #call(input = Undefined) {|output| ... } ⇒ Object

    When a block is passed, #call will never throw an exception on failed coercion, instead it will call the block.

    Parameters:

    • input (Object) (defaults to: Undefined)

    Yield Parameters:

    • output (Object)

      Partially coerced value

    Returns:

    • (Object)

#call_safe(input, &block) ⇒ 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.

Returns:

  • (Object)


44
45
46
47
48
49
50
# File 'lib/dry/types/constrained.rb', line 44

def call_safe(input, &block)
  if rule[input]
    type.call_safe(input, &block)
  else
    yield
  end
end

#call_unsafe(input) ⇒ 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.

Returns:

  • (Object)


31
32
33
34
35
36
37
38
39
# File 'lib/dry/types/constrained.rb', line 31

def call_unsafe(input)
  result = rule.(input)

  if result.success?
    type.call_unsafe(input)
  else
    raise ConstraintError.new(result, input)
  end
end

#constrained(*nullary_rules, **unary_rules) ⇒ Constrained

Parameters:

  • *nullary_rules (Array<Symbol>)

    a list of rules that do not require an additional argument (e.g., :odd)

  • **unary_rules (Hash)

    a list of rules that require an additional argument (e.g., gt: 0) The parameters are merger to create a rules hash provided to Dry::Types.Rule and combined using Builder#& with previous #rule

Returns:

See Also:

  • Logic::Operators#and


90
91
92
93
94
95
96
# File 'lib/dry/types/constrained.rb', line 90

def constrained(*nullary_rules, **unary_rules)
  nullary_rules_hash = parse_arguments(nullary_rules)

  rules = nullary_rules_hash.merge(unary_rules)

  with(rule: rule & Types.Rule(rules))
end

#constrained?true

Returns:

  • (true)


101
102
103
# File 'lib/dry/types/constrained.rb', line 101

def constrained?
  true
end

#constrained_typeClass Originally defined in module Builder

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.

Returns:

  • (Class)

#constructor(constructor = nil, **options, &block) ⇒ Constructor Also known as: append, prepend, >>, << Originally defined in module Builder

Define a constructor for the type

Parameters:

  • constructor (#call, nil) (defaults to: nil)
  • options (Hash)
  • block (#call, nil)

Returns:

#constructor_typeObject

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.



129
130
131
# File 'lib/dry/types/constrained.rb', line 129

def constructor_type
  type.constructor_type
end

#default(input = Undefined, options = EMPTY_HASH, &block) ⇒ Default Originally defined in module Builder

Turn a type into a type with a default value

Parameters:

  • input (Object) (defaults to: Undefined)
  • block (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

Raises:

#default?Boolean Originally defined in module Decorator

Returns:

  • (Boolean)

#enum(*values) ⇒ Enum Originally defined in module Builder

Define an enum on top of the existing type

Parameters:

Returns:

#fallback(value = Undefined, shared: false, &_fallback) ⇒ Constructor Originally defined in module Builder

Use the given value on type mismatch

Parameters:

  • value (Object) (defaults to: Undefined)
  • fallback (#call, nil)
  • [Boolean] (Hash)

    a customizable set of options

Returns:

#laxLax

Build lax type. Constraints are not applicable to lax types hence unwrapping

Returns:



118
119
120
# File 'lib/dry/types/constrained.rb', line 118

def lax
  type.lax
end

#maybeMaybe Originally defined in module Builder

Turn a type into a maybe type

Returns:

#optionalSum Originally defined in module Builder

Turn a type into an optional type

Returns:

#respond_to_missing?(meth, include_private = false) ⇒ Boolean Originally defined in module Decorator

Parameters:

  • meth (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)

#to_ast(meta: true) ⇒ Object

See Also:



124
125
126
# File 'lib/dry/types/constrained.rb', line 124

def to_ast(meta: true)
  [:constrained, [type.to_ast(meta: meta), rule.to_ast]]
end

#to_procProc Originally defined in module Decorator

Wrap the type with a proc

Returns:

  • (Proc)

#to_sString Also known as: inspect Originally defined in module Printable

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.

Returns:

  • (String)

#try(input) ⇒ Logic::Result #try(input) {|failure| ... } ⇒ Object

Safe coercion attempt. It is similar to #call with a block given but returns a Result instance with metadata about errors (if any).

Overloads:

  • #try(input) ⇒ Logic::Result

    Parameters:

    • input (Object)

    Returns:

    • (Logic::Result)
  • #try(input) {|failure| ... } ⇒ Object

    Parameters:

    • input (Object)

    Yield Parameters:

    • failure (Failure)

    Yield Returns:

    • (Object)

    Returns:

    • (Object)


67
68
69
70
71
72
73
74
75
76
# File 'lib/dry/types/constrained.rb', line 67

def try(input, &block)
  result = rule.(input)

  if result.success?
    type.try(input, &block)
  else
    failure = failure(input, ConstraintError.new(result, input))
    block_given? ? yield(failure) : failure
  end
end

#valid?(input = Undefined) ⇒ Boolean Also known as: === Originally defined in module Type

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.

Whether a value is a valid member of the type

Returns:

  • (Boolean)

#with(**new_options) ⇒ Type Originally defined in module Options

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.

Parameters:

  • new_options (Hash)

Returns:

#|(other) ⇒ Sum, Sum::Constrained Originally defined in module Builder

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.

Compose two types into a Sum type

Parameters:

Returns: