Class: Slick::ResourceProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/slick/resource_provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResourceProvider

Returns a new instance of ResourceProvider.



7
8
9
10
# File 'lib/slick/resource_provider.rb', line 7

def initialize
    @shared_resources = {}
    @mutex = Mutex.new
end

Instance Attribute Details

#shared_resourcesObject (readonly)

Returns the value of attribute shared_resources.



5
6
7
# File 'lib/slick/resource_provider.rb', line 5

def shared_resources
  @shared_resources
end

Instance Method Details

#[](name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/slick/resource_provider.rb', line 16

def [](name)
    name = name.to_s

    return thread_resources[name] if !thread_resources[name].nil?
    return shared_resources[name] if !shared_resources[name].nil?

    resource_factory = Slick::ResourceFactory.create(name)

    if resource_factory.class.shared?
        @mutex.synchronize { shared_resources[name] ||= resource_factory.create }
        shared_resources[name]
    else
        thread_resources[name] = resource_factory.create
        thread_resources[name]
    end
end

#[]=(name, value) ⇒ Object



33
34
35
36
# File 'lib/slick/resource_provider.rb', line 33

def []=(name, value)
    thread_resources[name.to_s] = value
    self
end

#reset(shared = false, thread = true, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/slick/resource_provider.rb', line 38

def reset(shared = false, thread = true, &block)
    previous_shared_resources = shared_resources
    previous_thread_resources = thread_resources
    @mutex.syncronize { @shared_resources = {} } if shared
    Thread.current["SLICK_RESOURCES"] = {} if thread
    if block
        out = block.call
        @mutex.synchronize { @shared_resources = previous_shared_resources } if shared
        Thread.current["SLICK_RESOURCES"] = previous_thread_resources if thread
        out
    end
end

#thread_resourcesObject



12
13
14
# File 'lib/slick/resource_provider.rb', line 12

def thread_resources
    Thread.current["SLICK_RESOURCES"] ||= {}
end