Class: SequentialFile::Base

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Namer

included

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(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

Instance Attribute Details

#buffer_limitObject

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_positionObject

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_sizeObject

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_pathObject

the complete path to the file



9
10
11
# File 'lib/sequential_file/base.rb', line 9

def complete_path
  @complete_path
end

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/sequential_file/base.rb', line 11

def file
  @file
end

#permission_styleObject

Returns the value of attribute permission_style.



11
12
13
# File 'lib/sequential_file/base.rb', line 11

def permission_style
  @permission_style
end

#read_positionObject

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

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/sequential_file/base.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#clearObject



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

#closeObject



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

#deleteObject



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_handleObject



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, @permission_style, {autoclose: false})
  self.file ||= File.new(@complete_path, @permission_style, {autoclose: false})
end

#flush_if_buffer_fullObject



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_stringObject



23
24
25
# File 'lib/sequential_file/base.rb', line 23

def info_string
  "PERM: #{@permission_style}\n\tPATH: #{@complete_path}"
end

#num_linesObject



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