Sha256: a9d882701b315ac919d508de5e5f4c39a2c482fa8c687434de174a72b52b9b9b

Contents?: true

Size: 1.24 KB

Versions: 62

Compression:

Stored size: 1.24 KB

Contents

module Workarea
  module Configuration
    class MongoidClient
      def initialize(name = nil)
        @name = name
        @default = MongoidClient.new unless name.nil?
      end

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

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

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

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

      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

      private

      def env_key
        ['WORKAREA', 'MONGOID', @name.to_s.underscore.upcase]
          .reject(&:blank?)
          .join('_')
      end

      # TODO: Remove this in v3.5 in favor of using WORKAREA_MONGOID_OPTIONS
      def max_pool_size
        (ENV['WORKAREA_MONGOID_MAX_POOL_SIZE'].presence || 100).to_i
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.26 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.45 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.25 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.23 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.44 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.22 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.43 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.21 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.42 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.20 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.41 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.19 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.40 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.18 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.39 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.17 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.38 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.5.16 lib/workarea/configuration/mongoid_client.rb
workarea-core-3.4.37 lib/workarea/configuration/mongoid_client.rb