Class: DBGeni::Base

Inherits:
Object
  • Object
show all
Includes:
DBGeni::BaseModules::Code
Defined in:
lib/dbgeni/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DBGeni::BaseModules::Code

#apply_all_code, #apply_code, #apply_list_of_code, #apply_outstanding_code, #code, #current_code, #list_of_code, #outstanding_code, #remove_all_code, #remove_code, #remove_list_of_code

Constructor Details

#initialize(config_file) ⇒ Base

Returns a new instance of Base.

[View source]

42
43
44
45
# File 'lib/dbgeni/base.rb', line 42

def initialize(config_file)
  load_config(config_file)
  initialize_logger
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (private)

[View source]

135
136
137
138
139
140
141
142
143
# File 'lib/dbgeni/base.rb', line 135

def method_missing(meth, *args, &blk)
  if meth =~ /migration/
    delegate_to_migration_cli(meth.intern, *args)
  elsif meth =~ /dml/
    delegate_to_dml_cli(meth.to_s.gsub(/dml/,'migration').intern, *args)
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.


25
26
27
# File 'lib/dbgeni/base.rb', line 25

def config
  @config
end

Class Method Details

.installer_for_environment(config_file, environment_name = nil) ⇒ Object

[View source]

32
33
34
35
36
37
38
39
40
# File 'lib/dbgeni/base.rb', line 32

def self.installer_for_environment(config_file, environment_name=nil)
  installer = self.new(config_file)
  # If environment is nil, then it assumes there is only a single environment
  # defined. So pass the nil value to select_environment - if there is more than
  # one environment then select_environment will error out after making a call
  # to get_environment.
  installer.select_environment(environment_name)
  installer
end

Instance Method Details

#connectObject

[View source]

84
85
86
87
88
89
# File 'lib/dbgeni/base.rb', line 84

def connect
  raise DBGeni::NoEnvironmentSelected unless selected_environment_name
  return @connection if @connection

  @connection = DBGeni::Connector.initialize(@config)
end

#connectionObject

[View source]

98
99
100
# File 'lib/dbgeni/base.rb', line 98

def connection
  @connection ||= connect
end

#database_initialized?Boolean

Returns:

  • (Boolean)
[View source]

106
107
108
# File 'lib/dbgeni/base.rb', line 106

def database_initialized?
  DBGeni::Initializer.initialized?(connection, @config)
end

#disconnectObject

[View source]

91
92
93
94
95
96
# File 'lib/dbgeni/base.rb', line 91

def disconnect
  if @connection
    @connection.disconnect
  end
  @connection = nil
end

#ensure_initializedObject

[View source]

111
112
113
# File 'lib/dbgeni/base.rb', line 111

def ensure_initialized
  raise DBGeni::DatabaseNotInitialized unless database_initialized?
end

#initialize_databaseObject

[View source]

102
103
104
# File 'lib/dbgeni/base.rb', line 102

def initialize_database
  DBGeni::Initializer.initialize(connection, @config)
end

#run_plugin(hook, object, params = {}) ⇒ Object

[View source]

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dbgeni/base.rb', line 65

def run_plugin(hook, object, params={})
  pdir = @config.plugin_directory
  if pdir && pdir != ''
    unless @plugin_manager
      @plugin_manager = DBGeni::Plugin.new
      @plugin_manager.load_plugins(pdir)
    end
    @plugin_manager.run_plugins(hook,
                                {
                                  :logger      => @logger,
                                  :object      => object,
                                  :environment => @config.env,
                                  :connection  => connection
                                }.merge!(params)
                                )
  end
end

#select_environment(environment_name) ⇒ Object

[View source]

47
48
49
50
51
52
53
54
# File 'lib/dbgeni/base.rb', line 47

def select_environment(environment_name)
  current_environment = selected_environment_name
  if current_environment != nil && current_environment != environment_name
    # disconnect from database as the connection may well have changed!
    disconnect
  end
  @config.set_env(environment_name)
end

#selected_environment_nameObject

[View source]

56
57
58
59
60
61
62
# File 'lib/dbgeni/base.rb', line 56

def selected_environment_name
  begin
    @config.env.__environment_name
  rescue DBGeni::ConfigAmbiguousEnvironment
    nil
  end
end