Class: Workarea::Configuration::MongoidClient

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/configuration/mongoid_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ MongoidClient

Returns a new instance of MongoidClient.



4
5
6
7
# File 'lib/workarea/configuration/mongoid_client.rb', line 4

def initialize(name = nil)
  @name = name
  @default = MongoidClient.new unless name.nil?
end

Instance Method Details

#databaseObject



17
18
19
# File 'lib/workarea/configuration/mongoid_client.rb', line 17

def database
  "#{Workarea.config.site_name.systemize}_#{Rails.env}"
end

#hostsObject



21
22
23
24
25
26
27
28
# File 'lib/workarea/configuration/mongoid_client.rb', line 21

def hosts
  env_hosts =
    ENV.select { |k| k =~ /^#{env_key}_HOST(?:_\d+)?$/ }
       .map(&:last)
       .reject(&:blank?)

  env_hosts.presence || @default&.hosts || ['localhost:27017']
end

#optionsObject



30
31
32
33
34
35
36
37
# File 'lib/workarea/configuration/mongoid_client.rb', line 30

def options
  JSON.parse(ENV["#{env_key}_OPTIONS"].presence || '{}')
      .deep_symbolize_keys
      .reverse_merge(@default&.options || {})
      .reverse_merge(max_pool_size: max_pool_size)
rescue JSON::ParserError
  {}
end

#to_hObject



9
10
11
12
13
14
15
# File 'lib/workarea/configuration/mongoid_client.rb', line 9

def to_h
  {
    database: database,
    hosts: hosts,
    options: options
  }
end