Class: Ekylibre::PluginSystem::GlobalContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/ekylibre/plugin_system/global_container.rb

Overview

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)

Class Method Summary collapse

Class Method Details

.getCorindon::DependencyInjection::Container

Returns:

  • (Corindon::DependencyInjection::Container)


44
45
46
47
48
49
50
51
52
53
54
# 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?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ekylibre/plugin_system/global_container.rb', line 39

def has?
  RequestStore.exist?('container')
end

.replace_with(container, &block) ⇒ Object

Parameters:

  • container (Corindon::DependencyInjection::Container)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object

Parameters:

  • container (Corindon::DependencyInjection::Container)


12
13
14
# File 'lib/ekylibre/plugin_system/global_container.rb', line 12

def set(container)
  RequestStore['container'] = container
end

.unsetObject



16
17
18
# File 'lib/ekylibre/plugin_system/global_container.rb', line 16

def unset
  RequestStore.delete('container')
end