Sha256: a497ec46ce7ccb536cc9324dd6763fe0f0f3e82cf676b87c311db29305449852

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

#Copied and modified from yamldb gem (https://github.com/ludicast/yaml_db)
#
# Credits:
# Created by Orion Henry and Adam Wiggins.
# Major updates by Ricardo Chimal, Jr.
# Patches contributed by Michael Irwin, Tom Locke, and Tim Galeckas.

require 'yaml'
require 'active_record'
require 'fidius-common/yamldb/serialization_helper'
require 'active_support/core_ext/kernel/reporting'

module YamlDb
  module Helper
    def self.loader
      YamlDb::Load
    end

    def self.dumper
      YamlDb::Dump
    end

    def self.extension
      "yml"
    end
  end


  module Utils
    def self.chunk_records(records)
      yaml = [ records ].to_yaml
      yaml.sub!("--- \n", "")
      yaml.sub!('- - -', '  - -')
      yaml
    end
  end

  class Dump < SerializationHelper::Dump

    def self.dump_table_columns(io, table)
      io.write("\n")
      io.write({ table => { 'columns' => table_column_names(table) } }.to_yaml)
    end

    def self.dump_table_records(io, table)
      table_record_header(io)

      column_names = table_column_names(table)

      each_table_page(table) do |records|
        rows = SerializationHelper::Utils.unhash_records(records, column_names)
        io.write(YamlDb::Utils.chunk_records(records))
      end
    end

    def self.table_record_header(io)
      io.write("  records: \n")
    end

  end

  class Load < SerializationHelper::Load
    def self.load_documents(io, truncate = true)
      YAML.load_documents(io) do |ydoc|
        ydoc.keys.each do |table_name|
          next if ydoc[table_name].nil?
          load_table(table_name, ydoc[table_name], truncate)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fidius-common-0.0.6 lib/fidius-common/yamldb/yaml_db.rb
fidius-common-0.0.5 lib/fidius-common/yamldb/yaml_db.rb
fidius-common-0.0.4beta0 lib/fidius-common/yamldb/yaml_db.rb