Class: MPLight::Types

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

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs) ⇒ Types

Returns a new instance of Types.



15
16
17
18
19
# File 'lib/mplight.rb', line 15

def initialize *args, **kwargs
  @cls = {}
  @ids = {}
  register -1, Time
end

Instance Method Details

#additional_dataObject



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

def additional_data
  []
end

#create(id, data) ⇒ Object



36
37
38
39
40
# File 'lib/mplight.rb', line 36

def create id, data
  c = @cls[ id]
  c or raise Error, "Unregistered type id: #{obj.class}"
  c.from_mpdata data, *additional_data
end

#default_to_string!Object



34
# File 'lib/mplight.rb', line 34

def default_to_string! ; @default_to_string = true ; end

#dump(obj) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/mplight.rb', line 25

def dump obj
  t = @ids[ obj.class]
  unless t then
    return if @default_to_string
    raise Error, "Unregistered class: #{obj.class}"
  end
  [ t, obj.to_mpdata]
end

#register(id, klass) ⇒ Object



21
22
23
# File 'lib/mplight.rb', line 21

def register id, klass
  @cls[ id], @ids[ klass] = klass, id
end