Class: ROM::Rails::ActiveRecord::UriBuilder

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

Instance Method Summary collapse

Instance Method Details

#build(adapter, uri_options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rom/rails/active_record/uri_builder.rb', line 7

def build(adapter, uri_options)
  builder_method = :"#{adapter}_uri"

  uri = if respond_to?(builder_method)
          send(builder_method, uri_options)
        else
          generic_uri(uri_options)
        end

  # JRuby connection strings require special care.
  if RUBY_ENGINE == 'jruby' && adapter != 'postgresql'
    uri = "jdbc:#{uri}"
  end

  uri
end

#build_uri(attrs) ⇒ Object



60
61
62
# File 'lib/rom/rails/active_record/uri_builder.rb', line 60

def build_uri(attrs)
  Addressable::URI.new(attrs).to_s
end

#escape_option(option) ⇒ Object



64
65
66
# File 'lib/rom/rails/active_record/uri_builder.rb', line 64

def escape_option(option)
  option.nil? ? option : CGI.escape(option)
end

#generic_uri(config) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/rom/rails/active_record/uri_builder.rb', line 49

def generic_uri(config)
  build_uri(
    scheme: config.fetch(:scheme),
    user: escape_option(config[:username]),
    password: escape_option(config[:password]),
    host: config[:host],
    port: config[:port],
    path: config[:database]
  )
end

#mysql_uri(config) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rom/rails/active_record/uri_builder.rb', line 41

def mysql_uri(config)
  if config.key?(:username) && !config.key?(:password)
    config.update(password: '')
  end

  generic_uri(config)
end

#postgresql_uri(config) ⇒ Object



34
35
36
37
38
39
# File 'lib/rom/rails/active_record/uri_builder.rb', line 34

def postgresql_uri(config)
  generic_uri(config.merge(
                host: config.fetch(:host) { '' },
                scheme: 'postgres'
              ))
end

#sqlite3_uri(config) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rom/rails/active_record/uri_builder.rb', line 24

def sqlite3_uri(config)
  path = Pathname.new(config.fetch(:root)).join(config.fetch(:database))

  build_uri(
    scheme: 'sqlite',
    host: '',
    path: path.to_s
  )
end