application_configuration
If you have tried to load or persist Objects in traditional languages, you know how difficult it can be because of impedance mismatch between the Object and Flat/Relational Models. XML is bridging the Gap but XML is not human readable and there are a lot of boiler or framework code just to do the plumbing. Well, in Rebol, Object Persistence is so trivial it may not even be worth mentioning it for Rebol Veterans, but for you who are newcomer and maybe used to DataTable, DataSet, DataAdapter etc. you may have to un-learn and go back to down-to-earth simplicity :)

As the best practice in programming is to store some parameters in an application configuration file, we’ll choose this as example. You can use ini file or xml file (we’ll see how to parse an XML file in a future lesson). But in Rebol, it’s better to use a specific format that is both easy to read by human and to interpret by the scripting engine that is Rebol’s object format. As it’s so easy to do so in Rebol that even a simple script should use it.

I have learned to do so by looking at 3Flex configuration file - 3flex.preferences - which contains:


inactive-node-color: 	240.235.230
active-node-color: 	255.250.250
workspace-color:	255.255.255

minimal-node-size:	100x35
most-recent-file:	none

drawer-header-height: 20
drawer-color: 240.240.240

To read this file, the instruction is simply:


prefs: construct load %3flex.preferences

Construct creates an object, but without evaluating its specification. Load binds Words to Global Context.

To access a preference, write something similar to this


color: prefs/workspace-color

Update! forgot to tell you how to save back the preferences: you have to save the body of the prefs object (you have learnt how to in Rebol’s Reflection):


 (pick to-block mold prefs 3)

test it with:


prefs/workspace-color:	0.0.255
save %3flex.preferences (pick to-block mold prefs 3)

In conclusion, Color is a Rebol’s Object that has been constructed dynamically from a persistent storage and which can be easily saved back to disk.

Note: save prefs also works but save under this format (which may be usefull for more complex hierarchical configuration needs):


make object! [
    inactive-node-color: 240.235.230
    active-node-color: 255.250.250
    workspace-color: 255.255.255
    minimal-node-size: 100x35
    most-recent-file: none
    drawer-header-height: 20
    drawer-color: 240.240.240
]

Update September 3rd: Peter W A Wood (see comments section) advocates the use of blocks of name/value pairs for preference files.

I believe they are simpler, quicker and more elegant.


>> prefs: load %3flex.preferences
== [
    inactive-node-color 240.235.230
    active-node-color 255.250.250
    workspace-color 255.255.255
    minimal-node-size...
>> prefs/workspace-color
== 255.255.255
>> prefs/workspace-color: 254.254.254
== 254.254.254
>> save %3flex.preferences prefs
>> prefs: load %3flex.preferences
== [
    inactive-node-color 240.235.230
    active-node-color 255.250.250
    workspace-color 254.254.254
    minimal-node-size.

Bookmark and Share