Class: FileIOManager

Inherits:
Object
  • Object
show all
Defined in:
lib/internal/iomanager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_dir, positions = 12) ⇒ FileIOManager

Returns a new instance of FileIOManager.



7
8
9
# File 'lib/internal/iomanager.rb', line 7

def initialize( target_dir, positions = 12 )
  @target_dir, @positions = target_dir, positions
end

Instance Attribute Details

#positionsObject (readonly)

Returns the value of attribute positions.



5
6
7
# File 'lib/internal/iomanager.rb', line 5

def positions
  @positions
end

#target_dirObject (readonly)

Returns the value of attribute target_dir.



5
6
7
# File 'lib/internal/iomanager.rb', line 5

def target_dir
  @target_dir
end

Instance Method Details

#create_new_stream(extension) ⇒ Object



11
12
13
14
# File 'lib/internal/iomanager.rb', line 11

def create_new_stream( extension )
  file_name = obtain_next_available( extension )
  create_write_stream( file_name )
end

#list(extension) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/internal/iomanager.rb', line 16

def list( extension )
  unless File.exist?( @target_dir )
    return []
  end
  dir = File.join( @target_dir, "*.#{extension}" )
  valid_entries = Dir.entries( @target_dir ).select do |entry|
    entry =~ /^\d{#{positions}}\.#{extension}$/ 
  end
  valid_entries.sort
end

#obtain_latest_read_stream(extension) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/internal/iomanager.rb', line 38

def obtain_latest_read_stream( extension )
  files = list( extension )
  return nil if files.length == 0
  
  file_name = File.join( @target_dir, files.max )
  create_read_stream( file_name )
end

#obtain_ordered_read_streams(extension) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/internal/iomanager.rb', line 27

def obtain_ordered_read_streams( extension )
  streams = []
  
  list( extension ).each do |file|
    file_name = File.join( @target_dir, file )
    streams << create_read_stream( file_name )
  end
  
  streams
end