Class: Bloomfilter::Serialization::File

Inherits:
Object
  • Object
show all
Defined in:
lib/bloomfilter/serialization/file.rb

Instance Method Summary collapse

Instance Method Details

#load(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bloomfilter/serialization/file.rb', line 34

def load(path)
  return nil unless ::File.exist?(path)
  
  ::File.open(path, 'r') do |f|
    begin
      @loaded_file = Marshal.load(f) unless f.size == 0
    rescue => e
      @loaded_file = nil
    end
  end
  @loaded_file
end

#store(path, filter) ⇒ Object



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
# File 'lib/bloomfilter/serialization/file.rb', line 6

def store(path, filter)
  dir = ::File.dirname(path)
  unless ::File.directory?(dir)
    begin
      FileUtils.mkdir_p(dir)
    rescue => e
      $stderr.puts "Exception raised when trying to create directory: #{path} - #{e.message}"
    end
  end

  unless ::File.directory?(dir)
    $stderr.puts "Unable to create the directory #{dir}. Trying again."
    begin
      FileUtils.mkdir_p(dir)
    rescue => e
      $stderr.puts "Exception raised when trying to create directory: #{path} - #{e.message}"
    end
  end        
  
  unless ::File.directory?(dir)
    $stderr.puts "#{dir} still doesn't exist. Giving up for now."
  else
    ::File.open(path, 'w') do |f|
      Marshal.dump(filter, f)
    end
  end
end