UpdateListener-class {Proc4} | R Documentation |
"UpdateListener"
This Listener
updates an existing record (in a Mongo
collection) for the student
(uid
), with the contents of the data (details) field of the
message.
The database is a mongo
collection identified
by dburi
, dbname
and colname
(collection within
the database). The mess
field of the P4Message
is checked against the applicable messages in messSet
. If it
is there, then the record in the database corresponding to the
qfields
(by default app(mess)
and uid(mess)
) is
updated. Specifically, the field targetField
is set to
details(mess)
. The function jsonEncoder
is called to
encode the target field as a JSON object for injection into the
database.
This class implements the Listener
interface.
All reference classes extend and inherit methods from
"envRefClass"
.
signature(x = "UpdateListener")
: TRUE
signature(x = "UpdateListener")
: If
the message is in the messSet
, it updates the record
corresponding to app(mess)
and uid(mess)
in the
database with the contents of details(mess)
. (See details.)
dbname
:Object of class character
giving the
name of the Mongo database
dburi
:Object of class character
giving the url
of the Mongo database.
colname
:Object of class character
giving the
column of the Mongo database.
messSet
:A vector of class character
giving the
name of messages which are sent to the database. Only messages
for which mess(mess)
is an element of messSet
will
be inserted.
db
:Object of class MongoDB
giving the
database. Use messdb()
to access this field to makes sure
it has been set up.
qfields
:Object of class character
giving the
names of the fields which should be considered a key for the
messages.
targetField
:Object of class character
naming
the field which is to be set.
jsonEncoder
:Object of class character
naming a
function which will be used to encode details(mess)
as a
JSON object. The default is unparseData
.
messdb()
:Accessor for the database collection. Initializes the connection if it has not been set up.
receiveMessage(mess)
:Does the work of updating the database. See Details.
initialize(sender, dbname, dburi, colname, messSet,
...)
:Sets default values for fields.
Russell Almond
This is an example of the observer design pattern. https://en.wikipedia.org/wiki/Observer_pattern.
Listener
, P4Message
,
UpdateListener
,
InjectionListener
,
CaptureListener
,
UpsertListener
,
TableListener
,
mongo
The function unparseData
is the default encoder.
mess2 <- P4Message(app="default",uid="Phred",context="Down Hill", sender="EIEvent",mess="Money Earned", details=list(trophyHall=list(list("Down Hill"="gold"), list("Stairs"="silver")), bankBalance=10)) data2json <- function(dat) { toJSON(sapply(dat,unboxer)) } upwind <- UpdateListener(messSet=c("Money Earned","Money Spent"), targetField="data",colname="Players", jsonEncoder="data2json") receiveMessage(upwind,mess2)