Class: XmlSerializer

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

Class Method Summary collapse

Class Method Details

.hash_to_object(hash) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/lab5.rb', line 28

def self.hash_to_object(hash)
  object = hash['class'].constantize.new

  hash['instance_variables'].each do |name, value|
    object.instance_variable_set(name, value)
  end

  object
end

.object_to_hash(object) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lab5.rb', line 14

def self.object_to_hash(object)
  variable_names = object.instance_variables
  variables = {}

  variable_names.each do |name|
    variables[name] = object.instance_variable_get(name)
  end

  {
    class: object.class.name,
    instance_variables: variables
  }
end

.xml_ser(array) ⇒ Object



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

def self.xml_ser(array)
  Ox.dump(XmlSerializer.object_to_hash(array))
end

.yaml_deser(xml) ⇒ Object



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

def self.yaml_deser(xml)
  Ox.parse(xml)
end