Class: Camille::BasicType

Inherits:
Object
  • Object
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.

Defined Under Namespace

Modules: CheckRendered Classes: InvalidTypeError, RenderError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBasicType

Returns a new instance of BasicType.



25
26
27
# File 'lib/camille/basic_type.rb', line 25

def initialize
  @fingerprint = Digest::MD5.hexdigest self.class.name
end

Instance Attribute Details

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



23
24
25
# File 'lib/camille/basic_type.rb', line 23

def fingerprint
  @fingerprint
end

Class Method Details

.&(other) ⇒ Object



66
67
68
# File 'lib/camille/basic_type.rb', line 66

def self.& other
  Camille::Type.instance(self) & other
end

.[]Object



70
71
72
# File 'lib/camille/basic_type.rb', line 70

def self.[]
  Camille::Type.instance(self)[]
end

.check_params(value) ⇒ Object



74
75
76
# File 'lib/camille/basic_type.rb', line 74

def self.check_params value
  Camille::Type.instance(self).check_params value
end

.directly_instantiable?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/camille/basic_type.rb', line 78

def self.directly_instantiable?
  instance_method(:initialize).arity == 0
end

.inherited(klass) ⇒ Object



82
83
84
# File 'lib/camille/basic_type.rb', line 82

def self.inherited klass
  klass.prepend CheckRendered
end

.instance(value) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/camille/basic_type.rb', line 86

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



62
63
64
# File 'lib/camille/basic_type.rb', line 62

def self.| other
  Camille::Type.instance(self) | other
end

Instance Method Details

#&(other) ⇒ Object



46
47
48
# File 'lib/camille/basic_type.rb', line 46

def & other
  Camille::Types::Intersection.new(self, other)
end

#[]Object



50
51
52
# File 'lib/camille/basic_type.rb', line 50

def []
  Camille::Types::Array.new(self)
end

#check(value) ⇒ Object

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/camille/basic_type.rb', line 54

def check value
  raise NotImplementedError
end

#check_params(value) ⇒ Object



58
59
60
# File 'lib/camille/basic_type.rb', line 58

def check_params value
  check value
end

#render!(value) ⇒ Object

Checks value and returns a Camille::Rendered, raising RenderError with the printed type error if the check fails.



31
32
33
34
35
36
37
38
39
40
# File 'lib/camille/basic_type.rb', line 31

def render! value
  result = check(value)
  if result.type_error?
    string_io = StringIO.new
    Camille::TypeErrorPrinter.new(result).print(string_io)
    raise RenderError.new("\nType check failed for render!.\n#{string_io.string}")
  else
    result.render
  end
end

#|(other) ⇒ Object



42
43
44
# File 'lib/camille/basic_type.rb', line 42

def | other
  Camille::Types::Union.new(self, other)
end