Class: SerializationHelper::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(helper, filter_table_names = nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(helper, filter_table_names = nil)
  @dumper = helper.dumper
  @loader = helper.loader
  @extension = helper.extension
  @dumper.filter_table_names = filter_table_names if filter_table_names
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



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

def extension
  @extension
end

Instance Method Details

#disable_loggerObject



50
51
52
53
# File 'lib/serialization_helper.rb', line 50

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

#dump(filename) ⇒ Object



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

def dump(filename)
  disable_logger
  @dumper.dump(File.new(filename, "w"))
  reenable_logger
end

#dump_to_dir(dirname) ⇒ Object



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

def dump_to_dir(dirname)
  Dir.mkdir(dirname)
  tables = @dumper.tables

  unless @dumper.filter_table_names.blank?
    tables = tables.grep(Regexp.new(@dumper.filter_table_names))
  end

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

#load(filename, truncate = true) ⇒ Object



35
36
37
38
39
# File 'lib/serialization_helper.rb', line 35

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

#load_from_dir(dirname, truncate = true) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/serialization_helper.rb', line 41

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



55
56
57
# File 'lib/serialization_helper.rb', line 55

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