Class: Norman::Adapters::File

Inherits:
Norman::Adapter show all
Defined in:
lib/norman/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 Norman::Adapter

#db, #name

Instance Method Summary collapse

Methods inherited from Norman::Adapter

#db_for

Constructor Details

#initialize(options) ⇒ File

Returns a new instance of File.



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

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

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



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

def file_path
  @file_path
end

#lockObject (readonly)

Returns the value of attribute lock.



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

def lock
  @lock
end

Instance Method Details

#export_dataObject



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

def export_data
  Marshal.dump(db)
end

#import_dataObject



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

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

#load_databaseObject



15
16
17
18
19
20
21
# File 'lib/norman/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

#save_databaseObject



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

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