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

Attributes included from Decorator

#type

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Printable

#to_s

Methods included from Builder

#&, #>, #constrained_type, #constructor, #default, #enum, #fallback, #maybe, #optional, #|

Methods included from Decorator

#default?, #respond_to_missing?, #to_proc

Methods included from Options

#with

Methods included from Type

#call, #valid?

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

#ruleDry::Logic::Rule (readonly)

Returns:

  • (Dry::Logic::Rule)


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

def rule
  @rule
end

Instance Method Details

#===(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


103
104
105
# File 'lib/dry/types/constrained.rb', line 103

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

#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(options) ⇒ Constrained

Parameters:

Returns:

See Also:

  • Logic::Operators#and


87
88
89
# File 'lib/dry/types/constrained.rb', line 87

def constrained(options)
  with(rule: rule & Types.Rule(options))
end

#constrained?true

Returns:

  • (true)


94
95
96
# File 'lib/dry/types/constrained.rb', line 94

def constrained?
  true
end

#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.



122
123
124
# File 'lib/dry/types/constrained.rb', line 122

def constructor_type
  type.constructor_type
end

#laxLax

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

Returns:



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

def lax
  type.lax
end

#to_ast(meta: true) ⇒ Object

See Also:



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

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

#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