Class: XRB::Builder::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/xrb/builder.rb

Overview

Represents a lazy fragment of markup that can be generated on demand.

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Fragment

Initialize the fragment with a block that will be called to generate the markup.



18
19
20
21
# File 'lib/xrb/builder.rb', line 18

def initialize(block)
  @block = block
  @builder = nil
end

Instance Method Details

#==(other) ⇒ Object

Compare the fragment to another object as a string. Primarily useful for testing.



44
45
46
47
# File 'lib/xrb/builder.rb', line 44

def == other
  # This is a bit of a hack... but is required for existing specs to pass:
  self.to_s == other.to_s
end

#>>(block) ⇒ Object

Assuming the fragment is generated in the context of a template being rendered, append the fragment to the output buffer.



51
52
53
54
55
56
57
58
59
# File 'lib/xrb/builder.rb', line 51

def >> block
  if block
    Template.buffer(block.binding) << self
    
    return nil
  else
    return self
  end
end

#append_markup(output) ⇒ Object

Append the markup to the given output.



24
25
26
27
28
29
30
# File 'lib/xrb/builder.rb', line 24

def append_markup(output)
  builder = Builder.new(output)
  
  self.build_markup(builder)
  
  return builder.output
end

#build_markup(builder) ⇒ Object

Build the markup using the given builder.



33
34
35
# File 'lib/xrb/builder.rb', line 33

def build_markup(builder)
  @block.call(builder)
end

#to_sObject

Convert the fragment to a string.



39
40
41
# File 'lib/xrb/builder.rb', line 39

def to_s
  self.append_markup(nil)
end