Class: Norman::Adapters::File
- Inherits:
-
Norman::Adapter
- Object
- Norman::Adapter
- Norman::Adapters::File
- Defined in:
- lib/norman/adapters/file.rb
Overview
Loads and saves hash database from a Marshal.dump file.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#lock ⇒ Object
readonly
Returns the value of attribute lock.
Attributes inherited from Norman::Adapter
Instance Method Summary collapse
- #export_data ⇒ Object
- #import_data ⇒ Object
-
#initialize(options) ⇒ File
constructor
A new instance of File.
- #load_database ⇒ Object
- #save_database ⇒ Object
Methods inherited from Norman::Adapter
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() @file_path = [:file] @lock = Mutex.new super end |
Instance Attribute Details
#file_path ⇒ Object (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 |
#lock ⇒ Object (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_data ⇒ Object
23 24 25 |
# File 'lib/norman/adapters/file.rb', line 23 def export_data Marshal.dump(db) end |
#import_data ⇒ Object
27 28 29 |
# File 'lib/norman/adapters/file.rb', line 27 def import_data Marshal.load(::File.read(file_path)) end |
#load_database ⇒ Object
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_database ⇒ Object
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 |