Learn R Programming

objectProperties (version 0.5.1)

setProperties: Properties class

Description

Encapsulates the properties for an operation.

Usage

setProperties(prefix, properties, contains=character(), where=topenv(parent.frame()),
    signalName="changed")

Arguments

prefix
Prefix for new subclass of Properties, e.g. if prefix is "Graphic", the new subclass name would be GraphicProperties.
properties
A list of properties with the names and class it belongs to, those properties are set as signaling fields.
contains
What class does this class extended besides Properties.
where
the environment in which to store or remove the definition. Defaults to the top-level environment of the calling function (the global environment for ordinary computations, and the environment or namespace of a package in the source code for that package)
signalName
Default name is "changed". A global signal for properties are defined with this specified name, whichever the properties changed, this signal will be emitted and the name of trigered field will be captured. Please check the example.

Value

  • A reference class generator for subclass.

Details

Each type of object should have a corresponding subclass of Properties. setProperties is a convenient subclass generator for class Properties, user could pass a list of different types of variables and return a specific Properties subclass generator, this new defined object store those properties as signaling fields, so it's able to connect signal to listen to individual property or the set as a whole. In this way, validation is enabled when user try to set the properties to new value.

Properties object has following methods [object Object]

Examples

Run this code
require(objectProperties)
gplist <- list(size = "numeric",
               color = "character")
GraphPars.gen <- setProperties("Graph", gplist, signalName = "GraphSignal")
obj <- GraphPars.gen$new(size = 1, color = "red")
class(obj)
## show the properties
obj$properties()
## convert the properties to a list
as(obj, "list")
as.list(obj)
## register global signals
obj$GraphSignal$connect(function(name){
  cat(name, "changed
")
})
obj$size <- 2

Run the code above in your browser using DataLab