Class: ROM::Rails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/rails/railtie.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Configuration

Returns a new instance of Configuration.



41
42
43
44
# File 'lib/rom/rails/railtie.rb', line 41

def initialize(config)
  @config = config
  @setup = ROM.setup(@config.symbolize_keys)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/rom/rails/railtie.rb', line 5

def config
  @config
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/rom/rails/railtie.rb', line 5

def env
  @env
end

#setupObject (readonly)

Returns the value of attribute setup.



5
6
7
# File 'lib/rom/rails/railtie.rb', line 5

def setup
  @setup
end

Class Method Details

.build(app) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/rom/rails/railtie.rb', line 7

def self.build(app)
  root = app.config.root
  db_config = app.config.database_configuration[::Rails.env].symbolize_keys

  config = rewrite_config(root, db_config)

  new(config)
end

.rewrite_config(root, config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rom/rails/railtie.rb', line 16

def self.rewrite_config(root, config)
  adapter = config[:adapter]
  database = config[:database]
  password = config[:password]
  username = config[:username]
  hostname = config.fetch(:hostname) { 'localhost' }

  adapter = "sqlite" if adapter == "sqlite3"

  path =
    if adapter == "sqlite"
      "#{root}/#{database}"
    else
      db_path = [hostname, database].join('/')

      if username && password
        [[username, password].join(':'), db_path].join('@')
      else
        db_path
      end
    end

  { default: "#{adapter}://#{path}" }
end

Instance Method Details

#finalizeObject



46
47
48
# File 'lib/rom/rails/railtie.rb', line 46

def finalize
  @env = setup.finalize
end