class Ccp::Persistent::Tsv

Public Class Methods

ext() click to toggle source
# File lib/ccp/persistent/tsv.rb, line 3
def self.ext
  "tsv"
end

Public Instance Methods

[]=(key, val) click to toggle source
Calls superclass method Ccp::Persistent::Dir#[]=
# File lib/ccp/persistent/tsv.rb, line 73
def []=(key, val)
  # Now, tsv can manage only hash
  case val
  when Hash
    tsv_path_for(key).open("w+"){|f| save_tsv(f, val)}
  else
    super
  end
end
files() click to toggle source
# File lib/ccp/persistent/tsv.rb, line 83
def files
  Dir["#{path}/*.#{ext}"] + Dir["#{path}/*.#{ext}.#{self.class.ext}"]
end
keys() click to toggle source
# File lib/ccp/persistent/tsv.rb, line 87
def keys
  files.map{|i| File.basename(i).split(".").first}.sort
end
load!(key) click to toggle source
Calls superclass method Ccp::Persistent::Dir#load!
# File lib/ccp/persistent/tsv.rb, line 62
def load!(key)
  path = path_for(key)
  if path.exist?
    super
  elsif (tsv = tsv_path_for(key)).exist?
    load_tsv(tsv)
  else
    super
  end
end
load_tsv(path) click to toggle source
# File lib/ccp/persistent/tsv.rb, line 7
def load_tsv(path)
  hash = {}
  prev = []

  path.readlines.each_with_index do |line, i|
    no = i+1
    key, val = line.split(/\t/,2)

    if val
      # flush previous line
      if prev.size == 2
        hash[prev[0]] = decode(prev[1])
        prev = []
      end

      # push prev line
      prev = [key, val]

    else
      # maybe sequencial lines for one value
      if prev.size == 2
        prev[1] = prev[1] + "\n" + key

      else
        $stderr.puts "#{self.class}#load_tsv: value not found. key='#{key}' (line: #{no})"
        next
      end
    end
  end

  # flush last line
  if prev.size == 2
    hash[prev[0]] = decode(prev[1])
  end

  return hash
end
save_tsv(f, hash) click to toggle source
# File lib/ccp/persistent/tsv.rb, line 45
def save_tsv(f, hash)
  keys = hash.keys
  keys =
    case keys.first
    when NilClass ; return
    when Symbol   ; keys
    when /\A\d+\Z/; keys.sort_by(&:to_i)
    when String   ; keys.sort
    else          ; keys
    end

  keys.each do |key|
    value = encode(hash[key]).sub(/\n+\Z/m, '') # strip last LF for human-readability
    f.puts "%s\t%s\n" % [key, value]
  end
end
truncate() click to toggle source
# File lib/ccp/persistent/tsv.rb, line 91
def truncate
  files.each{|file| File.unlink(file)}
end
tsv_path_for(key) click to toggle source
# File lib/ccp/persistent/tsv.rb, line 95
def tsv_path_for(key)
  Pathname(path_for(key).to_s + ".tsv")
end