| Hi, I've written a scaffolding system in arc for avoiding repetitive code when implementing similar (but not the identical) entities in an application. The classic example for this is a website that has products, customers, orders, etc- Each of these needs to be tracked by themselves and there must be similar (but different) mechanisms for creating/updating/deleting each on a site. My system extends the current arc template system and uses an extremely minimalistic approach, in keeping with arc. The current system has the following overall relationship: template -> instance
Scaffolding.arc defines new functions that models the following relationship: scaffold template -> scaffold instance -> entity
In concrete terms, you might have a template for web interactions called webscaff created using "deftem". You would create an instance of the template called mybooksite using "inst". Then you can call "inst-entity" (a new command) on mybooksite to generate versions of the source code for the customers, books, orders, suppliers, etc.For those of you who know Rails, this is analogous to Rails scaffolding, except that the code isn't created by a byzantine code generator. Instead, it is created dynamically when the program is loaded, basically using macro-like operations. The scaffold and app can therefore change independently and are stitched together dynamically when you load the app. This was designed to be in keeping with the arc/lisp philosophy. To start off, I created a sample scaffold template defined in webscaff.arc. It is pretty much a line-by-line refactoring of pg's blog.arc. Here is an example of the scaffolding code that handles a request to view an individual item through the website: (addtemscaff webscaff view
`(defop ,ename!view req
(aif (,(ename) (arg req "id"))
(,ename!-singleton-page (get-user req) it)
(notfound))))
"addtemscaff" is a macro basically identical to addtem tweaked a bit for making scaffolding a little easier. This code creates a new property in the webscaff template. The anaphoric variable "ename" contains the entity name. It will be expanded at the time each entity is defined, creating different pages such as "bookview", "customerview", "orderview", etc. More accurately, "ename" is a function that returns the entity name as a result, but can be given an extra parameter that will be concatenated as a suffix. This means if the entity is "book" then (ename "add") creates the symbol "bookadd". If you use the intrasymbol arc syntax, this can be elegantly written as ename!addOther anaphoric variables available from the scaffolds are sname (the name of the scaffold, i.e. "mybooksite") scaffold (allows you to access the instance of the current scaffold directly in case you've put extra scaffold-level data points there, like a title for your site) and entity (allows you to access any optional data points sent to the inst-entity command- The most common such data points would be the field names/types for a book or whatever) As a working example, I created a Blog app with comment support. It is in "blog-fancy.arc". As you will see, the amount code needed for this app is minimal. It just runs inst-entity once for "posts" and once for "comments". Then, it overrides a bit of the scaffolded code so that the program implements the one->many relationship between posts and comments. This means you can create comments linked to a post and can view post-specific comments. Note that most of the derived functions are just slightly modified from the scaffold- With additional refactoring of the scaffolding system, I plan to reduce this code even more drastically. Also note that nothing in scaffolding.arc is web specific (only the specific scaffold declared in webscaff.arc) and could conceivably be used in other contexts. It could also be inherited from (deftem allows inheritance) and specific fragments overridden by the new class by using the property names assigned to the parts of the scaffold. This is definitely still a very early implementation of this scaffolding system. I hope to push improvements to anarki over time. Please feel free to push your own changes as well if you like- There is definitely much room for improvement at this time. The files are on anarki:
http://git.nex-3.com/arc-wiki.git -Conrad Barski |