Class: ContextualService::Context

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeContext

: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

#flushObject

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