Class: InstanceAccountant::Filer

Inherits:
Object
  • Object
show all
Defined in:
lib/instance_accountant/filer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = { }) ⇒ Filer

Returns a new instance of Filer.



5
6
7
8
9
10
# File 'lib/instance_accountant/filer.rb', line 5

def initialize options = { }
  @file_klass = options[:file_klass] || File
  @time_klass = options[:time_klass] || Time

  @filepath = @file_klass.expand_path options[:filepath]
end

Instance Method Details

#readObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/instance_accountant/filer.rb', line 12

def read
  hour_string = nil

  @file_klass.open( @filepath, 'r' ) do |file|
    file.flock File::LOCK_EX
    hour_string = file.read
  end

  @time_klass.parse hour_string
rescue
  nil
end

#write(hour) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/instance_accountant/filer.rb', line 25

def write hour
  hour_string = hour.hour_string

  @file_klass.open( @filepath,
                    File::RDWR | File::CREAT,
                    0644 ) do |file|
    file.flock File::LOCK_EX
    file.truncate 0
    file.write hour_string
  end

  hour_string
end