Class: YamlDb::SerializationHelper::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_db/serialization_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(helper) ⇒ Base

Returns a new instance of Base.


7
8
9
10
11
# File 'lib/yaml_db/serialization_helper.rb', line 7

def initialize(helper)
  @dumper    = helper.dumper
  @loader    = helper.loader
  @extension = helper.extension
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension


5
6
7
# File 'lib/yaml_db/serialization_helper.rb', line 5

def extension
  @extension
end

Instance Method Details

#disable_loggerObject


48
49
50
51
# File 'lib/yaml_db/serialization_helper.rb', line 48

def disable_logger
  @@old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil
end

#dump(filename) ⇒ Object


13
14
15
16
17
18
19
# File 'lib/yaml_db/serialization_helper.rb', line 13

def dump(filename)
  disable_logger
  File.open(filename, "w") do |file|
    @dumper.dump(file)
  end
  reenable_logger
end

#dump_to_dir(dirname) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/yaml_db/serialization_helper.rb', line 21

def dump_to_dir(dirname)
  Dir.mkdir(dirname)
  tables = @dumper.tables
  tables.each do |table|
    File.open("#{dirname}/#{table}.#{@extension}", "w") do |io|
      @dumper.before_table(io, table)
      @dumper.dump_table io, table
      @dumper.after_table(io, table)
    end
  end
end

#load(filename, truncate = true) ⇒ Object


33
34
35
36
37
# File 'lib/yaml_db/serialization_helper.rb', line 33

def load(filename, truncate = true)
  disable_logger
  @loader.load(File.new(filename, "r"), truncate)
  reenable_logger
end

#load_from_dir(dirname, truncate = true) ⇒ Object


39
40
41
42
43
44
45
46
# File 'lib/yaml_db/serialization_helper.rb', line 39

def load_from_dir(dirname, truncate = true)
  Dir.entries(dirname).each do |filename|
    if filename =~ /^[.]/
      next
    end
    @loader.load(File.new("#{dirname}/#{filename}", "r"), truncate)
  end
end

#reenable_loggerObject


53
54
55
# File 'lib/yaml_db/serialization_helper.rb', line 53

def reenable_logger
  ActiveRecord::Base.logger = @@old_logger
end