Title: | A Config Package with No "Con" |
---|---|
Description: | Work with configs with a source precedence. Either create own R6 instance or work with convenient functions at a package level. |
Authors: | Tymoteusz Makowski [cre, aut] |
Maintainer: | Tymoteusz Makowski <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-11-19 03:24:21 UTC |
Source: | https://github.com/tymekdev/fig |
Fig
class is a main driver of this package. For usage details
refer to Fig
class methods documentation.
Fig provides a set of exported functions. This makes Fig
class instance
creation optional, and makes the package itself mimic being a class instance.
Those functions are wrappers on an internal Fig
object.
new()
Create a New Fig Instance
Fig$new(env_prefix = "", split_on = ".")
env_prefix
(character) A prefix to be prepended to a key before system environment lookup.
split_on
(character) A value to split keys on. See Details section. Providing an empty string disables this behavior.
Fig treats character provided in split_on
as key nest level
delimiter. Therefore, split_on
set to "."
(default value)
fig$get("foo.bar")
is equivalent to fig$get("foo")$bar
. Similarly
fig$set("foo.bar", 1) is equivalent to fig$set("foo", list(bar = 1))
.
This behavior can be disabled either by passing an empty string either to
new()
during Fig
instance creation or to configure()
function to
modify an existing instance.
New instance of Fig
.
fig <- Fig$new() fig <- Fig$new(env_prefix = "RCONNECT_")
configure()
Configure a Fig Instance
Fig$configure(env_prefix, split_on)
env_prefix
(character) A prefix to be prepended to a key before system environment lookup. Pass an empty string to reset.
split_on
(character) A value to split keys on. See Details
section in new()
. Providing an empty string disables this behavior.
Unset arguments do not change configuration.
Reference to self. Other methods can be chained after this one.
fig <- Fig$new(env_prefix = "RCONNECT_") fig$configure(env_prefix = "foo_") fig$configure(split_on = "") fig$configure() # has no effect
delete()
Delete Stored Values
Fig$delete(...)
...
Keys to be deleted.
Reference to self. Other methods can be chained after this one.
fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2, "baz" = 3) fig$delete("foo") fig$delete("bar", "baz") fig$get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL)
delete_all()
Delete All Stored Values
Fig$delete_all()
Reference to self. Other methods can be chained after this one.
fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2, "baz" = 3) fig$delete_all() fig$get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL)
get()
Retrieve a Stored Value
Fig$get(key)
key
A key to retrieve a value for.
This function returns values based on a following priority (highest to lowest). If value is not found, then it looks up next level in the precedence.
System environment variable (case sensitive)
Value manually set
For system environment lookup dots are replaced by underscores, e.g.
fig$get("foo.bar")
will look up foo_bar.
A value associated with provided key
.
fig <- Fig$new() fig$store("foo", 1) fig$get("foo") fig$store("bar", list(baz = 2)) fig$get("bar.baz") fig$configure(split_on = "") fig$get("bar.baz") # == NULL
get_many()
Retrieve Any Number of Stored Values
Fig$get_many(...)
...
Keys to retrieve values for.
See get()
Details section.
An unnamed list of values associated with keys provided in ...
.
fig <- Fig$new() fig$store_many(foo = 1, bar = 2, baz = 3) fig$get_many("foo", "bar")
get_all()
Retrieve All Stored Values
Fig$get_all()
See get()
Details section.
An unnamed list of all stored values.
fig <- Fig$new() fig$store_many(foo = 1, bar = 2, baz = 3) fig$get_all()
store()
Store a Value
Fig$store(key, value)
key
A key to store a value for.
value
A value to be stored.
Reference to self. Other methods can be chained after this one.
fig <- Fig$new() fig$store("foo", 1) fig$store("bar", 123)$store("baz", list(1, 2, 3)) fig$store("x.y", "a")
store_list()
Store a List's Contents
Fig$store_list(l)
l
(named list) Names are used as keys for storing their values.
Reference to self. Other methods can be chained after this one.
fig <- Fig$new() fig$store_list(list(foo = 123, bar = "abc"))
store_many()
Set Any Number of Values
Fig$store_many(...)
...
Named arguments. Names are used as keys for storing argument values.
Reference to self. Other methods can be chained after this one.
fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2) fig$store_many("foo.bar.baz" = 1) fig$store_many("foo" = "a", "baz" = 123)
clone()
The objects of this class are cloneable with this method.
Fig$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------ ## Method `Fig$new` ## ------------------------------------------------ fig <- Fig$new() fig <- Fig$new(env_prefix = "RCONNECT_") ## ------------------------------------------------ ## Method `Fig$configure` ## ------------------------------------------------ fig <- Fig$new(env_prefix = "RCONNECT_") fig$configure(env_prefix = "foo_") fig$configure(split_on = "") fig$configure() # has no effect ## ------------------------------------------------ ## Method `Fig$delete` ## ------------------------------------------------ fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2, "baz" = 3) fig$delete("foo") fig$delete("bar", "baz") fig$get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL) ## ------------------------------------------------ ## Method `Fig$delete_all` ## ------------------------------------------------ fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2, "baz" = 3) fig$delete_all() fig$get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL) ## ------------------------------------------------ ## Method `Fig$get` ## ------------------------------------------------ fig <- Fig$new() fig$store("foo", 1) fig$get("foo") fig$store("bar", list(baz = 2)) fig$get("bar.baz") fig$configure(split_on = "") fig$get("bar.baz") # == NULL ## ------------------------------------------------ ## Method `Fig$get_many` ## ------------------------------------------------ fig <- Fig$new() fig$store_many(foo = 1, bar = 2, baz = 3) fig$get_many("foo", "bar") ## ------------------------------------------------ ## Method `Fig$get_all` ## ------------------------------------------------ fig <- Fig$new() fig$store_many(foo = 1, bar = 2, baz = 3) fig$get_all() ## ------------------------------------------------ ## Method `Fig$store` ## ------------------------------------------------ fig <- Fig$new() fig$store("foo", 1) fig$store("bar", 123)$store("baz", list(1, 2, 3)) fig$store("x.y", "a") ## ------------------------------------------------ ## Method `Fig$store_list` ## ------------------------------------------------ fig <- Fig$new() fig$store_list(list(foo = 123, bar = "abc")) ## ------------------------------------------------ ## Method `Fig$store_many` ## ------------------------------------------------ fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2) fig$store_many("foo.bar.baz" = 1) fig$store_many("foo" = "a", "baz" = 123)
## ------------------------------------------------ ## Method `Fig$new` ## ------------------------------------------------ fig <- Fig$new() fig <- Fig$new(env_prefix = "RCONNECT_") ## ------------------------------------------------ ## Method `Fig$configure` ## ------------------------------------------------ fig <- Fig$new(env_prefix = "RCONNECT_") fig$configure(env_prefix = "foo_") fig$configure(split_on = "") fig$configure() # has no effect ## ------------------------------------------------ ## Method `Fig$delete` ## ------------------------------------------------ fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2, "baz" = 3) fig$delete("foo") fig$delete("bar", "baz") fig$get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL) ## ------------------------------------------------ ## Method `Fig$delete_all` ## ------------------------------------------------ fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2, "baz" = 3) fig$delete_all() fig$get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL) ## ------------------------------------------------ ## Method `Fig$get` ## ------------------------------------------------ fig <- Fig$new() fig$store("foo", 1) fig$get("foo") fig$store("bar", list(baz = 2)) fig$get("bar.baz") fig$configure(split_on = "") fig$get("bar.baz") # == NULL ## ------------------------------------------------ ## Method `Fig$get_many` ## ------------------------------------------------ fig <- Fig$new() fig$store_many(foo = 1, bar = 2, baz = 3) fig$get_many("foo", "bar") ## ------------------------------------------------ ## Method `Fig$get_all` ## ------------------------------------------------ fig <- Fig$new() fig$store_many(foo = 1, bar = 2, baz = 3) fig$get_all() ## ------------------------------------------------ ## Method `Fig$store` ## ------------------------------------------------ fig <- Fig$new() fig$store("foo", 1) fig$store("bar", 123)$store("baz", list(1, 2, 3)) fig$store("x.y", "a") ## ------------------------------------------------ ## Method `Fig$store_list` ## ------------------------------------------------ fig <- Fig$new() fig$store_list(list(foo = 123, bar = "abc")) ## ------------------------------------------------ ## Method `Fig$store_many` ## ------------------------------------------------ fig <- Fig$new() fig$store_many("foo" = 1, "bar" = 2) fig$store_many("foo.bar.baz" = 1) fig$store_many("foo" = "a", "baz" = 123)
This function allows modifying configuration of the global fig. Refer to argument descriptions for available options.
fig_configure(env_prefix, split_on)
fig_configure(env_prefix, split_on)
env_prefix |
(character) A prefix to be prepended to a key before system environment lookup. Pass an empty string to reset. |
split_on |
(character) A value to split keys on. See Details
section in |
Unset arguments do not change configuration.
Reference to the global fig instance. Other methods can be chained after this one.
fig_configure(env_prefix = "foo_") fig_configure(split_on = "") fig_configure() # has no effect
fig_configure(env_prefix = "foo_") fig_configure(split_on = "") fig_configure() # has no effect
These functions allow deleting values stored in the global fig instance.
fig_delete(...) fig_delete_all()
fig_delete(...) fig_delete_all()
... |
Keys to be deleted. |
Reference to the global fig instance. Other methods can be chained after this one.
fig_store_many("foo" = 1, "bar" = 2, "baz" = 3) fig_delete("foo") fig_delete("bar", "baz") fig_get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL) fig_store_many("foo" = 1, "bar" = 2, "baz" = 3) fig_delete_all() fig_get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL)
fig_store_many("foo" = 1, "bar" = 2, "baz" = 3) fig_delete("foo") fig_delete("bar", "baz") fig_get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL) fig_store_many("foo" = 1, "bar" = 2, "baz" = 3) fig_delete_all() fig_get_many("foo", "bar", "baz") # == list(NULL, NULL, NULL)
These functions allow retrieving values stored in the global fig instance.
fig_get(key) fig_get_many(...) fig_get_all()
fig_get(key) fig_get_many(...) fig_get_all()
key |
A key to retrieve a value for. |
... |
Keys to retrieve values for. |
These functions return values based on a following priority (highest to lowest). If value is not found, then it looks up next level in the precedence.
System environment variable (case sensitive)
Value manually set
For system environment lookup dots are replaced by underscores, e.g.
fig_get("foo.bar")
will look up foo_bar.
A value associated with provided key
.
An unnamed list of values associated with keys provided in ...
.
An unnamed list of all stored values.
fig_store("foo", 1) fig_get("foo") fig_store("bar", list(baz = 2)) fig_get("bar.baz") fig_configure(split_on = "") fig_get("bar.baz") # == NULL fig_store_many(foo = 1, bar = 2, baz = 3) fig_get_many("foo", "bar") fig_store_many(foo = 1, bar = 2, baz = 3) fig_get_all()
fig_store("foo", 1) fig_get("foo") fig_store("bar", list(baz = 2)) fig_get("bar.baz") fig_configure(split_on = "") fig_get("bar.baz") # == NULL fig_store_many(foo = 1, bar = 2, baz = 3) fig_get_many("foo", "bar") fig_store_many(foo = 1, bar = 2, baz = 3) fig_get_all()
These functions allow storing values in the global fig instance.
fig_store(key, value) fig_store_list(l) fig_store_many(...)
fig_store(key, value) fig_store_list(l) fig_store_many(...)
key |
A key to store a value for. |
value |
A value to be stored. |
l |
(named list) Names are used as keys for storing their values. |
... |
Named arguments. Names are used as keys for storing argument values. |
Reference to self. Other methods can be chained after this one.
fig_store("foo", 1) fig_store("bar", 123)$store("baz", list(1, 2, 3)) fig_store("x.y", "a") fig_store_list(list(foo = 123, bar = "abc")) fig_store_many("foo" = 1, "bar" = 2) fig_store_many("foo.bar.baz" = 1) fig_store_many("foo" = "a", "baz" = 123)
fig_store("foo", 1) fig_store("bar", 123)$store("baz", list(1, 2, 3)) fig_store("x.y", "a") fig_store_list(list(foo = 123, bar = "abc")) fig_store_many("foo" = 1, "bar" = 2) fig_store_many("foo.bar.baz" = 1) fig_store_many("foo" = "a", "baz" = 123)