Module: Dry::Types::Composition

Includes:
Builder, Meta, Options, Printable, Type
Included in:
Implication, Intersection, Sum
Defined in:
lib/dry/types/composition.rb

Defined Under Namespace

Modules: Constrained

Instance Attribute Summary collapse

Attributes included from Options

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Printable

#to_s

Methods included from Meta

#meta, #pristine, #with

Methods included from Options

#with

Methods included from Builder

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

Methods included from Type

#call, #valid?

Instance Attribute Details

#leftType (readonly)

Returns:



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

def left
  @left
end

#rightType (readonly)

Returns:



21
22
23
# File 'lib/dry/types/composition.rb', line 21

def right
  @right
end

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  composition_name = Inflector.demodulize(base)
  ast_type = Inflector.underscore(composition_name).to_sym
  base.define_singleton_method(:ast_type) { ast_type }
  base.define_singleton_method(:composition_name) { composition_name }
  base.const_set("Constrained", Class.new(base) { include Constrained })
end

Instance Method Details

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

Parameters:

  • input (Object)

Returns:

  • (Object)

Raises:

  • (NotImplementedError)


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

def call_safe(input, &block)
  raise NotImplementedError
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.

Parameters:

  • input (Object)

Returns:

  • (Object)

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/dry/types/composition.rb', line 85

def call_unsafe(input)
  raise NotImplementedError
end

#constrained?false

Returns:

  • (false)


69
70
71
# File 'lib/dry/types/composition.rb', line 69

def constrained?
  false
end

#default?false

Returns:

  • (false)


62
63
64
# File 'lib/dry/types/composition.rb', line 62

def default?
  false
end

#failure(input, _error = nil) ⇒ 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.



116
117
118
119
120
121
122
123
# File 'lib/dry/types/composition.rb', line 116

def failure(input, _error = nil)
  result = try(input)
  if result.failure?
    result
  else
    raise ArgumentError, "Invalid failure value '#{input}' for #{inspect}"
  end
end

#initialize(left, right, **options) ⇒ 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.

Parameters:



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

def initialize(left, right, **options)
  super
  @left, @right = left, right
  freeze
end

#nameString

Returns:

  • (String)


55
56
57
# File 'lib/dry/types/composition.rb', line 55

def name
  [left, right].map(&:name).join(" #{self.class.operator} ")
end

#optional?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/dry/types/composition.rb', line 76

def optional?
  false
end

#primitive?(value) ⇒ Boolean

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:

  • value (Object)

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


130
131
132
# File 'lib/dry/types/composition.rb', line 130

def primitive?(value)
  raise NotImplementedError
end

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



106
107
108
109
110
111
112
113
# File 'lib/dry/types/composition.rb', line 106

def success(input)
  result = try(input)
  if result.success?
    result
  else
    raise ArgumentError, "Invalid success value '#{input}' for #{inspect}"
  end
end

#to_ast(meta: true) ⇒ Object

See Also:



137
138
139
140
# File 'lib/dry/types/composition.rb', line 137

def to_ast(meta: true)
  [self.class.ast_type,
   [left.to_ast(meta: meta), right.to_ast(meta: meta), meta ? self.meta : EMPTY_HASH]]
end

#to_procProc

Wrap the type with a proc

Returns:

  • (Proc)


147
148
149
# File 'lib/dry/types/composition.rb', line 147

def to_proc
  proc { |value| self.(value) }
end

#try(input) ⇒ Object

Parameters:

  • input (Object)

Raises:

  • (NotImplementedError)


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

def try(input)
  raise NotImplementedError
end