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)


108
# File 'lib/dry/types/constrained.rb', line 108

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

#call_safe(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)


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

def call_safe(input, &)
  if rule[input]
    type.call_safe(input, &)
  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
# File 'lib/dry/types/constrained.rb', line 101

def constrained? = true

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



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

def constructor_type = type.constructor_type

#laxLax

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

Returns:



114
# File 'lib/dry/types/constrained.rb', line 114

def lax = type.lax

#to_ast(meta: true) ⇒ Object

See Also:



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

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, &)
  result = rule.(input)

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