Class: Dry::Data::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/data/type.rb,
lib/dry/data/type/enum.rb,
lib/dry/data/type/hash.rb,
lib/dry/data/type/array.rb,
lib/dry/data/type/optional.rb

Direct Known Subclasses

Array, Constrained, Hash, Optional

Defined Under Namespace

Classes: Array, Constrained, Enum, Hash, Optional

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor, primitive) ⇒ Type

Returns a new instance of Type.



57
58
59
60
# File 'lib/dry/data/type.rb', line 57

def initialize(constructor, primitive)
  @constructor = constructor
  @primitive = primitive
end

Instance Attribute Details

#constructorObject (readonly)

Returns the value of attribute constructor.



12
13
14
# File 'lib/dry/data/type.rb', line 12

def constructor
  @constructor
end

#primitiveObject (readonly)

Returns the value of attribute primitive.



13
14
15
# File 'lib/dry/data/type.rb', line 13

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/dry/data/type.rb', line 35

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

.passthrough_constructor(input) ⇒ Object



53
54
55
# File 'lib/dry/data/type.rb', line 53

def self.passthrough_constructor(input)
  input
end

.strict_constructor(primitive, input) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/dry/data/type.rb', line 45

def self.strict_constructor(primitive, input)
  if input.is_a?(primitive)
    input
  else
    raise TypeError, "#{input.inspect} has invalid type"
  end
end

Instance Method Details

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



74
75
76
# File 'lib/dry/data/type.rb', line 74

def call(input)
  constructor[input]
end

#constrained(options) ⇒ Object



62
63
64
# File 'lib/dry/data/type.rb', line 62

def constrained(options)
  Constrained.new(constructor, primitive, Data.Rule(primitive, options))
end

#enum(*values) ⇒ Object



66
67
68
# File 'lib/dry/data/type.rb', line 66

def enum(*values)
  Enum.new(values, constrained(inclusion: values))
end

#nameObject



70
71
72
# File 'lib/dry/data/type.rb', line 70

def name
  primitive.name
end

#valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/dry/data/type.rb', line 79

def valid?(input)
  input.is_a?(primitive)
end

#|(other) ⇒ Object



83
84
85
# File 'lib/dry/data/type.rb', line 83

def |(other)
  Data.SumType(self, other)
end