Class: InstanceAccountant::Filer
- Inherits:
-
Object
- Object
- InstanceAccountant::Filer
- Defined in:
- lib/instance_accountant/filer.rb
Instance Method Summary collapse
-
#initialize(options = { }) ⇒ Filer
constructor
A new instance of Filer.
- #read ⇒ Object
- #write(hour) ⇒ Object
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 = { } @file_klass = [:file_klass] || File @time_klass = [:time_klass] || Time @filepath = @file_klass. [:filepath] end |
Instance Method Details
#read ⇒ Object
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 |