Class: ContextualService::Context
- Inherits:
-
Object
- Object
- ContextualService::Context
- Includes:
- Singleton
- Defined in:
- lib/contxtlservice.rb
Overview
Context controls the various Service instances. It’s a Singleton, so if you want to use Context, you’ll have to call Context.instance instead of Context.new.
Instance Method Summary collapse
-
#create_instance(service_class, *init_args) ⇒ Object
:nodoc:.
-
#flush ⇒ Object
Flushes all cached Services.
-
#get_resource(service_class, *init_args) ⇒ Object
:nodoc:.
-
#initialize ⇒ Context
constructor
:nodoc:.
-
#set_init_proc(service_class, proc) ⇒ Object
:nodoc:.
-
#set_resource(service_class, resource, *init_args) ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ Context
:nodoc:
66 67 68 69 |
# File 'lib/contxtlservice.rb', line 66 def initialize #:nodoc: flush @init_procs = {} end |
Instance Method Details
#create_instance(service_class, *init_args) ⇒ Object
:nodoc:
71 72 73 74 75 76 77 |
# File 'lib/contxtlservice.rb', line 71 def create_instance( service_class, *init_args ) #:nodoc: if ( proc = @init_procs[service_class] ) proc.call( *init_args ) else service_class.new( *init_args ) end end |
#flush ⇒ Object
Flushes all cached Services.
80 81 82 |
# File 'lib/contxtlservice.rb', line 80 def flush @resources_by_class = Hash.new { |hash, key| hash[key] = {} } end |
#get_resource(service_class, *init_args) ⇒ Object
:nodoc:
84 85 86 87 88 89 90 91 |
# File 'lib/contxtlservice.rb', line 84 def get_resource( service_class, *init_args ) #:nodoc: resource = @resources_by_class[service_class][init_args] unless resource resource = create_instance( service_class, *init_args ) set_resource( service_class, resource, *init_args ) end resource end |
#set_init_proc(service_class, proc) ⇒ Object
:nodoc:
93 94 95 |
# File 'lib/contxtlservice.rb', line 93 def set_init_proc( service_class, proc ) #:nodoc: @init_procs[service_class] = proc end |
#set_resource(service_class, resource, *init_args) ⇒ Object
:nodoc:
97 98 99 |
# File 'lib/contxtlservice.rb', line 97 def set_resource( service_class, resource, *init_args ) #:nodoc: @resources_by_class[service_class][init_args] = resource end |