Class: Dry::Types::Definition

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

Direct Known Subclasses

Array, Constructor, Hash, Map

Instance Attribute Summary collapse

Attributes included from Options

#options

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, #pristine, #with

Constructor Details

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

Returns a new instance of Definition.

Parameters:

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


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

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

Instance Attribute Details

#primitiveClass (readonly)

Returns:

  • (Class)


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

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Type

Parameters:

  • primitive (Class)

Returns:



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

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

Instance Method Details

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

Parameters:

  • input (BasicObject)

Returns:

  • (BasicObject)


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

def call(input)
  input
end

#constrained?false

Returns:

  • (false)


47
48
49
# File 'lib/dry/types/definition.rb', line 47

def constrained?
  false
end

#default?false

Returns:

  • (false)


42
43
44
# File 'lib/dry/types/definition.rb', line 42

def default?
  false
end

#failure(input, error) ⇒ Result::Failure

Returns:



86
87
88
# File 'lib/dry/types/definition.rb', line 86

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

#nameString

Returns:

  • (String)


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

def name
  primitive.name
end

#optional?false

Returns:

  • (false)


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

def optional?
  false
end

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

Checks whether value is of a #primitive class

Parameters:

  • value (Object)

Returns:

  • (Boolean)


93
94
95
# File 'lib/dry/types/definition.rb', line 93

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

#success(input) ⇒ Result::Success

Returns:



80
81
82
# File 'lib/dry/types/definition.rb', line 80

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

#to_ast(meta: true) ⇒ Array

Return AST representation of a type definition

Returns:



104
105
106
# File 'lib/dry/types/definition.rb', line 104

def to_ast(meta: true)
  [:definition, [primitive, meta ? self.meta : EMPTY_HASH]]
end

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

Parameters:

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

Yield Parameters:

  • failure (Failure)

Yield Returns:

Returns:

  • (Result, Logic::Result)

    when a block is not provided

  • (nil)

    otherwise



69
70
71
72
73
74
75
76
# File 'lib/dry/types/definition.rb', line 69

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