module Lingo::TextUtils

Constants

DEFAULT_MODE
GZIP_RE
STDIN_EXT
STDOUT_EXT

Private Instance Methods

_require_lib(lib) click to toggle source
# File lib/lingo/text_utils.rb, line 175
def _require_lib(lib)
  respond_to?(:require_lib, true) ? require_lib(lib) : require(lib)
end
_yield_obj(obj) { |obj| ... } click to toggle source
# File lib/lingo/text_utils.rb, line 179
def _yield_obj(obj)
  !block_given? ? obj : begin
    yield obj
  ensure
    obj.close
  end
end
agree?(msg) click to toggle source
# File lib/lingo/text_utils.rb, line 59
def agree?(msg)
  print "#{msg} (y/n) [n]: "

  case stdin.gets.chomp
    when /\Ano?\z/i, ''  then nil
    when /\Ay(?:es)?\z/i then true
    else puts 'Please enter "yes" or "no".'; agree?(msg)
  end
rescue Interrupt
  abort ''
end
bom_encoding(mode = 'r', encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 166
def bom_encoding(mode = 'r', encoding = nil)
  encoding = get_encoding(encoding)

  encoding && (mode.include?('r') || mode.include?('+')) &&
    encoding.name.start_with?('UTF-') ? "BOM|#{encoding}" : encoding
end
foreach(path, encoding = nil) { |line| ... } click to toggle source
# File lib/lingo/text_utils.rb, line 133
def foreach(path, encoding = nil)
  open(path, nil, encoding) { |io|
    io.each { |line| line.chomp!; yield line } }
end
foreach_csv(path, options = {}, encoding = nil, &block) click to toggle source
# File lib/lingo/text_utils.rb, line 138
def foreach_csv(path, options = {}, encoding = nil, &block)
  open_csv(path, nil, options, encoding) { |csv| csv.each(&block) }
end
get_encoding(encoding = nil, iv = :@encoding) click to toggle source
# File lib/lingo/text_utils.rb, line 161
def get_encoding(encoding = nil, iv = :@encoding)
  encoding ||
    (instance_variable_defined?(iv) ? instance_variable_get(iv) : nil)
end
get_path(path, ext) click to toggle source
# File lib/lingo/text_utils.rb, line 142
def get_path(path, ext)
  set_ext(path, ext).format { |directive|
    case directive
      when 'd', t = 't' then Time.now.strftime(t ? '%H%M%S' : '%Y%m%d')
      when 'c', l = 'l' then File.chomp_ext(File.basename(
        lingo.config.send("#{l ? :lang : :config}_file")))
    end
  }
end
open(path, mode = nil, encoding = nil, &block) click to toggle source
# File lib/lingo/text_utils.rb, line 79
def open(path, mode = nil, encoding = nil, &block)
  mode ||= DEFAULT_MODE

  _yield_obj(case mode
    when /r/ then stdin?(path) ? open_stdin(encoding) : File.exist?(path) ?
      open_path(path, mode, encoding) : raise(FileNotFoundError.new(path))
    when /w/ then stdout?(path) ? open_stdout(encoding) : overwrite?(path) ?
      open_path(path, mode, encoding) : raise(FileExistsError.new(path))
  end, &block)
end
open_csv(path, mode = nil, options = {}, encoding = nil, &block) click to toggle source
# File lib/lingo/text_utils.rb, line 90
def open_csv(path, mode = nil, options = {}, encoding = nil, &block)
  _require_lib('csv')

  open(path, mode, encoding) { |io|
    _yield_obj(CSV.new(io, options), &block) }
end
open_file(path, mode = nil, encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 114
def open_file(path, mode = nil, encoding = nil)
  File.open(path, mode ||= DEFAULT_MODE,
    encoding: bom_encoding(mode, encoding))
end
open_gzip(path, mode = nil, encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 119
def open_gzip(path, mode = nil, encoding = nil)
  _require_lib('zlib')

  case mode ||= DEFAULT_MODE
    when 'r', 'rb'
      @progress = false
      Zlib::GzipReader
    when 'w', 'wb'
      Zlib::GzipWriter
    else
      raise ArgumentError, 'invalid access mode %s' % mode
  end.open(path, encoding: get_encoding(encoding))
end
open_path(path, mode = nil, encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 106
def open_path(path, mode = nil, encoding = nil)
  mode ||= DEFAULT_MODE

  path =~ GZIP_RE ?
    open_gzip(path, mode, encoding) :
    open_file(path, mode, encoding)
end
open_stdin(encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 97
def open_stdin(encoding = nil)
  io = set_encoding(stdin, encoding)
  @progress ? StringIO.new(io.read) : io
end
open_stdout(encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 102
def open_stdout(encoding = nil)
  set_encoding(stdout, encoding)
end
overwrite?(path, unlink = false) click to toggle source
# File lib/lingo/text_utils.rb, line 52
def overwrite?(path, unlink = false)
  !File.exist?(path) || if agree?("#{path} already exists. Overwrite?")
    File.unlink(path) if unlink
    true
  end
end
set_encoding(io, encoding = nil) click to toggle source
# File lib/lingo/text_utils.rb, line 156
def set_encoding(io, encoding = nil)
  io.set_encoding(get_encoding(encoding))
  io
end
set_ext(path, ext) click to toggle source
# File lib/lingo/text_utils.rb, line 152
def set_ext(path, ext)
  File.set_ext(path.sub(GZIP_RE, ''), ".#{ext}")
end
stdin() click to toggle source
# File lib/lingo/text_utils.rb, line 71
def stdin
  respond_to?(:lingo, true) ? lingo.config.stdin : $stdin
end
stdin?(path) click to toggle source
# File lib/lingo/text_utils.rb, line 44
def stdin?(path)
  STDIN_EXT.include?(path)
end
stdout() click to toggle source
# File lib/lingo/text_utils.rb, line 75
def stdout
  respond_to?(:lingo, true) ? lingo.config.stdout : $stdout
end
stdout?(path) click to toggle source
# File lib/lingo/text_utils.rb, line 48
def stdout?(path)
  STDOUT_EXT.include?(path)
end