-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton_pattern.rb
More file actions
45 lines (35 loc) · 900 Bytes
/
singleton_pattern.rb
File metadata and controls
45 lines (35 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'singleton'
require 'pry'
class SingletonPattern
include Singleton
attr_accessor :file_type, :content
def _dump(depth)
puts "Dumping the file content...."
Marshal.dump(@content, depth)
end
def self._load(str)
puts "Loading the dumped content...."
instance.content = Marshal.load(str)
instance
end
def read_file(file_type)
puts "Reading #{file_type} file...."
@content = File.read("test.#{file_type}")
end
end
boro = Boro.instance
# Process JSON data
boro.read_file('json')
stored_state = Marshal.dump(boro)
boro_json = Marshal.load(stored_state)
puts boro_json.content
# Process XML
boro_json.read_file('xml')
stored_state = Marshal.dump(boro)
boro_xml = Marshal.load(stored_state)
puts boro_xml.content
# Process XML
boro_xml.read_file('yml')
stored_state = Marshal.dump(boro)
boro_yml = Marshal.load(stored_state)
puts boro_yml.content