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.

Parameters:

  • primitive (Class)
  • options (Hash) (defaults to: {})


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

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

Instance Attribute Details

#optionsHash (readonly)

Returns:



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

def options
  @options
end

#primitiveClass (readonly)

Returns:

  • (Class)


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

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Definition

Parameters:

  • primitive (Class)

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/dry/types/definition.rb', line 20

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: []

Parameters:

  • input (Object)

Returns:

  • (Object)


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

def call(input)
  input
end

#constrained?false

Returns:

  • (false)


49
50
51
# File 'lib/dry/types/definition.rb', line 49

def constrained?
  false
end

#default?false

Returns:

  • (false)


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

def default?
  false
end

#failure(input, error) ⇒ Failure

Returns:

  • (Failure)


83
84
85
# File 'lib/dry/types/definition.rb', line 83

def failure(input, error)
  Result::Failure.new(input, error)
end

#nameString

Returns:

  • (String)


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

def name
  primitive.name
end

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

Checks whether value is of a #primitive class

Parameters:

  • value (Object)

Returns:

  • (Boolean)


97
98
99
# File 'lib/dry/types/definition.rb', line 97

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

#result(klass, *args) ⇒ Object

Returns new instance of the given +klass+.

Parameters:

  • klass (Object)

    class of the result instance

  • args (Array)

    arguments for the +klass#initialize+ method

Returns:

  • (Object)

    new instance of the given +klass+



90
91
92
# File 'lib/dry/types/definition.rb', line 90

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

#success(input) ⇒ Success

Returns:

  • (Success)


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

def success(input)
  Result::Success.new(input)
end

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

Parameters:

  • input (Object)
  • block (#call)

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:



65
66
67
68
69
70
71
72
# File 'lib/dry/types/definition.rb', line 65

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