class SequentialFile::Base
This is a reference implementation of a File-wrapper class that uses the Namer
to determine which file to process
Attributes
buffer_limit[RW]
buffer_position[RW]
buffer_size[RW]
complete_path[RW]
file[RW]
permission_style[RW]
read_position[RW]
verbose[RW]
Public Class Methods
new(options = {}) { || ... }
click to toggle source
# File lib/sequential_file/base.rb, line 14 def initialize(options = {}, &block) yield if block_given? @complete_path ||= File.join(self.directory_path, self.name) @buffer_limit = options[:buffer_limit] || 200 # bytes @buffer_size = 0 @permission_style = options[:permission_style] || File::RDWR|File::CREAT @verbose = !!options[:verbose] end
Public Instance Methods
clear()
click to toggle source
# File lib/sequential_file/base.rb, line 70 def clear file_handle.truncate 0 # Set the file to nil, so it will be reopened when needed. close end
close()
click to toggle source
# File lib/sequential_file/base.rb, line 64 def close file_handle.close # Set the file to nil, so it will be reopened when needed. self.file = nil end
delete()
click to toggle source
# File lib/sequential_file/base.rb, line 76 def delete if file close # just in case File.delete(complete_path) end end
file_handle()
click to toggle source
# File lib/sequential_file/base.rb, line 27 def file_handle # autoclose: false => Keep the file descriptor open until we want to close it. File.new(@complete_path, @permission_style, {autoclose: false}) self.file ||= File.new(@complete_path, @permission_style, {autoclose: false}) end
flush_if_buffer_full()
click to toggle source
# File lib/sequential_file/base.rb, line 46 def flush_if_buffer_full if buffer_size >= buffer_limit file_handle.flush @buffer_size = 0 end end
info_string()
click to toggle source
# File lib/sequential_file/base.rb, line 23 def info_string "PERM: #{@permission_style}\n\tPATH: #{@complete_path}" end
num_lines()
click to toggle source
# File lib/sequential_file/base.rb, line 33 def num_lines file_handle.seek(0, IO::SEEK_SET) file_handle.readlines.size end
read() { |line| ... }
click to toggle source
Read file from beginning to end, recording position when done Need to remember to close file with self.close after done using the file descriptor
# File lib/sequential_file/base.rb, line 55 def read &block prep_read data = block_given? ? file_handle.each {|line| yield line } : file_handle.read @read_position = file_handle.pos data end
write(msg)
click to toggle source
Need to remember to close file with self.close after done using the file descriptor
# File lib/sequential_file/base.rb, line 39 def write(msg) prep_write puts "#-------> @[ #{@file_handle.pos} ] B[ #{@buffer_size} ] in #{@complete_path} <---------#\n#{msg}" if verbose add_to_buffer(msg) flush_if_buffer_full end
Protected Instance Methods
add_to_buffer(msg)
click to toggle source
# File lib/sequential_file/base.rb, line 92 def add_to_buffer(msg) @buffer_size += msg.length file_handle.write(msg) end
prep_read()
click to toggle source
# File lib/sequential_file/base.rb, line 88 def prep_read file_handle.flock(File::LOCK_SH) # a shared lock on the file. file_handle.seek(0, IO::SEEK_SET) end
prep_write()
click to toggle source
# File lib/sequential_file/base.rb, line 84 def prep_write file_handle.flock(File::LOCK_EX) # an exclusive lock on the file. file_handle.seek(0, IO::SEEK_END) end