Class: Camille::BasicType
- Inherits:
-
Object
- Object
- Camille::BasicType
show all
- Defined in:
- lib/camille/basic_type.rb
Overview
This class specifies the methods available for all types includeing built-in and custom ones.
Direct Known Subclasses
PickAndOmit, Type, Types::Any, Types::Array, Types::Boolean, Types::BooleanLiteral, Types::Intersection, Types::Null, Types::Number, Types::NumberLiteral, Types::Object, Types::Record, Types::String, Types::StringLiteral, Types::Tuple, Types::Undefined, Types::Union
Defined Under Namespace
Modules: CheckRendered
Classes: InvalidTypeError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of BasicType.
24
25
26
|
# File 'lib/camille/basic_type.rb', line 24
def initialize
@fingerprint = Digest::MD5.hexdigest self.class.name
end
|
Instance Attribute Details
#fingerprint ⇒ Object
Returns the value of attribute fingerprint.
22
23
24
|
# File 'lib/camille/basic_type.rb', line 22
def fingerprint
@fingerprint
end
|
Class Method Details
.&(other) ⇒ Object
52
53
54
|
# File 'lib/camille/basic_type.rb', line 52
def self.& other
Camille::Type.instance(self) & other
end
|
.[] ⇒ Object
56
57
58
|
# File 'lib/camille/basic_type.rb', line 56
def self.[]
Camille::Type.instance(self)[]
end
|
.directly_instantiable? ⇒ Boolean
60
61
62
|
# File 'lib/camille/basic_type.rb', line 60
def self.directly_instantiable?
instance_method(:initialize).arity == 0
end
|
.inherited(klass) ⇒ Object
64
65
66
|
# File 'lib/camille/basic_type.rb', line 64
def self.inherited klass
klass.prepend CheckRendered
end
|
.instance(value) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/camille/basic_type.rb', line 68
def self.instance value
case
when value.is_a?(::Hash)
Camille::Types::Object.new(value)
when value.is_a?(::Array)
Camille::Types::Tuple.new(value)
when value.is_a?(Integer) || value.is_a?(Float)
Camille::Types::NumberLiteral.new(value)
when value.is_a?(::String)
Camille::Types::StringLiteral.new(value)
when value == true || value == false
Camille::Types::BooleanLiteral.new(value)
when value.is_a?(Camille::BasicType)
value
when value.is_a?(Class) && value < Camille::BasicType && value.directly_instantiable?
value.new
else
raise InvalidTypeError.new("#{value} cannot be converted to a type instance.")
end
end
|
.|(other) ⇒ Object
48
49
50
|
# File 'lib/camille/basic_type.rb', line 48
def self.| other
Camille::Type.instance(self) | other
end
|
Instance Method Details
#&(other) ⇒ Object
32
33
34
|
# File 'lib/camille/basic_type.rb', line 32
def & other
Camille::Types::Intersection.new(self, other)
end
|
#[] ⇒ Object
36
37
38
|
# File 'lib/camille/basic_type.rb', line 36
def []
Camille::Types::Array.new(self)
end
|
#check(value) ⇒ Object
40
41
42
|
# File 'lib/camille/basic_type.rb', line 40
def check value
raise NotImplementedError
end
|
44
45
46
|
# File 'lib/camille/basic_type.rb', line 44
def transform value
value
end
|
#|(other) ⇒ Object
28
29
30
|
# File 'lib/camille/basic_type.rb', line 28
def | other
Camille::Types::Union.new(self, other)
end
|