Class: DBGeni::Config

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

Overview

migrations_directory ‘value’ # This will be checked to ensure its a valid dir

# and defaults to 'migrations'

environment(‘development’)

username 'user' # this must be here, or it will error
database 'MYDB.WORLD'     # this must be here, or it will error. For Oracle, this is the TNS Name
password ''     # If this value is missing, it will be promoted for if the env is used.

environment(‘other environment’)

username '' # the environment block can be repeated for many environments

global_parameters { # These are common parameters to all environments, but they can be

                  # overriden. Basically take global, merge in environment
param_name 'value'

}

Constant Summary collapse

DEFAULTS_ENV =
'__defaults__'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.

[View source]

41
42
43
44
45
46
47
48
49
50
# File 'lib/dbgeni/config.rb', line 41

def initialize
  @migration_directory  = 'migrations'
  @dml_directory        = 'dml'
  @code_dir             = 'code'
  @plugin_dir           =  nil
  @db_type              = 'sqlite'
  @db_table             = 'dbgeni_migrations'
  @base_dir             = '.'
  @environments         = Hash.new
end

Instance Attribute Details

#base_directoryObject

Returns the value of attribute base_directory.


36
37
38
# File 'lib/dbgeni/config.rb', line 36

def base_directory
  @base_directory
end

#code_dirObject (readonly)

ideally wnat code_dir to be code_directory, but then it clashes with the setter used in the config file, so need to change it. Probably more sensible like this than the migrations_directory vs migration_directory

_-_

32
33
34
# File 'lib/dbgeni/config.rb', line 32

def code_dir
  @code_dir
end

#config_fileObject (readonly)

Returns the value of attribute config_file.


35
36
37
# File 'lib/dbgeni/config.rb', line 35

def config_file
  @config_file
end

#current_environmentObject (readonly)

Returns the value of attribute current_environment.


24
25
26
# File 'lib/dbgeni/config.rb', line 24

def current_environment
  @current_environment
end

#db_tableObject (readonly)

defaults to dbgeni_migrations


34
35
36
# File 'lib/dbgeni/config.rb', line 34

def db_table
  @db_table
end

#db_typeObject (readonly)

oracle, mysql, sqlite etc, default sqlite


33
34
35
# File 'lib/dbgeni/config.rb', line 33

def db_type
  @db_type
end

#dml_directory(*p) ⇒ Object (readonly)

Returns the value of attribute dml_directory.


26
27
28
# File 'lib/dbgeni/config.rb', line 26

def dml_directory
  @dml_directory
end

#environmentsObject (readonly)

Returns the value of attribute environments.


23
24
25
# File 'lib/dbgeni/config.rb', line 23

def environments
  @environments
end

#migration_directoryObject (readonly)

Returns the value of attribute migration_directory.


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

def migration_directory
  @migration_directory
end

Class Method Details

.load_from_file(filename) ⇒ Object

[View source]

53
54
55
56
# File 'lib/dbgeni/config.rb', line 53

def self.load_from_file(filename)
  cfg = self.new
  cfg.load_from_file(filename)
end

Instance Method Details

#code_directory(*p) ⇒ Object

[View source]

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dbgeni/config.rb', line 217

def code_directory(*p)
  if p.length == 0
    @code_dir
  else
    if is_absolute_path?(p[0])
      # it looks like an absolute path
      @code_dir = p[0]
    else
      # it looks like a relative path
      if @base_directory
        @code_dir = File.join(@base_directory, p[0])
      else
        @code_dir = p[0]
      end
    end
  end
end

#current_envObject

[View source]

114
115
116
117
118
119
120
121
122
# File 'lib/dbgeni/config.rb', line 114

def current_env
  if @current_environment
    @environments[@current_environment]
  elsif @environments.keys.reject{|i| i == DEFAULTS_ENV}.length == 1
    @environments[@environments.keys.reject{|i| i == DEFAULTS_ENV}.first]
  else
    raise DBGeni::ConfigAmbiguousEnvironment, "More than one environment is defined"
  end
end

#database_table(*p) ⇒ Object

[View source]

259
260
261
262
# File 'lib/dbgeni/config.rb', line 259

def database_table(*p)
  # TODO - consider putting validation here
  @db_table = p[0]
end

#database_type(*p) ⇒ Object

[View source]

254
255
256
257
# File 'lib/dbgeni/config.rb', line 254

def database_type(*p)
  # TODO - consider putting validation here
  @db_type = p[0]
end

#defaults(&block) ⇒ Object

[View source]

292
293
294
# File 'lib/dbgeni/config.rb', line 292

def defaults(&block)
  environment(DEFAULTS_ENV, &block)
end

#envObject

[View source]

109
110
111
# File 'lib/dbgeni/config.rb', line 109

def env
  current_env
end

#environment(name, &block) ⇒ Object

Given a block of environment details, generate a new environment object. eg environment(‘some_name’) do

