UpdateListener-class | 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.
If targetField=""
, then the behavior is slightly different. Instead the fields in
details(mess)
(labeled by their names) are updated.
This class implements the Listener
interface.
All reference classes extend and inherit methods from
"envRefClass"
.
signature(x = "UpdateListener")
: TRUE
signature(x = "UpdateListener", message)
: 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.)
signature(x = "UpdateListener")
: Returns
the name assigned to the listener.
signature(listener =
"UpdateListener", appid )
: Builds a data datable from the messages.
When the listenerDataTable
method is called, the table
is made by applying mdbFind
to the target column.
The behavior is different depending on whether or not a
targetField
is specified. If there is no target field, then
all fields of the column are returned.
If there is a targetField
, then the jsonDecoder
function
is applied to its value, and it is joined with the app
,
uid
, context
, timestamp
fields from the header to
make the data table.
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
:A function
or a non-empty
character
scalar naming a function which will be used to encode
details(mess)
as a JSON object. The default is
unparseData
.
decoderEncoder
:A function
or character
scalar naming a function which will be used to decode
the target field when building a data table.
The default is parseData
.
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.
reset(app)
:Empties the database collection of messages with this app id.
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.
## Updating the data field.
fm <- mongo::fake_mongo(count=list(0L,1L))
ul <- UpdateListener("tester", db=fm,messSet=c("Scored Response"))
## New message, insert
mess1 <- P4Message("Fred","Task 1","Evidence ID","Scored Response",
as.POSIXct("2018-11-04 21:15:25 EST"),
list(correct=TRUE,selection="D"))
receiveMessage(ul,mess1)
## Message is update, update.
mess1a <- P4Message("Fred","Task 1","Evidence ID","Scored Response",
as.POSIXct("2018-11-04 21:15:40 EST"),
list(correct=TRUE,selection="D",key="D"))
receiveMessage(ul,mess1a)
### No target field, details are added to record.
data2json <- function(dat) {
jsonlite::toJSON(mongo::unboxer(dat))
}
upwind <- UpdateListener(messSet=c("Money Earned","Money Spent"),
db=mongo::MongoDB("Players",noMongo=TRUE),
targetField="",
jsonEncoder="data2json")
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))
receiveMessage(upwind,mess2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.