Class BuildingBlock
In: lib/facets/more/buildingblock.rb
Parent: Object

BuildingBlock

Build content programatically with Ruby and Ruby‘s blocks.

  require 'facets'
  require 'xmlhelper'

  builder = BuildingBlocks.new(XMLHelper, :element)

  doc = builder.html do

    head do
      title "Test"
    end

    body do
      i "Hello"
      br
      text "Test"
      text "Hey"
    end

  end

produces

  <html><head><title>Test</title><body><i>Hello</i><br />TestHey</body></html>

All calls within the block are routed via the Helper Module‘s constructor method (element in the above example) unless they are defined by the helper module, in which case they are sent to the helper module directly. The results of these invocations are appended to the output buffer. To prevent this, prefix the method with ‘call_’.

Sometimes keywords can get in the way of a construction. In these cases you can ensure use of constructor method by calling the special build! command. You can also add verbatium text to the output via the #<< operator. All of Ruby‘s built-in keywords (eg. ‘while’, ‘if’, ‘until’, etc.) and the following methods are treated as keywords.

     method_missing
     initialize
     inspect
     to_str
     to_s
     respond_to?
     singleton_method_undefined

This work was of course inspired by many great minds, and represents a concise and simple means of accomplishing this pattern of design, which is unique to Ruby.

Methods

<<   build!   inspect   method_missing   new   to_s   to_str  

Constants

ESCAPE = [ 'singleton_method_undefined', 'method_missing', 'respond_to?', 'initialize', 'inspect', 'to_str', 'to_s', '<<', 'build!'

Public Class methods

Public Instance methods

If creating and XML/HTML builder, you‘ll want to alias this to tag!.

Could improve.

[Validate]