Module: RailsSequel

Defined in:
lib/rails_sequel/version.rb,
lib/rails_sequel/rails_sequel.rb

Class Method Summary collapse

Class Method Details

.configObject

Returns loaded database.yml configuration for current environment



13
14
15
# File 'lib/rails_sequel/rails_sequel.rb', line 13

def self.config
  @config ||= YAML::load(ERB.new(IO.read(File.join(Rails.root, "config", "database.yml"))).result)[Rails.env].with_indifferent_access
end

.connectObject

Connects to database using constructed Database Connection URI



4
5
6
7
8
9
10
# File 'lib/rails_sequel/rails_sequel.rb', line 4

def self.connect
  connection = Sequel.connect(options = self.prepare_options)
  if options[:adapter] == 'mysql'
    connection.execute("SET SQL_AUTO_IS_NULL=0")
  end
  connection
end

.prepare_optionsObject

Constructs Database Connection URI



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_sequel/rails_sequel.rb', line 23

def self.prepare_options
  options = {}

  # Use SQLite by default
  options[:adapter]   = config[:adapter] || "sqlite"
  
  # Use localhost as default host
  options[:host]      = config[:host] || config[:hostname] || "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
  [:db_type, :socket, :charset, :encoding].each do |var|
    options[var]      = config[var] if config[var]
  end
  
  # JDBC support
  [:url, :uri].each do |var|
    options[var]      = config[var] if config[var]
  end
  
  options[:loggers]   = [Rails.logger]
  options
end

.reset_config!Object

Resets config



18
19
20
# File 'lib/rails_sequel/rails_sequel.rb', line 18

def self.reset_config!
  @config = nil
end

.versionObject

Returns current plugin version



3
4
5
6
7
# File 'lib/rails_sequel/version.rb', line 3

def self.version
  return @version if @version
  config = YAML.load(File.read(File.join(File.dirname(__FILE__), '../../VERSION.yml')))
  @version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
end