lab5 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lab5.rb +27 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 136e37999c33b5cadfff5d2c18e6008bcd34b29b
|
4
|
+
data.tar.gz: 64c1834a7c8e73f4685ff6a8ad34acd5a920767d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91e1a7558a2997522fd79fb1dc756324979a45e348cff83a775e537f11da4a5b0a09fa24a4448ee617964b90bce3e7ca351c31c28d6f09c9e48e831774755268
|
7
|
+
data.tar.gz: 93d19eaa2855b56c1c0310733adb94828c63c7ea8bf57dd8b3c0694ad9760bc284dfae027248d51acaf1078886162cadb2b986a8ca2708c6a81d1cb5eb608bdd
|
data/lib/lab5.rb
CHANGED
@@ -2,10 +2,36 @@ require 'ox'
|
|
2
2
|
|
3
3
|
class XmlSerializer
|
4
4
|
def self.xml_ser(array)
|
5
|
-
Ox.dump(array)
|
5
|
+
Ox.dump(XmlSerializer.object_to_hash(array))
|
6
6
|
end
|
7
7
|
|
8
8
|
def self.yaml_deser(xml)
|
9
9
|
Ox.parse(xml)
|
10
10
|
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def self.object_to_hash(object)
|
15
|
+
variable_names = object.instance_variables
|
16
|
+
variables = {}
|
17
|
+
|
18
|
+
variable_names.each do |name|
|
19
|
+
variables[name] = object.instance_variable_get(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
{
|
23
|
+
class: object.class.name,
|
24
|
+
instance_variables: variables
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.hash_to_object(hash)
|
29
|
+
object = hash['class'].constantize.new
|
30
|
+
|
31
|
+
hash['instance_variables'].each do |name, value|
|
32
|
+
object.instance_variable_set(name, value)
|
33
|
+
end
|
34
|
+
|
35
|
+
object
|
36
|
+
end
|
11
37
|
end
|