Function for creating new mapper types

Share:

Description

A mapper is a collection of functions that define how a type of foreign object is accessed. newMapper creates a new mapper object, assigns it a type, and places it in the global Mapper database.

Usage

1
newMapper( type=NULL, init=NULL, get=NULL, assign=NULL, finalize=NULL, ...)

Arguments

type

Character vector length one describing the unique type of foreign objects available through the new mapper.

init

A function whose signature is .init(map,...) where map is a new map object and ... are those from the newMap call. Returns TRUE for a successfull initialization of the map object or FALSE on error.

get

A function whose signature is .get(symbol) where symbol is a character vector of length one defining the name of the foreign object to fetch. Returns the foreign object or UnboundValue(). NULL is also an acceptable value for .get.

assign

A function whose signature is .assign(symbol,val) where symbol is a character string of length one defining the name of the foreign object to assign to and val is the value the foreign object will take. There are no requirements on the return value of .assign. NULL is also an acceptable value for .assign.

finalize

A function whose signature is 'finalize(map)' where map is the object to finalize. There are no requirements on .finalize's return value. NULL is also an acceptible value.

...

Additional objects that are also added to the mapper environment. They MUST be named arguments, i.e. name=val.

Value

Invisibly returns the new mapper, an object of class 'dataMapper'. newMapper is called for its side effect of adding the new mapper to the global Mapper database.

See Also

newMap

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Complete example mapper
newMapper(
	type="EXAMPLE",
	init=function(map,symbols=c('foo','bar','baz'),len=3){

		# Install symbols that the users passes in from newMap().
		lapply(symbols,install,map)

		# Now let's add some state to the internal portion of our map.
		map$len <- len

		# Returning FALSE means failure
		return(TRUE)
	},
	get = function(x) {
		cat("I'll just get",x,"for you.\n")

		# len is pulled from the internal portion of the map
		# by lexical scoping rules. Anything can be returned here, but we 
		# default to a numeric value
		rnorm(len)
	},
	assign = function(x,val){
		cat("Calling assign",val,"to",x,".\n")
	},
	finalize = function(map){
		cat("Finalization can clear any state, like shutting down database\n")
		cat("connections, socket connections, etc.\n")
	},

	# The rest of the arguments are copied to the internal portion of the map.
	foo = 'bar'
)