forked from yamldb/yaml_db
/
yaml_db.rb
67 lines (52 loc) · 1.33 KB
/
yaml_db.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'rubygems'
require 'yaml'
require 'active_record'
require 'serialization_helper'
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