Sha256: 6ca7de47b4e9bc2cc44dc53a0d32be0ec46081d00ca1ce1dad169bbecc15029b
Contents?: true
Size: 1.96 KB
Versions: 2
Compression:
Stored size: 1.96 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 < BasicObject 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 ||= self.__class__.builder.new end def out( str=nil ) if str @buffer << str else @builder_functor ||= Functor.new( &__self__.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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
facets-1.4.4 | lib/facets/more/builderobject.rb |
facets-1.4.5 | lib/facets/more/builderobject.rb |