Sha256: 9d058cfa05b07c2158bead28764008e0afe9d325667d4c805f58b9bab763dc62

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

# = builderobject.rb
#
# == Copyright (c) 2006 Thomas Sawyer
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.
#
# == Authors and Contributors
#
# * Thomas Sawyer

# Author::    Thomas Sawyer
# Copyright:: Copyright (c) 2006 Thomas Sawyer
# License::   Ruby License

#require 'facets/more/basicobject'
require 'facets/more/functor'

# = BuilderObject
#
# Build content programatically with Ruby and Ruby's blocks.
#
# Builders can use either an implict or explicit receiver.
# Explicit is the default. To use implicit pass the :implicit
# option to the constructor.
#
# Implict building is more elegant in form, but it is not as
# functional because it makes it more difficult to refer to
# external references.
#
# BuilderObject avoides metrhod name clashes by using
# Functor redirection. Unlike other implementations of
# the Builder patterns which append '!' to builder methods
# or simply use odd names to avoid clashes, BuilderObject
# routes all builder method vis the #out method.
#
# NOTE The name of this method (#out) may be changed.

class BuilderObject

  private *instance_methods

  def self.builder
    @builder ||= Class.new
  end

  def self.builder_include( mod )
    builder.class_eval { include mod }
  end

  def initialize
    @stack = []
    @buffer = ''
  end

  def builder
    @builder ||= object_class.builder.new
  end

  def out( str=nil )
    if str
      @buffer << str
    else
      @builder_functor ||= Functor.new( &send(:method,:+) )
    end
  end

  def +( op, *args, &blk )
    @buffer << builder.send( op, *args, &blk )
  end

  def to_s
    @buffer
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

# TODO

=begin
=end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-1.7.0 lib/facets/more/builderobject.rb
facets-1.7.30 lib/facets/more/builderobject.rb
facets-1.7.38 lib/facets/more/builderobject.rb
facets-1.7.46 lib/facets/more/builderobject.rb