Class: SequentialFile::Base
- Inherits:
-
Object
- Object
- SequentialFile::Base
- Includes:
- Namer
- Defined in:
- lib/sequential_file/base.rb
Overview
This is a reference implementation of a File-wrapper class that uses the Namer to determine which file to process
Instance Attribute Summary collapse
-
#buffer_limit ⇒ Object
Returns the value of attribute buffer_limit.
-
#buffer_position ⇒ Object
Returns the value of attribute buffer_position.
-
#buffer_size ⇒ Object
Returns the value of attribute buffer_size.
-
#complete_path ⇒ Object
the complete path to the file.
-
#file ⇒ Object
Returns the value of attribute file.
-
#permission_style ⇒ Object
Returns the value of attribute permission_style.
-
#read_position ⇒ Object
the byte position in the file where read stopped and monitor will begin.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #clear ⇒ Object
- #close ⇒ Object
- #delete ⇒ Object
- #file_handle ⇒ Object
- #flush_if_buffer_full ⇒ Object
- #info_string ⇒ Object
-
#initialize(options = {}, &block) ⇒ Base
constructor
A new instance of Base.
- #num_lines ⇒ Object
-
#read(&block) ⇒ Object
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.
-
#write(msg) ⇒ Object
Need to remember to close file with self.close after done using the file descriptor.
Methods included from Namer
Constructor Details
#initialize(options = {}, &block) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 20 21 |
# File 'lib/sequential_file/base.rb', line 14 def initialize( = {}, &block) yield if block_given? @complete_path ||= File.join(self.directory_path, self.name) @buffer_limit = [:buffer_limit] || 200 # bytes @buffer_size = 0 = [:permission_style] || File::RDWR|File::CREAT @verbose = !![:verbose] end |
Instance Attribute Details
#buffer_limit ⇒ Object
Returns the value of attribute buffer_limit.
12 13 14 |
# File 'lib/sequential_file/base.rb', line 12 def buffer_limit @buffer_limit end |
#buffer_position ⇒ Object
Returns the value of attribute buffer_position.
12 13 14 |
# File 'lib/sequential_file/base.rb', line 12 def buffer_position @buffer_position end |
#buffer_size ⇒ Object
Returns the value of attribute buffer_size.
12 13 14 |
# File 'lib/sequential_file/base.rb', line 12 def buffer_size @buffer_size end |
#complete_path ⇒ Object
the complete path to the file
9 10 11 |
# File 'lib/sequential_file/base.rb', line 9 def complete_path @complete_path end |
#file ⇒ Object
Returns the value of attribute file.
11 12 13 |
# File 'lib/sequential_file/base.rb', line 11 def file @file end |
#permission_style ⇒ Object
Returns the value of attribute permission_style.
11 12 13 |
# File 'lib/sequential_file/base.rb', line 11 def end |
#read_position ⇒ Object
the byte position in the file where read stopped and monitor will begin.
10 11 12 |
# File 'lib/sequential_file/base.rb', line 10 def read_position @read_position end |
#verbose ⇒ Object
Returns the value of attribute verbose.
12 13 14 |
# File 'lib/sequential_file/base.rb', line 12 def verbose @verbose end |
Instance Method Details
#clear ⇒ Object
70 71 72 73 74 |
# 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 ⇒ Object
64 65 66 67 68 |
# 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 ⇒ Object
76 77 78 79 80 81 |
# File 'lib/sequential_file/base.rb', line 76 def delete if file close # just in case File.delete(complete_path) end end |
#file_handle ⇒ Object
27 28 29 30 31 |
# 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, , {autoclose: false}) self.file ||= File.new(@complete_path, , {autoclose: false}) end |
#flush_if_buffer_full ⇒ Object
46 47 48 49 50 51 |
# 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 ⇒ Object
23 24 25 |
# File 'lib/sequential_file/base.rb', line 23 def info_string "PERM: #{@permission_style}\n\tPATH: #{@complete_path}" end |
#num_lines ⇒ Object
33 34 35 36 |
# File 'lib/sequential_file/base.rb', line 33 def num_lines file_handle.seek(0, IO::SEEK_SET) file_handle.readlines.size end |
#read(&block) ⇒ Object
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
55 56 57 58 59 60 61 62 |
# 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) ⇒ Object
Need to remember to close file with self.close after done using the file descriptor
39 40 41 42 43 44 |
# 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 |