lib/rbbt/persist.rb in rbbt-util-4.0.0 vs lib/rbbt/persist.rb in rbbt-util-4.0.1
- old
+ new
@@ -2,10 +2,11 @@
require 'rbbt/util/misc'
require 'rbbt/util/open'
require 'rbbt/persist/tsv'
+require 'set'
module Persist
CACHEDIR="/tmp/tsv_persistent_cache"
FileUtils.mkdir CACHEDIR unless File.exist? CACHEDIR
@@ -65,14 +66,17 @@
filename << ":" << options_md5 unless options_md5.empty?
File.join(persistence_dir, filename)
end
+ TRUE_STRINGS = Set.new ["true", "True", "TRUE", "t", "T", "1", "yes", "Yes", "YES", "y", "Y", "ON", "on"]
def self.load_file(path, type)
case (type || "nil").to_sym
when :nil
nil
+ when :boolean
+ TRUE_STRINGS.include? Open.read(path).chomp.strip
when :tsv
TSV.open(path)
when :marshal_tsv
TSV.setup(Marshal.load(Open.open(path)))
when :fwt
@@ -103,18 +107,20 @@
return if (content.nil? and File.exists? path)
case (type || "nil").to_sym
when :nil
nil
+ when :boolean
+ Open.write(path, content ? "true" : "false")
when :fwt
content.file.seek 0
Open.write(path, content.file.read)
when :tsv
Open.write(path, content.to_s)
when :string, :text
Open.write(path, content)
when :array
- Open.write(path, content * "\n" + "\n")
+ Open.write(path, content * "\n")
when :marshal_tsv
Open.write(path, Marshal.dump(content.dup))
when :marshal
Open.write(path, Marshal.dump(content))
when :yaml