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

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

Constructor Details

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

Returns a new instance of Definition.

Parameters:

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


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

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

Instance Attribute Details

#optionsHash (readonly)

Returns:



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

def options
  @options
end

#primitiveClass (readonly)

Returns:

  • (Class)


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

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Type

Parameters:

  • primitive (Class)

Returns:



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

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)


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

def call(input)
  input
end

#constrained?false

Returns:

  • (false)


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

def constrained?
  false
end

#default?false

Returns:

  • (false)


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

def default?
  false
end

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

Returns:



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

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

#nameString

Returns:

  • (String)


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

def name
  primitive.name
end

#optional?false

Returns:

  • (false)


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

def optional?
  false
end

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

Checks whether value is of a #primitive class

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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

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+



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

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

#success(input) ⇒ Result::Success

Returns:



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

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

#to_ast(meta: true) ⇒ Array

Return AST representation of a type definition

Returns:



115
116
117
# File 'lib/dry/types/definition.rb', line 115

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



72
73
74
75
76
77
78
79
# File 'lib/dry/types/definition.rb', line 72

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