Class: CryptoconditionsRuby::TypeRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptoconditions_ruby/type_registry.rb

Constant Summary collapse

MAX_SAFE_INTEGER_JS =
2 ** 53 - 1

Class Method Summary collapse

Class Method Details

.get_class_from_type_id(type_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cryptoconditions_ruby/type_registry.rb', line 8

def self.get_class_from_type_id(type_id)
  if type_id > MAX_SAFE_INTEGER_JS
    raise TypeError.new("Type #{type_id} is not supported")
  end

  type = registered_types.find do |registered_type|
    type_id == registered_type['type_id']
  end

  if type
    type['class']
  else
    raise TypeError.new("Type #{type_id} is not supported")
  end
end

.register_type(cls) ⇒ Object



24
25
26
# File 'lib/cryptoconditions_ruby/type_registry.rb', line 24

def self.register_type(cls)
  registered_types.push({'type_id' => cls::TYPE_ID, 'class' => cls})
end

.registered_typesObject



4
5
6
# File 'lib/cryptoconditions_ruby/type_registry.rb', line 4

def self.registered_types
  @registered_types ||=[]
end