Class: Dry::Types::Definition

Inherits:
Object
  • Object
show all
Includes:
Builder, Options
Defined in:
lib/dry/types/definition.rb

Direct Known Subclasses

Array, Constructor, Hash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Builder

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

Methods included from Options

#meta, #with

Constructor Details

#initialize(primitive, options = {}) ⇒ Definition

Returns a new instance of Definition.



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

def initialize(primitive, options = {})
  super
  @primitive = primitive
  freeze
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/dry/types/definition.rb', line 12

def options
  @options
end

#primitiveObject (readonly)

Returns the value of attribute primitive.



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

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/dry/types/definition.rb', line 16

def self.[](primitive)
  if primitive == ::Array
    Types::Array
  elsif primitive == ::Hash
    Types::Hash
  else
    self
  end
end

Instance Method Details

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



44
45
46
# File 'lib/dry/types/definition.rb', line 44

def call(input)
  input
end

#constrained?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dry/types/definition.rb', line 40

def constrained?
  false
end

#default?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dry/types/definition.rb', line 36

def default?
  false
end

#failure(*args) ⇒ Object



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

def failure(*args)
  result(Result::Failure, *args)
end

#nameObject



32
33
34
# File 'lib/dry/types/definition.rb', line 32

def name
  primitive.name
end

#primitive?(value) ⇒ Boolean Also known as: valid?

Returns:

  • (Boolean)


70
71
72
# File 'lib/dry/types/definition.rb', line 70

def primitive?(value)
  value.is_a?(primitive)
end

#result(klass, *args) ⇒ Object



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

def result(klass, *args)
  klass.new(*args)
end

#success(*args) ⇒ Object



58
59
60
# File 'lib/dry/types/definition.rb', line 58

def success(*args)
  result(Result::Success, *args)
end

#try(input, &block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/dry/types/definition.rb', line 49

def try(input, &block)
  if valid?(input)
    success(input)
  else
    failure = failure(input, "#{input.inspect} must be an instance of #{primitive}")
    block ? yield(failure) : failure
  end
end