Class: SequelRails::Configuration

Inherits:
ActiveSupport::OrderedOptions
  • Object
show all
Defined in:
lib/sequel_rails/configuration.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sequel_rails/configuration.rb', line 21

def initialize(*)
  super
  self.root = Rails.root
  self.raw = nil
  self.logger = nil
  self.migration_dir = nil
  self.schema_dump = default_schema_dump
  self.load_database_tasks = true
  self.after_connect = nil
  self.after_new_connection = nil
  self.skip_connect = nil
  self.test_connect = true
end

Class Method Details

.for(root, database_yml_hash) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/sequel_rails/configuration.rb', line 12

def self.for(root, database_yml_hash)
  ::SequelRails.configuration ||= begin
    config = new
    config.root = root
    config.raw = database_yml_hash
    config
  end
end

Instance Method Details

#connect(environment) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sequel_rails/configuration.rb', line 51

def connect(environment)
  normalized_config = environment_for environment

  unless (normalized_config.keys & %w[adapter url]).any?
    raise "Database not configured.\n" \
        'Please create config/database.yml or set DATABASE_URL in environment.'
  end

  if normalized_config['url']
    ::Sequel.connect normalized_config['url'], SequelRails.deep_symbolize_keys(normalized_config)
  else
    ::Sequel.connect SequelRails.deep_symbolize_keys(normalized_config)
  end.tap { after_connect.call if after_connect.respond_to?(:call) }
end

#environment_for(name) ⇒ Object



35
36
37
# File 'lib/sequel_rails/configuration.rb', line 35

def environment_for(name)
  environments[name.to_s] || environments[name.to_sym]
end

#environmentsObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sequel_rails/configuration.rb', line 39

def environments
  @environments ||= raw.reduce(
    # default config - use just the environment variable
    Hash.new(normalize_repository_config({}))
  ) do |normalized, environment|
    name = environment.first
    config = environment.last
    normalized[name] = normalize_repository_config(config)
    normalized
  end
end