Class: Playoffs::Loader

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/playoffs/loader.rb

Overview

Knows how to (de)serialize a tournament and read/write from/to disk.

Instance Method Summary collapse

Instance Method Details

#deserialize(string) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/playoffs/loader.rb', line 19

def deserialize(string)
  YAML.load(
    string,
    permitted_classes: [
      BestOf,
      Round,
      Series,
      Team,
      Tournament
    ],
    aliases: true
  )
end

#load(path) ⇒ Object



43
44
45
# File 'lib/playoffs/loader.rb', line 43

def load(path)
  deserialize(File.read(path))
end

#save(path, tournament) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/playoffs/loader.rb', line 34

def save(path, tournament)
  FileUtils.mkdir_p(File.dirname(path))

  File.write(path, serialize(tournament))

  self
end

#serialize(tournament) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/playoffs/loader.rb', line 10

def serialize(tournament)
  io = StringIO.new

  YAML.dump(tournament, io)

  io.string
end