Class: ContextualService::Service

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

Overview

A Service is the point of access to the global service itself. Using ContextualService mostly means creating subclasses of Service and then adding your domain-specific functionality to that subclass.

To get a Service, call its get_< service_name > method:

object_store = ObjectStore.get_object_store

To set a Service, call its set_< service_name > method:

ObjectStore.set_object_store MockObjectStore.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

A Service can only be initialized through the Context instance: This behavior is enforced during initialization. So if you child class has its own initialize method, you should make sure to call super() to keep this restriction.



147
148
149
150
151
152
153
154
155
# File 'lib/contxtlservice.rb', line 147

def initialize
	regexp = %r{[^_]contxtlservice\.rb.*create_instance}
	unless caller.any? { |line| line =~ regexp }
		raise ArgumentError,
		"#{ self.class.name.to_s } should be instantiated by calling " +
				self.class.name.to_s + ".get_" + self.class.name.camel_case_to_underscore,
		caller
	end
end

Class Method Details

.flushObject

Flushes the currently saved instance of the Service in the Context; the next time Service.get_< service_name > is called, Context will re-intantiate the Service from scratch.



115
# File 'lib/contxtlservice.rb', line 115

def self.flush; Context.instance.set_resource( self, nil ); end

.method_missing(symbol, *args) ⇒ Object

:nodoc:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/contxtlservice.rb', line 117

def self.method_missing( symbol, *args ) #:nodoc:
	method_name = symbol.id2name
	target = nil
	if method_name =~ /^get_(.*)/
		target = :get_resource if $1.underscore_to_camel_case == basename
	elsif method_name =~ /^set_(.*)/
		target = :set_resource if $1.underscore_to_camel_case == basename
	end
	if target
		Context.instance.send( target, self, *args )
	else
		super
	end
end

.set_init_procObject

Sets what happens when Context is asked for a Service that hasn’t yet been set or saved. By default, Context simply creates that Service and passes it on, but you can override that with a proc of some kind.

ObjectStore.set_init_proc { MockObjectStore.new }

Most of the time, it will just be simpler to use Service.set_< service_name > instead.



138
139
140
141
# File 'lib/contxtlservice.rb', line 138

def self.set_init_proc
	proc = proc { yield }
	Context.instance.set_init_proc( self, proc )
end