druma(n) 0.1 druma "DruMa"

Name

druma - Teach an AitCS new moves.

Table Of Contents

Synopsis

Description

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.

In this document, the term or argument "aname" or "aname<something>" refers to the AitCS name supplied when the DruMa is created (ex: "name!").

COMMANDS

DruMa new aname ?-fields fields? ?-boolFields boolFields? ?-listFile listFile?

Creates a DruMa, creates an AitCS named aname, links them together. Returns the newly-created DruMa.

Linking a DruMa with an AitCS is accomplished by two steps. First, a DruMa accessor alias "aname:" is added to the AitCS. Second, the "aname---" destructor alias is changed to call the DruMa's destructor method. Destroying the DruMa or invoking the destructor alias will destroy both the DruMa and the AitCS. There is no need to save the DruMa in a variable since it can be accessed via the AitCS.

AITCS COMMANDS

Added or modified AitCS commands.

aname: ?method ?arg...??

Added command. Calls a method on the DruMa linked with the AitCS, passing any args. Returns the value returned from the method or the DruMa if called without a method. Nickname: Object accessor.

aname---

Modified command. Calls the DruMa's destructor. The DruMa's destructor will call the AitCS's destructor. Returns an empty string. Nickname: Destroy.

DRUMA ACCESSORS

DruMa accessors get or set the value of DruMa variables. Read-only variables take no argument.

fields ?fields?
boolFields ?boolFields?
listFile ?listFile?
name

Returns the name of the AitCS linked with the DruMa.

aitcs

Returns the AitCS linked with the DruMa.

DRUMA METHODS

load ?listFile?

Loads the data from the DruMa's listFile into the AitCS then validates. Filter input using fields. If listFile is supplied then the DruMa's listFile will be set to listFile before loading.

save ?listFile?

Validates then saves the data from the AitCS to the DruMa's listFile. Filter output using fields. If listFile is supplied then the DruMa's listFile will be set to listFile before saving.

validate

Validates all fields. Currently only boolFields.

validateBools

Validates all bool fields.

scanBool bool

Returns 1 if bool is true or 0 if bool is false or not a bool.

formatBool bool

Returns "yes" if bool is true or "no" if bool is false.

LIST FILE

A list file is a text file where each line is a two-element Tcl list as a name-value pair. When loading, lines beginning with "#" and blank lines are ignored.

EXAMPLES

This example code exists in the file examples/breakfast.tcl.

	# 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" }

Running the example code.

	$ ./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

In order to get the example to format properly, lines normally terminated with "\" are terminated here with "\ ". When copying the example code, adjust for this.

See Also

aitcs(n)

Keywords

alias, array, command, drunken, eagle, master, namespace, object, shadow, snake

Category

Programming tools