database  ''
user      ''
password  prompt

some_dir_path '/path_to_directory'
[View source]

281
282
283
284
285
286
287
288
289
290
# File 'lib/dbgeni/config.rb', line 281

def environment(name, &block)
  env = Environment.new(name)
  block.arity < 1 ? env.instance_eval(&block) : block.call(env)
  env.__completed_loading
  if @environments.has_key?(name)
    @environments[name].__merge_environment(env)
  else
    @environments[name] = env
  end
end

#include_file(*p) ⇒ Object

[View source]

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/dbgeni/config.rb', line 297

def include_file(*p)
  file = p[0]
  if !is_absolute_path?(file)
    file = File.join(@base_directory, file)
  end
  begin
    raw_config = ''
    File.open(file) do |f|
      raw_config = f.read
    end
    self.load(raw_config)
  rescue Errno::ENOENT
    raise DBGeni::ConfigFileNotExist, "Included config #{file} does not exist"
  rescue DBGeni::ConfigSyntaxError
    raise DBGeni::ConfigSyntaxError,  "Included config #{file} contains errors: #{e.to_s}"
  end
end

#load(raw_config, recursed = false) ⇒ Object

[View source]

141
142
143
144
145
146
147
148
149
# File 'lib/dbgeni/config.rb', line 141

def load(raw_config, recursed=false)
  begin
    self.instance_eval(raw_config)
  rescue Exception => e
    raise DBGeni::ConfigSyntaxError, e.to_s
  end
  merge_defaults unless recursed
  self
end

#load_from_file(filename) ⇒ Object

[View source]

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dbgeni/config.rb', line 59

def load_from_file(filename)
  if filename == nil
    raise DBGeni::ConfigFileNotSpecified
  end
  raw_config = ''
  self.base_directory = File.expand_path(File.dirname(filename))
  @config_file        = File.expand_path(filename)
  begin
    File.open(@config_file) do |f|
      raw_config = f.read
    end
  rescue Errno::ENOENT
    raise DBGeni::ConfigFileNotExist, "#{@config_location} (expanded from #{filename}) does not exist"
  end
  load(raw_config)
end

#merge_defaultsObject

[View source]

151
152
153
154
155
156
157
158
# File 'lib/dbgeni/config.rb', line 151

def merge_defaults
  if @environments.has_key? DEFAULTS_ENV
    @environments.keys.each do |k|
      next if k == DEFAULTS_ENV
      @environments[k].__merge_defaults(@environments[DEFAULTS_ENV].__params)
    end
  end
end

#migrations_directory(*p) ⇒ Object

mig_dir could be defined as a file in current directory ‘migrations’ or it could be a relative directory ‘./somedir/migrations’ or it could be a full path ‘/somedir/migrations’ or in windows ‘C:somedirmigrations’ To use the migrations it needs to be expanded to a full path somehow. If it begins / or <LETTER>:\ then assume its full path, otherwise concatenate to the full path of the config file.

[View source]

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dbgeni/config.rb', line 181

def migrations_directory(*p)
  if p.length == 0
    @migration_directory
  else
    if is_absolute_path?(p[0])
      # it looks like an absolute path
      @migration_directory = p[0]
    else
      # it looks like a relative path
      if @base_directory
        @migration_directory = File.join(@base_directory, p[0])
      else
        @migration_directory = p[0]
      end
    end
  end
end

#plugin_directory(*p) ⇒ Object

[View source]

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/dbgeni/config.rb', line 235

def plugin_directory(*p)
  if p.length == 0
    @plugin_dir
  else
    if is_absolute_path?(p[0])
      # it looks like an absolute path
      @plugin_dir = p[0]
    else
      # it looks like a relative path
      if @base_directory
        @plugin_dir = File.join(@base_directory, p[0])
      else
        @plugin_dir = p[0]
      end
    end
  end
end

#set_env(name = nil) ⇒ Object

[View source]

125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dbgeni/config.rb', line 125

def set_env(name=nil)
  if name == nil
    valid_envs = @environments.keys.reject{|i| i == DEFAULTS_ENV}
    if valid_envs.length == 1
      @current_environment = valid_envs.first
    else
      raise DBGeni::ConfigAmbiguousEnvironment, "More than one environment is defined"
    end
  elsif @environments.has_key?(name)
    @current_environment = name
  else
    raise DBGeni::EnvironmentNotExist
  end
end

#to_sObject

[View source]

160
161
162
163
164
165
166
167
168
169
# File 'lib/dbgeni/config.rb', line 160

def to_s
  str = ''
  str << "migrations_directory => #{@migration_directory}\n"
  @environments.keys.sort.each do |k|
    str << "\n\nEnvironment: #{k}\n"
    str <<     "=============#{(1..k.length).map do "=" end.join}\n\n"
    str << @environments[k].__to_s
  end
  str
end