Class: DpStmMap::JsonMessage

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_from_string(str) ⇒ Object



31
32
33
34
35
# File 'lib/dp_stm_map/Message.rb', line 31

def self.class_from_string(str)
  str.split('::').inject(Object) do |mod, class_name|
    mod.const_get(class_name)
  end
end

.deserialize(string) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/dp_stm_map/Message.rb', line 20

def self.deserialize string
  # return YAML::load(string)

  json=JSON.parse(string)
  type=json.delete('type')
  cls=class_from_string(type)
  obj=cls.new
  obj.from_hash! json
  obj
end

Instance Method Details

#from_hash!(hash) ⇒ Object



47
48
49
50
51
# File 'lib/dp_stm_map/Message.rb', line 47

def from_hash! hash
    hash.each do |var, val|
        self.instance_variable_set var, val
    end
end

#serializeObject



37
38
39
40
41
42
43
44
45
# File 'lib/dp_stm_map/Message.rb', line 37

def serialize
    # return YAML::dump(self)
    hash = {}
    self.instance_variables.each do |var|
        hash[var] = self.instance_variable_get var
    end
    hash['type']=self.class.name
    hash.to_json
end