Module: Merb::Orms::Sequel

Defined in:
lib/merb/orms/sequel/model.rb,
lib/merb/orms/sequel/connection.rb

Defined Under Namespace

Modules: Model Classes: Connect

Class Method Summary collapse

Class Method Details

.configObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/merb/orms/sequel/connection.rb', line 26

def config
  @config ||= begin
    # Convert string keys to symbols
    full_config = Erubis.load_yaml_file(config_file)
    config = Merb::Plugins.config[:merb_sequel]
    (full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value|
      config[key.to_sym] = value
    end
    config
  end
end

.config_fileObject



10
# File 'lib/merb/orms/sequel/connection.rb', line 10

def config_file() Merb.dir_for(:config) / "database.yml" end

.config_options(config = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/merb/orms/sequel/connection.rb', line 59

def config_options(config = {})
  options = {}
  
  # Use SQLite by default
  options[:adapter]  = (config[:adapter]  || "sqlite")
  # Use localhost as default host
  options[:host]     = (config[:host]     || "localhost")          
  # Default user is an empty string. Both username and user keys are supported.
  options[:user]     = (config[:username] || config[:user] || "")
  
  options[:password] = config[:password] || ""
  
  # Both encoding and charset options are supported, default is utf8
  options[:encoding] = (config[:encoding] || config[:charset] || "utf8")
  # Default database is hey_dude_configure_your_database
  options[:database] = config[:database] || "hey_dude_configure_your_database"
  # MSSQL support
  options[:db_type] = config[:db_type]  if config[:db_type]
  options[:socket] = config[:socket] if config[:socket]
  options[:logger]   = Merb.logger
  options
end

.connectObject

Database connects as soon as the gem is loaded



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/merb/orms/sequel/connection.rb', line 39

def connect
  if File.exists?(config_file)
    Merb.logger.info!("Connecting to the '#{config[:database]}' database on '#{config[:host]}' using '#{config[:adapter]}' ...")
    connection = ::Sequel.connect(config_options(config))
    begin
      connection.test_connection
    rescue => e
      Merb.logger.error!("Connection Error: #{e}")
      exit(1)
    end
    connection
  else
    copy_sample_config
    Merb.logger.set_log(STDERR)
    Merb.logger.error! "No database.yml file found at #{config_file}."
    Merb.logger.error! "A sample file was created called #{sample_dest} for you to copy and edit."
    exit(1)
  end
end

.copy_sample_configObject



22
23
24
# File 'lib/merb/orms/sequel/connection.rb', line 22

def copy_sample_config
  FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
end

.new_sequel?Boolean

Determine if we use Sequel 3 or not

Returns

Bool

True if using Sequel >= 2.12.0 or False

Returns:

  • (Boolean)


18
19
20
# File 'lib/merb/orms/sequel/connection.rb', line 18

def new_sequel?
  !!(/^(2.12|3)/ =~ ::Sequel.version)
end

.sample_destObject



11
# File 'lib/merb/orms/sequel/connection.rb', line 11

def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end

.sample_sourceObject



12
# File 'lib/merb/orms/sequel/connection.rb', line 12

def sample_source() File.dirname(__FILE__) / "database.yml.sample" end