Class: Camille::Type

Inherits:
BasicType show all
Defined in:
lib/camille/type.rb

Overview

This class represents all custom types defined by the user.

Defined Under Namespace

Classes: NotImplementedError

Instance Attribute Summary collapse

Attributes inherited from BasicType

#fingerprint

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicType

&, #&, #[], [], directly_instantiable?, instance, #transform, #|, |

Constructor Details

#initializeType

Returns a new instance of Type.



8
9
10
# File 'lib/camille/type.rb', line 8

def initialize
  raise NotImplementedError.new("Missing `alias_of` definition for type.")
end

Instance Attribute Details

#underlyingObject (readonly)

Returns the value of attribute underlying.



6
7
8
# File 'lib/camille/type.rb', line 6

def underlying
  @underlying
end

Class Method Details

.alias_of(type) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/camille/type.rb', line 12

def self.alias_of type
  underlying = Camille::Type.instance(type)
  fingerprint = underlying.fingerprint

  define_method(:initialize) do
    @underlying = underlying
    @fingerprint = fingerprint
  end
end

.inherited(klass) ⇒ Object



44
45
46
# File 'lib/camille/type.rb', line 44

def self.inherited klass
  Camille::Loader.loaded_types << klass
end

.klass_nameObject



36
37
38
# File 'lib/camille/type.rb', line 36

def self.klass_name
  name.gsub(/^Camille::Types::/, '')
end

.test(value) ⇒ Object



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

def self.test value
  new.test value
end

Instance Method Details

#check(value) ⇒ Object



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

def check value
  normalized = transform value
  @underlying.check normalized
end

#literalObject



40
41
42
# File 'lib/camille/type.rb', line 40

def literal
  self.class.klass_name.gsub(/::/, '_')
end

#test(value) ⇒ Object



27
28
29
30
# File 'lib/camille/type.rb', line 27

def test value
  result = check value
  result.type_error? ? result : nil
end