Class: FileConnector

Inherits:
Object
  • Object
show all
Includes:
LockManager
Defined in:
lib/file_connector.rb

Class Method Summary collapse

Instance Method Summary collapse

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 FileConnector.exist(name, directory)
  @file = File.open(@file_path, File::RDWR)
end

Class Method Details

.exist(name, directory) ⇒ Object



27
28
29
30
# File 'lib/file_connector.rb', line 27

def self.exist(name, directory)
  file_path = directory + name.to_s + '.json'
  File.file?(file_path)
end

Instance Method Details

#readObject



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