class Ekylibre::PluginSystem::GlobalContainer

This class is a wrapper around RequestStore that, in addition to provide a concise way to access the container globally, ensures that a container is present if the application ask for one (nil is not a valid value)

Public Class Methods

get() click to toggle source

@return [Corindon::DependencyInjection::Container]

# File lib/ekylibre/plugin_system/global_container.rb, line 44
def get
  container = RequestStore['container']

  if container.nil?
    raise StandardError.new(
      'The application requires the container to be loaded to work. Please load it before calling GlobalContainer.get'
    )
  else
    container
  end
end
has?() click to toggle source
# File lib/ekylibre/plugin_system/global_container.rb, line 39
def has?
  RequestStore.exist?('container')
end
replace_with(container, &block) click to toggle source

@param [Corindon::DependencyInjection::Container] container

# File lib/ekylibre/plugin_system/global_container.rb, line 21
def replace_with(container, &block)
  prev = if has?
           get
         else
           nil
         end

  set(container)

  block.call
ensure
  if prev.nil?
    unset
  else
    set(prev)
  end
end
set(container) click to toggle source

@param [Corindon::DependencyInjection::Container] container

# File lib/ekylibre/plugin_system/global_container.rb, line 12
def set(container)
  RequestStore['container'] = container
end
unset() click to toggle source
# File lib/ekylibre/plugin_system/global_container.rb, line 16
def unset
  RequestStore.delete('container')
end