new_bids_class {bidsr} | R Documentation |
Create new bidsr
class definition
Description
By default, all generated classes inherit BIDSClassBase
,
which provides S3
generics
Usage
new_bids_class(
name,
parent = BIDSClassBase,
abstract = FALSE,
hidden_names = NULL,
properties = NULL,
methods = NULL,
validator = NULL,
constructor = NULL
)
Arguments
name |
string, required, name of the class |
parent |
parent class definition, needs to be a |
abstract |
whether the class is abstract ( |
vector of string, names of properties and/or methods
whose presence should be hidden from the users; this will affect | |
properties |
a named list where the names are the property names
that can be queried via |
methods |
read-only methods for the class, such as |
validator |
validate function; see |
constructor |
function to custom the constructor; see parameter
|
Value
A S7
object inheriting the 'bidsr::BIDSClassBase'
class.
Author(s)
Zhengjia Wang
Examples
# ---- Basic usage --------------------------------------------
Range <- new_bids_class(
"Range",
properties = list(
start = bids_property_numeric("start", "required"),
end = bids_property_numeric("end", "optional")
),
validator = function(self) {
if(length(self@end) && self@end < self@start) {
"@end must be great than or equal to @start"
}
}
)
r <- Range(start = 10)
r
# get and set properties with @ or $
r$start
r$end <- 40
r$end
try(Range(start = c(10, 15), end = 20))
try(Range(start = 15, end = 10))
# ---- hide properties and attributes -------------------------
MyClass <- new_bids_class(
name = "MyClass",
properties = list(
str = bids_property_character(
name = "str", type = "required"),
hidden_prop = bids_property_character("hidden_prop")
),
methods = list(
# read-only methods
format = function(self, ...) {
sprintf("MyClass@str -> %s", self$str)
},
hidden_method = function(self) {
"Nonononono"
}
),
hidden_names = c("hidden_method", "hidden_prop")
)
x <- MyClass(str = "a")
x
# hidden names will not be displayed
names(x)
as.list(x)
# however, then can still be queried
x$hidden_prop
x$hidden_method()