brilliant-idea-thumb1Watch out this article:

1. There’s more than meets the eyes as this article also prepares gently for the next lessons: How to create a Plug-In Architecture Application Framework in Rebol - with a parallel comparison with C#

2. This may be the most time-savier idea you will encounter in your life if you are like me. 80% of the time I’m searching Google for the same little snippet of code, the same site url by just typing the domain and other stuffs like that. Bookmarking and Storing on multiple social bookmarks and storage sites are great except that it becomes a hassle when you just need to pick up a tiny info like a connection string for Oracle.

With Rebol I have finally found this handy or agile solution much more powerfull and productive than just an url command line like YubNub. Let’s see how with an example.

Let’s say I’m looking for an Oracle Connection String:

1. I would type “connection string oracle” in my Firefox Search Box
search-google

2. I would look for the link. I’m lucky for this one, it’s the first link
connection-string-oracle

3. After clicking http://www.connectionstrings.com/oracle I will have to choose among all available Oracle Connection Strings and finally select the one I was looking for: the “Microsoft OLE DB Provider for Oracle”
connection-string-oracle2

4. I would then click and copy the one below to the clipboard
connection-string-oracle3

Repeating this a dozen of time in a week for different connection strings is daunting. So let’s streamline this process a little bit by storing the information in our KB.

First, create a KB directory under the same folder as user.r (for last version of rebol it should be in rebol install directory, if not see this article on what is user.r and where to find it):
knowledge-management-system

Then inside this KB folder, create a rebol file (ConnectionString.r) with this content:


Rebol []

ConnectionString.Context: Context [

  set 'ConnectionString.go func[][
    browse http://connectionstring.com
  ]

  set 'ConnectionString.Oracle.OleDB "Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;"
  set 'ConnectionString.Oracle.ODP "Data Source=TORCL;User Id=myUsername;Password=myPassword;"

]

which looks like this in Programmer’s Notepad:
pnotepad1
As you can see, you create a ConnectionString.Context as a Context which is Rebol’s shortcut for Make Object! and inside this Context you create and set the value of some properties to whatever you want. Note that “.” in the variable names have no special meaning for Rebol, it’s just convenient for us to use a syntax form that is similar to traditional OOP syntax in other languages.

Wait a minute: why did I use Set (as we saw Set and Get in previous lesson) like in:


set ‘ConnectionString.Oracle.OleDB “…”

instead of just:


ConnectionString.Oracle.OleDB: "..."

Because in the first case ConnectionString.Oracle.OleDB will be accessible from outside the Context (get allows kind of public-like property in other languages) whereas in the second case it will be private to the Context only.

To call this file and in fact all files that are residing in kb and kb subdirectories, add inside user.r this line:


foreach-file %kb/ :do-file

The foreach-file is a custom function we created in a previous lesson “How to apply a function to all files in a directory recursively” so that you should precede the snippet code above with the foreach-file function and the do-file function:


do-file: func[file][
  do file
]

foreach-file: func [
    "Perform function on each file in selected directory recursively"
    dir [file! url!] "Directory to look in"
    act [function!] "Function to perform (filename is unput to fuction)"
    /directory "Perform function also on directories"
    /local f files
][
    if not equal? last dir #"/" [
      dir: to-rebol-file join dir #"/"
    ]
    files: attempt [read dir]
    either none? files [return][
        foreach file files [
            f: join dir file
            either dir? f [
                either directory [
                    act f
                    foreach-file/directory f :act
                ][
                    foreach-file f :act
                ]
            ][act f]
        ]
    ]
]

You can now test it in Rebol’s Console (launch a new Rebol Console as to execute user.r once again):


help connectionstr

As you can see, you can just type a partial string you’re looking for. You should then get:


>> help connectionstring.
Found these words:
   ConnectionString.Context object! []
   ConnectionString.go function! []
   ConnectionString.Oracle.ODP string! {Data Source=TORCL;User Id=myUsername;Password=myP...
   ConnectionString.Oracle.OleDB string! {Provider=msdaora;Data Source=MyOracleDB;User Id=m...

Then copy ConnectionString.Oracle.ODP from the list and paste in again in Rebol’s Console, you will get the wanted value of Oracle ODP connection string:


>> ConnectionString.Oracle.ODP
== {Data Source=TORCL;User Id=myUsername;Password=myPassword;}

You can create any number of kb you like like adding a snippets kb like a csharp.snippets kb as shown on the screen below from my real kb:
free-knowledge-management-system

And above you can do more than just storing information. For example if you type


connectionstring.go

You will automatically be transported to http://connectionstring.com website.

You can create wizard for filling the connectingstring parameter with the build-markup function which we already saw multiple times like this:


  set 'ConnectionString.Oracle.ODP func[][
    ;"Data Source=TORCL;User Id=myUsername;Password=myPassword;"
    write clipboard:// return-result: build-markup {Data Source=<%ask "Data-Source: "%>;User Id=<%ask "user Name: " %>;Password=<%ask "Password: "%>;}
    return-result
  ]

Typing ConnectionString.Oracle.ODP in Console will launch a small Wizard which will ask you the parameters and then copy the result back into clipboard and console:
connection-string-oracle-demo

In conclusion, my life has become really brighter and as Carl seems to say Rebol’s Help will be improved in R3 the future will be marvellous ;). But if you want to improve it right now, you can as the help function is a mezzanine function (custom user function shipped with Rebol official package).

Reference: Help from Rebol Console.

Addendum: if you’re using Subversion, to avoid errors, modify the do-file function to ignore files without Rebol’s Header like this:


do-file: func[file][

  if error? error: try [
    do file
  ][
    disarm error
  ]

]

or even better:


do-file: func[file][
  if error? error: try [
    if (find [%.r %.cgi] (suffix? file)) [
      do file
    ]
  ][
    disarm error
    print ["error executing " file]
    input
  ]
]

For an introduction to try/disarm see previous lesson.

Bookmark and Share