Sha256: 969df4ae3577faef090d336c6ec9fc66bac09bc9c44958efa313285e693bf8b8

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module Prefab
  @@lock = Concurrent::ReadWriteLock.new

  def self.init(options = Prefab::Options.new)
    unless @singleton.nil?
      Prefab::LoggerClient.instance.warn 'Prefab already initialized.'
      return @singleton
    end

    @@lock.with_write_lock {
      @singleton = Prefab::Client.new(options)
    }
  end

  def self.fork
    ensure_initialized
    @@lock.with_write_lock {
      @singleton = @singleton.fork
    }
  end

  def self.set_rails_loggers
    ensure_initialized
    @singleton.set_rails_loggers
  end

  def self.get(key, properties = NO_DEFAULT_PROVIDED)
    ensure_initialized
    @singleton.get(key, properties)
  end

  def self.enabled?(feature_name, jit_context = NO_DEFAULT_PROVIDED)
    ensure_initialized
    @singleton.enabled?(feature_name, jit_context)
  end

  def self.with_context(properties, &block)
    ensure_initialized
    @singleton.with_context(properties, &block)
  end

  def self.instance
    ensure_initialized
    @singleton
  end

  private

  def self.ensure_initialized
    if not defined? @singleton or @singleton.nil?
      raise "Use Prefab.initialize before calling Prefab.get"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
prefab-cloud-ruby-1.3.2 lib/prefab/prefab.rb
prefab-cloud-ruby-1.3.1 lib/prefab/prefab.rb
prefab-cloud-ruby-1.3.0 lib/prefab/prefab.rb
prefab-cloud-ruby-1.2.1 lib/prefab/prefab.rb
prefab-cloud-ruby-1.2.0 lib/prefab/prefab.rb