lib/rbbt/util/tsv.rb in rbbt-util-3.1.0 vs lib/rbbt/util/tsv.rb in rbbt-util-3.2.0

- old
+ new

@@ -12,12 +12,32 @@ require 'rbbt/util/tsv/accessor' require 'rbbt/util/tsv/manipulate' require 'rbbt/util/tsv/index' require 'rbbt/util/tsv/attach' require 'rbbt/util/tsv/resource' + class TSV + ESCAPES = { + "\n" => "[[NL]]", + "\t" => "[[TAB]]", + } + + def self.escape(text) + ESCAPES.each do |char,replacement| + text = text.gsub(char, replacement) + end + text + end + + def self.unescape(text) + ESCAPES.each do |char,replacement| + text = text.gsub(replacement, char) + end + text + end + def self.headers(file, options = {}) ## Remove options from filename if String === file and file =~/(.*?)#(.*)/ and File.exists? $1 options = Misc.add_defaults options, Misc.string2hash($2) @@ -89,22 +109,28 @@ when block_given? @data, extra = Persistence.persist(file, :TSV, :tsv_extra, options.merge(:force_array => true)) do |file, options, filename| yield file, options, filename end extra.each do |key, values| self.send("#{ key }=".to_sym, values) if self.respond_to? "#{ key }=".to_sym end if not extra.nil? - + else case when Array === file @data = Hash[file.collect{|v| [v,[]] }] + @data.key_field = key_field if key_field + @data.fields = fields if fields when Hash === file @data = file + @data.key_field = key_field if key_field + @data.fields = fields if fields when TSV === file @data = file.data + @data.key_field = key_field if key_field + @data.fields = fields if fields when Persistence::TSV === file @data = file %w(case_insensitive namespace identifiers datadir fields key_field type filename cast).each do |key| if @data.respond_to?(key.to_sym) and self.respond_to?("#{key}=".to_sym) self.send "#{key}=".to_sym, @data.send(key.to_sym) @@ -114,29 +140,11 @@ in_situ_persistence = Misc.process_options(options, :in_situ_persistence) @data, extra = Persistence.persist(file, :TSV, :tsv_extra, options) do |file, options, filename, persistence_file| data, extra = nil if in_situ_persistence and persistence_file - - cast = options[:cast] - type = options[:type] - serializer = case - when ((cast == "to_i" or cast == :to_i) and type == :single) - :integer - when ((cast == "to_i" or cast == :to_i) and (type == :flat or type == :list)) - :integer_array - when type == :double - :double - when type == :list - :list - when type == :single - :single - else - :marshal - end - - options.merge! :persistence_data => Persistence::TSV.get(persistence_file, true, serializer) + options.merge! :persistence_data => Persistence::TSV.get(persistence_file, true, :double) end begin case ## Parse source @@ -167,10 +175,21 @@ rescue Exception FileUtils.rm persistence_file if persistence_file and File.exists?(persistence_file) raise $! end + if Persistence::TSV === data + %w(case_insensitive namespace identifiers datadir fields key_field type filename cast).each do |key| + if extra.include? key.to_sym + if data.respond_to? "#{key}=".to_sym + data.send("#{key}=".to_sym, extra[key.to_sym]) + end + end + end + data.read + end + [data, extra] end end end @@ -181,18 +200,22 @@ #if @data.respond_to? "#{key}=".to_sym # @data.send("#{key}=".to_sym, extra[key.to_sym]) #end end end - @data.read if Persistence::TSV === @data end end def write @data.write if @data.respond_to? :write end def read @data.read if @data.respond_to? :read end + + def write? + @data.write? if @data.respond_to? :write + end + end