diff options
author | Gianni Ceccarelli <gianni.ceccarelli@broadbean.com> | 2023-07-13 17:29:26 +0100 |
---|---|---|
committer | Gianni Ceccarelli <gianni.ceccarelli@broadbean.com> | 2023-07-13 17:29:26 +0100 |
commit | c3141fc3b3c8d6fb7270a3a1cd15be4895ea052d (patch) | |
tree | 51e0ab1cca45f5d9491ab04b2a9ea80425a40474 /bos-namespaces.sh | |
parent | some docs (diff) | |
download | bash-object-system-c3141fc3b3c8d6fb7270a3a1cd15be4895ea052d.tar.gz bash-object-system-c3141fc3b3c8d6fb7270a3a1cd15be4895ea052d.tar.bz2 bash-object-system-c3141fc3b3c8d6fb7270a3a1cd15be4895ea052d.zip |
flailing about with MOPs
Diffstat (limited to 'bos-namespaces.sh')
-rw-r--r-- | bos-namespaces.sh | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/bos-namespaces.sh b/bos-namespaces.sh new file mode 100644 index 0000000..3e9e711 --- /dev/null +++ b/bos-namespaces.sh @@ -0,0 +1,63 @@ +#!bash +## namespace handling + +function bos-namespaces/store-for-into() { + local name="bos_namespaces__${2//[^[:word:]/__}__${1//[^[:word:]]/__}" + local -n result="$2" + declare -gA "$name" + result="$name" +} + +# save all currently-visible functions in an associative array +function bos-namespaces/start() { + local namespace="$1";shift + local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store + local -n saved_funcs="$ns_store" + local _ funcname + + while read -r _ _ funcname; do + saved_funcs+=( ["$funcname"]="$(declare -pf "$funcname")" ) + done < <( declare -pF ) + + return 0 +} + +# diff all currently-visible function against the saved ones, list the +# ones that differ +function bos-namespaces/list-new-funcs-into() { + local namespace="$1";shift + local -n result="$1" + local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store + local -n saved_funcs="$ns_store" + local _ funcname new_function + + while read -r _ _ funcname; do + new_function="$(declare -pf "$funcname")" + if [[ "$new_function" != "${saved_funcs["$funcname"]}" ]]; then + result+=( "${funcname}" ) + fi + done < <( declare -pF ) + + return 0 +} + +# rename a bunch of functions by prepending a prefix; restore the +# saved function of the same name, if it exists +function bos-namespaces/qualify-funcs() { + local namespace="$1";shift + local ns_store; bos-namespaces/store-for-into saved_funcs "$namespace" ns_store + local -n saved_funcs="$ns_store" + local _ funcname new_function + + for funcname in "$@"; do + new_function="$(declare -pf "$funcname")" + eval "${namespace}/${new_function}" + if [[ -n "${saved_funcs["$funcname"]}" ]]; then + eval "${saved_funcs["$funcname"]}" + else + unset -f "$funcname" + fi + done + + return 0 +} |