Module: SuperModel::Marshal

Extended by:
Marshal
Included in:
Marshal
Defined in:
lib/supermodel/marshal.rb

Defined Under Namespace

Modules: Model

Instance Method Summary collapse

Instance Method Details

#dumpObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/supermodel/marshal.rb', line 29

def dump
  return unless path
  tmp_file = Tempfile.new("rbdump")
  tmp_file.binmode
  records  = klasses.map {|k| k.records }.flatten
  ::Marshal.dump(records, tmp_file)
  # Atomic serialization - so we never corrupt the db
  FileUtils.mv(tmp_file.path, path)
  true
end

#klassesObject



14
15
16
# File 'lib/supermodel/marshal.rb', line 14

def klasses
  @klasses ||= []
end

#loadObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/supermodel/marshal.rb', line 18

def load
  return unless path
  return unless File.exist?(path)
  records = []
  File.open(path, "rb") {|file|
    records = ::Marshal.load(file)
  }
  records.each {|r| r.class.records << r }
  true
end

#pathObject



6
7
8
# File 'lib/supermodel/marshal.rb', line 6

def path
  @path || raise("Provide a path")
end

#path=(p) ⇒ Object



10
11
12
# File 'lib/supermodel/marshal.rb', line 10

def path=(p)
  @path = p
end