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

- old
+ new

@@ -39,13 +39,10 @@ else fields end end - def self.encapsulate_persistence(file, options) - end - def initialize(file = {}, type = nil, options = {}) # Process Options if Hash === type options = type @@ -56,11 +53,11 @@ if String === file and file =~/(.*?)#(.*)/ and File.exists? $1 options = Misc.add_defaults options, Misc.string2hash($2) file = $1 end - options = Misc.add_defaults options, :persistence => false, :type => type + options = Misc.add_defaults options, :persistence => false, :type => type, :in_situ_persistence => true # Extract Filename file, extra = file if Array === file and file.length == 2 and Hash === file.last @@ -112,53 +109,82 @@ if @data.respond_to?(key.to_sym) and self.respond_to?("#{key}=".to_sym) self.send "#{key}=".to_sym, @data.send(key.to_sym) end end else - @data, extra = Persistence.persist(file, :TSV, :tsv_extra, options) do |file, options, filename| + 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 - case - ## Parse source - when Resource::Path === file #(String === file and file.respond_to? :open) - data, extra = TSV.parse(file.open(:grep => options[:grep]) , options) - extra[:namespace] ||= file.namespace - extra[:datadir] ||= file.datadir - when StringIO === file - data, extra = TSV.parse(file, options) - when Open.can_open?(file) - Open.open(file, :grep => options[:grep]) do |f| - data, extra = TSV.parse(f, options) - end - when File === file - path = file.path - file = Open.grep(file, options[:grep]) if options[:grep] - data, extra = TSV.parse(file, options) - when IO === file - file = Open.grep(file, options[:grep]) if options[:grep] - data, extra = TSV.parse(file, options) - when block_given? - data - else - raise "Unknown input in TSV.new #{file.inspect}" + 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) end - extra[:filename] = filename + begin + case + ## Parse source + when Resource::Path === file #(String === file and file.respond_to? :open) + data, extra = TSV.parse(file.open(:grep => options[:grep]) , options) + extra[:namespace] ||= file.namespace + extra[:datadir] ||= file.datadir + when StringIO === file + data, extra = TSV.parse(file, options) + when Open.can_open?(file) + Open.open(file, :grep => options[:grep]) do |f| + data, extra = TSV.parse(f, options) + end + when File === file + path = file.path + file = Open.grep(file, options[:grep]) if options[:grep] + data, extra = TSV.parse(file, options) + when IO === file + file = Open.grep(file, options[:grep]) if options[:grep] + data, extra = TSV.parse(file, options) + when block_given? + data + else + raise "Unknown input in TSV.new #{file.inspect}" + end + extra[:filename] = filename + rescue Exception + FileUtils.rm persistence_file if persistence_file and File.exists?(persistence_file) + raise $! + end + [data, extra] end end end - if not extra.nil? + if not extra.nil? %w(case_insensitive namespace identifiers datadir fields key_field type filename cast).each do |key| if extra.include? key.to_sym self.send("#{key}=".to_sym, extra[key.to_sym]) - if @data.respond_to? "#{key}=".to_sym - @data.send("#{key}=".to_sym, extra[key.to_sym]) - end + #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