class IOHelpers::Reader

Public Class Methods

file(path, options = {}) click to toggle source
# File lib/io_helpers/reader.rb, line 3
def self.file(path, options = {})
  line_sep = options[:line_sep] || "\n"
  lines = File.readlines(path, line_sep, chomp: true)
  map_lines(lines, options)
end
map_element(element, as, mapper) click to toggle source
# File lib/io_helpers/reader.rb, line 30
def self.map_element(element, as, mapper)
  if mapper
    mapper.call(element)
  else
    element.method("to_#{as}").call
  end
end
map_lines(lines, options) click to toggle source
# File lib/io_helpers/reader.rb, line 19
def self.map_lines(lines, options)
  col_sep = options[:col_sep]
  as = options[:as] || 's'
  mapper = options[:mapper]
  if col_sep
    lines.map { |line| line.split(col_sep).map { |col| map_element(col, as, mapper) } }
  else
    lines.map { |line| map_element(line, as, mapper) }
  end
end
stream(path) click to toggle source
# File lib/io_helpers/reader.rb, line 15
def self.stream(path)
  IO.foreach(path).lazy
end
string(str, options = {}) click to toggle source
# File lib/io_helpers/reader.rb, line 9
def self.string(str, options = {})
  line_sep = options[:line_sep] || "\n"
  lines = str.split(line_sep)
  map_lines(lines, options)
end