Sha256: 03eed8c41a467fae0cf22a4632fe533282ef8bc76350ae09a60cc3dac2990811

Contents?: true

Size: 1.67 KB

Versions: 27

Compression:

Stored size: 1.67 KB

Contents

#require 'FasterCSV'
module CsvDb
  module Helper
    def self.loader
      Load
    end

    def self.dumper
      Dump
    end

    def self.extension
      "csv"
    end
  end

  class Load < SerializationHelper::Load
    def self.load_documents(io, truncate = true)
      tables = {}
      curr_table = nil
      io.each do |line|
        if /BEGIN_CSV_TABLE_DECLARATION(.+)END_CSV_TABLE_DECLARATION/ =~ line
          curr_table = $1
          tables[curr_table] = {}
        else
          if tables[curr_table]["columns"]
            tables[curr_table]["records"] << FasterCSV.parse(line)[0]
          else
            tables[curr_table]["columns"] = FasterCSV.parse(line)[0]
            tables[curr_table]["records"] = []
          end
        end
      end

      tables.each_pair do |table_name, contents|
        load_table(table_name, contents, truncate)
      end
    end
  end

  class Dump < SerializationHelper::Dump

    def self.before_table(io,table)
      io.write "BEGIN_CSV_TABLE_DECLARATION#{table}END_CSV_TABLE_DECLARATION\n"
    end

    def self.dump(io)
      tables.each do |table|
        before_table(io, table)
        dump_table(io, table)
        after_table(io, table)
      end
    end

    def self.after_table(io,table)
      io.write ""
    end

    def self.dump_table_columns(io, table)
      io.write(table_column_names(table).to_csv)
    end

    def self.dump_table_records(io, table)

      column_names = table_column_names(table)

      each_table_page(table) do |records|
        rows = SerializationHelper::Utils.unhash_records(records, column_names)
        records.each do |record|
          io.write(record.to_csv)
        end
      end
    end
    
  end


end

Version data entries

27 entries across 27 versions & 11 rubygems

Version Path
yaml_db-0.3.0 lib/csv_db.rb
self_systeem-0.1.0 lib/csv_db.rb
mfpiccolo_yaml_db-0.2.3 lib/csv_db.rb
yaml_db_with_schema_tables-0.2.4 lib/csv_db.rb
irm_yaml_db-0.0.4 lib/csv_db.rb
irm_yaml_db-0.0.3 lib/csv_db.rb
irm_yaml_db-0.0.2 lib/csv_db.rb
irm_yaml_db-0.0.1 lib/csv_db.rb
mtwtfss_yaml_db-0.5 lib/csv_db.rb
keeguon-yaml_db-0.2.4 lib/csv_db.rb
gitlab_yaml_db-1.0.0 lib/csv_db.rb
fm_yaml_db-0.2.5 lib/csv_db.rb
fm_yaml_db-0.2.4 lib/csv_db.rb
yaml_db-0.2.3 lib/csv_db.rb
yaml_db_arel-0.2.2 lib/csv_db.rb
yaml_db-0.2.2 lib/csv_db.rb
yaml_db-0.2.1 lib/csv_db.rb
molo-0.7.6 vendor/yaml_db/lib/csv_db.rb
molo-0.7.5 vendor/yaml_db/lib/csv_db.rb
molo-0.7.4 vendor/yaml_db/lib/csv_db.rb