Sha256: c243220f1414c4d69bb913e3a8aacea6bf15b178afc9a5a62883961bd8e46bbd

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Glue

# A Builder integrates a number of Modules containing text 
# manipulation utilities and provides an alternative
# 'accomulation' interface.

class Builder

  # The builder output is accomulated in the buffer.
  
  attr_accessor :buffer
  
  class << self 
  
    def include_builder(*modules)
      for mod in modules
        include mod      
        for meth in mod.public_instance_methods
          self.module_eval %{
            alias_method :_mixin_#{meth}, :#{meth}
            def #{meth}(*args)
              @buffer << _mixin_#{meth}(*args)
              return self
            end
          }
        end      
      end
    end
    alias_method :builder, :include_builder
    
  end

  # Provide the target where the builder output will be
  # accomulated. The builder utilizes duck typing to make it
  # compatible with any target responding to <<.
  
  def initialize(buffer = '', options = {})
    @buffer = buffer    
  end  
  
  # Emit a text string.

  def text!(str)
    @buffer << str

    return self
  end
  alias_method :print, :text!
  alias_method :<<, :text!
  
  def to_s
    @buffer.to_s
  end
  
end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
glue-0.31.0 lib/glue/builder.rb
glue-0.30.0 lib/glue/builder.rb