Class: IocRb::Scopes::RequestScope

Inherits:
Object
  • Object
show all
Defined in:
lib/ioc_rb/scopes/request_scope.rb

Overview

Request scope instantiates new bean instance on each new HTTP request

Instance Method Summary collapse

Constructor Details

#initialize(bean_factory) ⇒ RequestScope

Constructon

Parameters:

  • bean_factory

    bean factory



9
10
11
# File 'lib/ioc_rb/scopes/request_scope.rb', line 9

def initialize(bean_factory)
  @bean_factory = bean_factory
end

Instance Method Details

#delete_bean(bean_metadata) ⇒ Object

Delete bean from scope

Parameters:



30
31
32
# File 'lib/ioc_rb/scopes/request_scope.rb', line 30

def delete_bean()
  RequestStore.store[:_iocrb_beans].delete(.name)
end

#get_bean(bean_metadata) ⇒ Object

Returns a bean from the RequestStore RequestStore is a wrapper for Thread.current which clears it on each new HTTP request

Parameters:



19
20
21
22
23
24
25
26
# File 'lib/ioc_rb/scopes/request_scope.rb', line 19

def get_bean()
  RequestStore.store[:_iocrb_beans] ||= {}
  if bean = RequestStore.store[:_iocrb_beans][.name]
    bean
  else
   @bean_factory.create_bean_and_save(, RequestStore.store[:_iocrb_beans])
  end
end