Sha256: e3bea9cbf7a9cea4ef100231a1b7f2f05b62634b19b6ce12c99d125407835b0b

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module YamlDb
  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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yaml_db-0.7.0 lib/yaml_db/csv_db.rb
yaml_db-0.6.0 lib/yaml_db/csv_db.rb
yaml_db-0.5.0 lib/yaml_db/csv_db.rb
yaml_db-0.4.2 lib/yaml_db/csv_db.rb
yaml_db-0.4.0 lib/yaml_db/csv_db.rb