Create Builder Applications Using Cheri
Author: ceefour | Filed under: Cool, Rails, Ruby, Tips, Tools, Web 2.0If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
The
builder–builder is a tool for creating (or extending) builder applications, or more simply, builders. Let’s read more about builder–builder!
Well, before we go any further about it, we must know the meaning of a builder. What is it??
A builder is a domain-specific language (DSL) for creating and/or representing hierarchies of related objects using a declarative syntax. By declarative syntax, I mean one that describes the (resultant) hierarchical structure, rather than the means by which it is created. The Cheri builder–builder, then, is a meta-DSL for creating builder DSLs. (However, in light of the above definition, the builder–builder is not, strictly speaking, a meta-builder, or a builder at all, as its syntax is not strictly declarative.)
This is provides an easy way to create builders based on the Cheri builder framework/engine (as are Cheri::Swing, Cheri::Xml and Cheri::Html). Before I bore you with a BNF diagram of Cheri’s particular take on builder syntax (honestly, I was about to), let me move on to an example, which should make things quite clear. It has 3 class information about the store, the authors, and the books.
A Really Simple Builder, about the store:
class Bookstore
attr :name, true
attr :address, true
attr :phone, true
attr :authors
def initialize(name=nil)
@name = name
@authors = []
end
def add(author)
@authors << author
end
end
Cheri’s type matching logic works from most-specific class/module (nearest ancestor) to least, so this code will only be invoked if a would-be child object can’t be connected based on a more specific ancestor than Object. [Note that an upcoming version of the builder–builder will provide a shorthand method of specifying warnings/exceptions for unconnected objects.]
Here’s Our completed builder:
BookstoreBuilder = Cheri::Builder.new_builder do
# specify each class to be built
build Bookstore
build Author
build Book
# specify how objects of each type connect to other objects
type Bookstore do
connect Author, :add_author
end
type Author do
connect Book, :add_book
end
# issue warning for unconnected objects
type Object do
connect Object do |parent, child, symbol|
warn "warning: can't connect #{child.class} (#{symbol}) to #{parent.class}"
end
end
end
We’ve been working with a very simple model: Bookstore => Author => Book.
For further details visit Cheri project page.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.