Module: Story::DB::Utils

Included in:
Base
Defined in:
lib/story/db/utils.rb

Instance Method Summary collapse

Instance Method Details

#concat_default_and_user_adapters?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/story/db/utils.rb', line 29

def concat_default_and_user_adapters?
  sinatra_setting_exists? :db_adapters and settings.db_adapters.select { |b| b.is_a? (String) }.size > 0 and settings.db_adapters.is_a? (Array)
end

#db_connected?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/story/db/utils.rb', line 4

def db_connected?
  @errors ||= []
  begin
    configuration_file = File.exists?('dbconfig.yml') ? 'dbconfig.yml' : File.join(File.dirname(__FILE__), 'config.yml')
    dbconfig = parse_configuration_from configuration_file
    ActiveRecord::Base.logger = Logger.new STDERR
    ActiveRecord::Base.establish_connection dbconfig
    true
  rescue Errno::ENOENT
    @errors.push "Database configuration file not found."
    false
  rescue Errors::DatabaseError => e
    @errors.push e.message
    false
  end
end

#define_db_error_type(adapters, config) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/story/db/utils.rb', line 39

def define_db_error_type adapters, config
  case
    when !config.has_key?("adapter") && !adapters.include?(config["adapter"].downcase) then 0
    when !config.has_key?("adapter") then 1
    when !config.has_key?("database") then 2
    when !config.has_key?("adapter") && !config.has_key?("database") then 3
  end
end

#get_list_of_db_adaptersObject



25
26
27
# File 'lib/story/db/utils.rb', line 25

def get_list_of_db_adapters
  ["jdbc", "fb", "frontbase", "mysql", "openbase", "oci", "postgresql", "sqlite3", "sqlite2", "sqlite", "sqlserver", "sybsql"]
end

#parse_adaptersObject



33
34
35
36
37
# File 'lib/story/db/utils.rb', line 33

def parse_adapters
  default_adapters = get_list_of_db_adapters
  if concat_default_and_user_adapters? then default_adapters.concat(settings.db_adapters.each(&:downcase!))
  else default_adapters end
end

#parse_configuration_from(file) ⇒ Object



57
58
59
60
61
62
# File 'lib/story/db/utils.rb', line 57

def parse_configuration_from file
  config = YAML::load File.open file
  error_types = ["unsupported_db_adapter", "no_db_adapter_specified", "no_database_specified", "no_db_adapter_and_database_specified"]
  raise Errors::DatabaseError, send("raise_#{error_types[@db_error_type]}".to_sym, file, config) if some_db_errors_in? config
  config
end

#sinatra_setting_exists?(setting) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/story/db/utils.rb', line 21

def sinatra_setting_exists? setting
  settings.respond_to? setting
end

#some_db_errors_in?(config) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/story/db/utils.rb', line 48

def some_db_errors_in? config
  adapters = parse_adapters
  errors_found = !config.has_key?("adapter") or !config.has_key?("database") or !(config.has_key?("adapter") and adapters.include?(config["adapter"].downcase))
  if errors_found then
    @db_error_type = define_db_error_type adapters, config
  end
  errors_found
end