Class: Dry::Types::Constrained

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

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 Builder

#constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|

Methods included from Decorator

#default?, #method_missing, #respond_to_missing?

Methods included from Options

#meta, #pristine, #with

Constructor Details

#initialize(type, options) ⇒ Constrained

Returns a new instance of Constrained.

Parameters:



18
19
20
21
# File 'lib/dry/types/constrained.rb', line 18

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)


14
15
16
# File 'lib/dry/types/constrained.rb', line 14

def rule
  @rule
end

Instance Method Details

#===(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


72
73
74
# File 'lib/dry/types/constrained.rb', line 72

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

#call(input) ⇒ Object Also known as: []

Parameters:

  • input (Object)

Returns:

  • (Object)

Raises:



26
27
28
29
30
# File 'lib/dry/types/constrained.rb', line 26

def call(input)
  try(input) do |result|
    raise ConstraintError.new(result, input)
  end.input
end

#constrained(options) ⇒ Constrained

Parameters:

Returns:

See Also:

  • Logic::Operators#and


61
62
63
# File 'lib/dry/types/constrained.rb', line 61

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

#constrained?true

Returns:

  • (true)


66
67
68
# File 'lib/dry/types/constrained.rb', line 66

def constrained?
  true
end

#decorate?(response) ⇒ Boolean (private)

Parameters:

  • response (Object)

Returns:

  • (Boolean)


89
90
91
# File 'lib/dry/types/constrained.rb', line 89

def decorate?(response)
  super || response.kind_of?(Constructor)
end

#to_ast(meta: true) ⇒ Object

See Also:



79
80
81
82
83
# File 'lib/dry/types/constrained.rb', line 79

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

#try(input, &block) {|failure| ... } ⇒ Logic::Result, ...

Parameters:

  • input (Object)
  • block (#call, nil)

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:

  • (Logic::Result, Result)
  • (Object)

    if block given and try fails



39
40
41
42
43
44
45
46
47
48
# File 'lib/dry/types/constrained.rb', line 39

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

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

#valid?(value) ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


52
53
54
# File 'lib/dry/types/constrained.rb', line 52

def valid?(value)
  rule.(value).success? && type.valid?(value)
end