Class: Dry::Data::Type

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

Direct Known Subclasses

Array, Hash, Optional

Defined Under Namespace

Classes: Array, 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.



36
37
38
39
# File 'lib/dry/data/type.rb', line 36

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

Instance Attribute Details

#constructorObject (readonly)

Returns the value of attribute constructor.



10
11
12
# File 'lib/dry/data/type.rb', line 10

def constructor
  @constructor
end

#primitiveObject (readonly)

Returns the value of attribute primitive.



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

def primitive
  @primitive
end

Class Method Details

.[](primitive) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/dry/data/type.rb', line 14

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

.passthrough_constructor(input) ⇒ Object



32
33
34
# File 'lib/dry/data/type.rb', line 32

def self.passthrough_constructor(input)
  input
end

.strict_constructor(primitive, input) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/dry/data/type.rb', line 24

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



45
46
47
# File 'lib/dry/data/type.rb', line 45

def call(input)
  constructor[input]
end

#nameObject



41
42
43
# File 'lib/dry/data/type.rb', line 41

def name
  primitive.name
end

#valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/dry/data/type.rb', line 50

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

#|(other) ⇒ Object



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

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