Class: File

Inherits:
Object show all
Includes:
Jinx::Collection
Defined in:
lib/jinx/helpers/collections.rb,
lib/jinx/helpers/file_separator.rb

Instance Method Summary collapse

Methods included from Jinx::Collection

#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union

Instance Method Details

#content_line_separatorObject (private)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jinx/helpers/file_separator.rb', line 41

def content_line_separator
  begin
    saved_pos = pos  # remember where we were
    # read a chunk until a separator is discovered
    sep = discover_line_separator
    # tricky seek() clone to work around GzipReader's lack of seek()
    rewind
    # reset back to the remembered position
    chunks, residual = saved_pos.divmod(1024)
    chunks.times { read(1024) }
    read(residual)
  rescue IOError  # stream not opened for reading
  end
  sep
end

#default_line_separatorObject (private)

Returns the default line separator. The logic is borrowed from the FasterCVS gem.



26
27
28
# File 'lib/jinx/helpers/file_separator.rb', line 26

def default_line_separator
  @def_line_sep ||= infer_line_separator
end

#discover_line_separatorObject (private)



57
58
59
60
61
62
63
64
# File 'lib/jinx/helpers/file_separator.rb', line 57

def discover_line_separator
  # read a chunk until a separator is discovered
  while (sample = read(1024)) do
    sample += read(1) if sample[-1, 1] == "\r" and not eof?
    # try to find a standard separator
    return $& if sample =~ /\r\n?|\n/
  end
end

#infer_line_separatorObject (private)



30
31
32
# File 'lib/jinx/helpers/file_separator.rb', line 30

def infer_line_separator
  type_line_separator or content_line_separator or $/
end

#type_line_separatorObject (private)



34
35
36
37
38
39
# File 'lib/jinx/helpers/file_separator.rb', line 34

def type_line_separator
  if [ARGF, STDIN, STDOUT, STDERR].include?(self) or
    (defined?(Zlib) and self.class == Zlib::GzipWriter) then
    return $/
  end
end