Module: Gamefic::Snapshot

Defined in:
lib/gamefic/snapshot.rb

Overview

Save and restore plots.

Class Method Summary collapse

Class Method Details

.match?(plot, snapshot) ⇒ Boolean

True if the plot’s state matches the snapshot.

Parameters:

Returns:

  • (Boolean)


40
41
42
# File 'lib/gamefic/snapshot.rb', line 40

def self.match?(plot, snapshot)
  save(plot) == snapshot
end

.restore(snapshot) ⇒ Plot

Restore a plot from a base64-encoded string.

Parameters:

Returns:



25
26
27
28
29
30
31
32
33
34
# File 'lib/gamefic/snapshot.rb', line 25

def self.restore snapshot
  binary = Base64.decode64(snapshot)
  Marshal.load(binary).tap do |plot|
    plot.hydrate
    # @todo Opal marshal dumps are not idempotent
    next if RUBY_ENGINE == 'opal' || match?(plot, snapshot)

    Logging.logger.warn "Scripts modified #{plot.class} data. Snapshot may not have restored properly"
  end
end

.save(plot) ⇒ String

Save a base64-encoded snapshot of a plot.

Parameters:

Returns:



14
15
16
17
18
19
# File 'lib/gamefic/snapshot.rb', line 14

def self.save plot
  cache = plot.detach
  binary = Marshal.dump(plot)
  plot.attach cache
  Base64.encode64(binary)
end