Class: FileConnector
- Inherits:
-
Object
- Object
- FileConnector
- Includes:
- LockManager
- Defined in:
- lib/file_connector.rb
Instance Method Summary collapse
-
#initialize(name, directory) ⇒ FileConnector
constructor
A new instance of FileConnector.
- #read ⇒ Object
- #write(hash) ⇒ Object
Methods included from LockManager
#grab_ex_lock, #grab_sh_lock, #release_lock
Constructor Details
#initialize(name, directory) ⇒ FileConnector
Returns a new instance of FileConnector.
9 10 11 12 13 |
# File 'lib/file_connector.rb', line 9 def initialize(name, directory) @file_path = directory + name.to_s + '.json' create_file unless if_exist @file = File.open(@file_path, File::RDWR) end |
Instance Method Details
#read ⇒ Object
15 16 17 18 |
# File 'lib/file_connector.rb', line 15 def read file = File.read(@file_path) JSON.parse(file) end |
#write(hash) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/file_connector.rb', line 20 def write(hash) File.open(@file_path, 'w') do |f| f.write(hash.to_json) end read end |