Class: SerializationHelper::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/fidius-common/yamldb/serialization_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(helper, config_filename, db_entry) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
25
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 19

def initialize(helper, config_filename, db_entry)
  @dumper = helper.dumper
  @loader = helper.loader
  @extension = helper.extension

  establish_connection(config_filename, db_entry)
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



17
18
19
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 17

def extension
  @extension
end

Instance Method Details

#disable_loggerObject



104
105
106
107
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 104

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

#dump(target_dir, timestamp) ⇒ Object

Added creation of directories with timestamps.

Parameters:

  • path (String)

    to target dir

  • true, (Boolean)

    if timestamp should be added



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 71

def dump(target_dir, timestamp)
  unless target_dir.empty? and !Dir.exists?(target_dir)
    target_dir += '/'
  end
  dir = target_dir + @db_entry
  if timestamp
    timestamp = Time.now
    dir += "_#{timestamp.strftime("%Y-%m-%d_%H%M%S")}"
  end

  Dir.mkdir dir
  @data_dir = File.expand_path(dir)
  @data_filename = "#{@data_dir}/#{@db_entry}.#{@extension}"

  disable_logger
  dump_schema
  @dumper.dump(File.new(@data_filename, "w"))
  reenable_logger
  @data_dir
end

#dump_schemaObject

copied and modified activerecord-3.0.6/lib/active_record/railties/database.rake



52
53
54
55
56
57
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 52

def dump_schema
  require 'active_record/schema_dumper'
  File.open(File.join(@data_dir ,'schema.rb'), "w") do |file|
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  end
end

#establish_connection(yml_file, db_entry) ⇒ Object

Set configuration for the ActiveRecord connection.

Parameters:

  • path (String)

    to yaml configuration file

  • name (String)

    of the db entry in the configuration file



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 31

def establish_connection(yml_file, db_entry)
  if yml_file.class == String
    raise "#{yml_file} does not exist" unless File.exists? File.expand_path(yml_file)
    yml_config = YAML.load(File.read(yml_file))
    db_config = yml_config[db_entry]
  elsif yml_file.class == Hash
    # also react on connection settings given as hash
    db_config = yml_file
  else
    raise "please input string or hash"
  end
  unless db_config
    raise "No entry '#{db_entry}' found in #{yml_file}"
  else
    @db_entry = db_entry
    ActiveRecord::Base.establish_connection db_config
    ActiveRecord::Base.connection
  end
end

#load(import_dir, truncate = true) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 92

def load(import_dir ,truncate = true)
  if Dir.exists? import_dir
    @data_dir = import_dir
    disable_logger
    load_schema
    @loader.load(File.new("#{@data_dir}/#{@db_entry}.#{@extension}", "r"), truncate)
    reenable_logger
  else
    puts "#{import_dir} does not exist!"
  end
end

#load_schemaObject



59
60
61
62
63
64
65
66
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 59

def load_schema
  file = File.join(@data_dir ,'schema.rb')
  if File.exists?(file)
    ActiveSupport::Dependencies::Loadable.load(file)
  else
    puts "#{file} does not exist"
  end
end

#reenable_loggerObject



109
110
111
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 109

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