Updated 2018-08-13 16:48:33 by Stu

DruMa - Teach an AitCS new moves.

Found at https://chiselapp.com/user/stwo/repository/druma

Stu 2018-08-13 Created page. Version 0.1.

A DruMa is an object that augments an AitCS (Array in the Command's Shadow) by being an object that doesn't "wrap" the AitCS but "links" with it.

New functionality includes field definition, validation, loading and saving.

A DruMa may be used where a program would normally use an AitCS.

Example

#! /bin/sh
# \
exec tclsh${TCL} "$0" "$@"

#
# Breakfast - DruMa example at breakfast time.
#
# When run, this program will
# create the file "breakfast.txt" in the current directory.
#
#
# Stuart Cassoff
# Summer 2018
#

namespace eval myBreakfastWithDruMa {

        # Start your morning with DruMa!

        package require druma
        namespace import ::druma::DruMa


        # Create a DruMa, and by consequence, an AitCS.
        # Supply fields and boolFields at creation time.

        DruMa new breakfast \
                -fields {toast jam cereal coffee togo} \
                -boolFields {cereal togo}


        # Setup fields. Note the bad bool!

        breakfast {*}{
                toast   2
                jam     durian
                coffee  "many cups of delicious"
                cereal  "I'm the bad bool!"
        }


        # Call with "G" to invite Godzilla.

        breakfast togo [expr {[llength $::argv] == 1 && [lindex $::argv 0] eq "G"}]


        # Call the DruMa validate method to correct any bad fields (currently only bools).

        breakfast: validate


        # Setup other breakfast elements that DruMa doesn't care about.

        breakfast time    eventually
        breakfast guests  [list Bob Connie Laika Reagan]
        breakfast nguests [llength [breakfast guests]]


        # Godzilla tends to destroy the breakfast nook. Sorry.

        if {[breakfast togo]} {        # Bad bool field has been corrected!
                breakfast+ guests Godzilla
                breakfast+= nguests
        }


        # Another bad bool!

        breakfast cereal muesli


        # Save. Bad fields will be corrected. The listFile will be set.

        breakfast: save breakfast.txt


        # Mess up breakfast.

        breakfast toast  -100
        breakfast coffee decaf


        # When do we eat?

        breakfast time morning


        # Load. Bad fields will be corrected. The listFile was set when breakfast was saved.

        breakfast: load


        # Adjust for certain guests who may eat more than others. Just sayin'.

        if {"Godzilla" in [breakfast guests]} {
                breakfast   cereal 1
                breakfast+= toast 2
        }


        # We're out of durian jam. Hope strawberry's ok.

        breakfast jam strawberry


        # Let's eat!

        puts "[breakfast nguests] are having breakfast in the\
                [expr {[breakfast togo] ? "park adjacent" : "breakfast nook"}]!"

        puts "This [breakfast time], our guests are\
                [join [lrange [breakfast guests] 0 end-1] ", "],\
                and [lindex [breakfast guests] end]."

        puts "They'll be having\
                [breakfast toast] slices of toast with\
                [breakfast jam] jam,[expr {[breakfast cereal] ? " cereal," : ""}]\
                and [breakfast coffee] coffee."


        # Clean up / Check cleanup went ok

        set obj [breakfast:]
        set arr [breakfast&]
        set cmd [breakfast&&]

        set ok 0

        incr ok [info object isa object $obj]
        incr ok [array exists $arr]
        incr ok [expr {[llength [info commands $cmd]] == 1}]

        if {$ok != 3} { error "Breakfast cleanup: where is breakfast?" }

        breakfast---

        incr ok [info object isa object $obj]
        incr ok [array exists $arr]
        incr ok [expr {[llength [info commands $cmd]] == 1}]

        if {$ok != 3} { error "Breakfast cleanup: leftovers" }

}; #namespace eval myBreakfastWithDruMa

# EOF

Running the example
$ ./breakfast.tcl                                                                                          

4 are having breakfast in the breakfast nook!
This morning, our guests are Bob, Connie, Laika, and Reagan.
They'll be having 2 slices of toast with strawberry jam, and many cups of delicious coffee.


$ ./breakfast.tcl G

5 are having breakfast in the park adjacent!
This morning, our guests are Bob, Connie, Laika, Reagan, and Godzilla.
They'll be having 4 slices of toast with strawberry jam, cereal, and many cups of delicious coffee.


$ cat breakfast.txt

cereal  no
coffee  {many cups of delicious}
jam     durian
toast   2
togo    yes


[Category Array Object]