class Fragment
Represents a lazy fragment of markup that can be generated on demand.
Definitions
def initialize(block)
Initialize the fragment with a block that will be called to generate the markup.
Signature
-
parameter
block
Proc
the block that will be called to generate the markup.
Implementation
def initialize(block)
@block = block
@builder = nil
end
def append_markup(output)
Append the markup to the given output.
Implementation
def append_markup(output)
builder = Builder.new(output)
self.build_markup(builder)
return builder.output
end
def build_markup(builder)
Build the markup using the given builder.
Implementation
def build_markup(builder)
@block.call(builder)
end
def to_s
Convert the fragment to a string.
Signature
-
returns
MarkupString
the markup generated by the fragment.
Implementation
def to_s
self.append_markup(nil)
end
def == other
Compare the fragment to another object as a string. Primarily useful for testing.
Implementation
def == other
# This is a bit of a hack... but is required for existing specs to pass:
self.to_s == other.to_s
end
def >> block
Assuming the fragment is generated in the context of a template being rendered, append the fragment to the output buffer.
Signature
-
parameter
block
Proc
the block which is within the scope of the template being rendered.
Implementation
def >> block
if block
Template.buffer(block.binding) << self
return nil
else
return self
end
end