Class: Ambry::Adapters::File

Inherits:
Ambry::Adapter show all
Defined in:
lib/ambry/adapters/file.rb

Overview

Loads and saves hash database from a Marshal.dump file.

Direct Known Subclasses

YAML

Instance Attribute Summary (collapse)

Attributes inherited from Ambry::Adapter

#db, #name

Instance Method Summary (collapse)

Methods inherited from Ambry::Adapter

#db_for

Constructor Details

- (File) initialize(options)



9
10
11
12
13
# File 'lib/ambry/adapters/file.rb', line 9

def initialize(options)
  @file_path = options[:file]
  @lock      = Mutex.new
  super
end

Instance Attribute Details

- (Object) file_path (readonly)

Returns the value of attribute file_path



6
7
8
# File 'lib/ambry/adapters/file.rb', line 6

def file_path
  @file_path
end

- (Object) lock (readonly)

Returns the value of attribute lock



7
8
9
# File 'lib/ambry/adapters/file.rb', line 7

def lock
  @lock
end

Instance Method Details

- (Object) export_data



23
24
25
# File 'lib/ambry/adapters/file.rb', line 23

def export_data
  Marshal.dump(db)
end

- (Object) import_data



27
28
29
# File 'lib/ambry/adapters/file.rb', line 27

def import_data
  Marshal.load(::File.read(file_path))
end

- (Object) load_database



15
16
17
18
19
20
21
# File 'lib/ambry/adapters/file.rb', line 15

def load_database
  @db = import_data
  @db.blank? ? @db = {} : @db.map(&:freeze)
rescue Errno::ENOENT
  # @TODO warn via logger when file doesn't exist
  @db = {}
end

- (Object) save_database



31
32
33
34
35
# File 'lib/ambry/adapters/file.rb', line 31

def save_database
  @lock.synchronize do
    ::File.open(file_path, "w") {|f| f.write(export_data)}
  end
end