Last Modified
2011-04-15 18:11:14 +0200
Requires
  • fileutils

Description

RCodeLeveler main library.

RCodeLeveler is a Ruby preprocessor (used when requiring files) that interprets some comments in source files to enable or disable parts of the source code.

Its goal is to activate parts of the source code without any performance issue. For example, logging some information only when the program is run with a given option. The simple implementation would be to dynamically test a condition enabling some parts of the code, but sometimes even a simple condition testing is harmful enough to performances to want to comment/uncomment the function call when needed. RCodeLeveler does it for you.

As it alters tbe source code (in memory) before being interpreted by Ruby, it is not fit to enable source code based on dynamic conditions. It is used while requiring a Ruby source file. The conditions can be dynamically computed before requiring the file, but once it has been done it is too late: disabled code cannot be enabled back (unless you clear every class/module/constant defined by this file, delete its reference from Ruby’s loaded files’ cache and re-require it).

In order to use the functionalities of RCodeLeveler, you have to require your Ruby files using requireLevel function instead of require. Please note it works only with Ruby files.

The files being required using requireLevel can then use some special comments:

Level sections can be hierarchically embedded one in another. If the inclusion of a level section is useless (because it will have the same activation level as its surrounding level section), RCodeLeveler will issue a warning during the file parsing.

The following functions can be used to set levels before requiring the file:

Please note that using only ##__LVL__, ##__LVLCAT__ and ##__LVLRUBY__ directives enables you to also require the file without RCodeLeveler, with all leveled code commented. This can be useful if you use RCodeLeveler to just activate source code for debugging purposes only.

You can also define different behaviour while encountering warnings during a file’s parsing with function:

where iWarningSeverity can be:

It is also possible to get the content of a file once leveled in a list of strings using function RCodeLeveler.getLeveledFileContent(iLibraryName). RCodeLeveler can also write in a repository all files that have been leveled, by settinhg a directory name using function RCodeLeveler.setOutputDirectory(iDirectory) (setting the argument to nil disables the functionality). Those 2 last functionalities (added in versions >= 0.1.X) provide ways to release your files without any dependency on RCodeLeveler itself: you use it to generate your Ruby files leveled, and then you can require them normally without RCodeLeveler.

Please check the tests to find examples of all possible functionalities and uses.