Class: Utopia::Content::Builder
- Inherits:
-
XRB::Builder
- Object
- XRB::Builder
- Utopia::Content::Builder
- Defined in:
- lib/utopia/content/builder.rb
Overview
A builder for rendering Utopia content that extends XRB::Builder with Utopia-specific functionality.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#deferred ⇒ Object
readonly
Returns the value of attribute deferred.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#tags ⇒ Object
readonly
A list of all tags in order of rendering them, which have not been finished yet.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #call(document) ⇒ Object
- #defer(value = nil, &block) ⇒ Object
-
#empty? ⇒ Boolean
Whether this state has any nested tags.
-
#initialize(parent, tag, node, attributes = tag.to_hash, **options) ⇒ Builder
constructor
A new instance of Builder.
- #tag_begin(tag) ⇒ Object
- #tag_complete(tag) ⇒ Object
- #tag_end(tag) ⇒ Object
-
#text(content) ⇒ Object
Override text to handle build_markup protocol.
-
#write(string) ⇒ Object
Override write to directly append to output.
Constructor Details
#initialize(parent, tag, node, attributes = tag.to_hash, **options) ⇒ Builder
Returns a new instance of Builder.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/utopia/content/builder.rb', line 14 def initialize(parent, tag, node, attributes = tag.to_hash, **) super(**) @parent = parent @tag = tag @node = node @attributes = attributes @content = nil @deferred = [] = [] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
29 30 31 |
# File 'lib/utopia/content/builder.rb', line 29 def attributes @attributes end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
30 31 32 |
# File 'lib/utopia/content/builder.rb', line 30 def content @content end |
#deferred ⇒ Object (readonly)
Returns the value of attribute deferred.
36 37 38 |
# File 'lib/utopia/content/builder.rb', line 36 def deferred @deferred end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
31 32 33 |
# File 'lib/utopia/content/builder.rb', line 31 def node @node end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
27 28 29 |
# File 'lib/utopia/content/builder.rb', line 27 def parent @parent end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
28 29 30 |
# File 'lib/utopia/content/builder.rb', line 28 def tag @tag end |
#tags ⇒ Object (readonly)
A list of all tags in order of rendering them, which have not been finished yet.
34 35 36 |
# File 'lib/utopia/content/builder.rb', line 34 def end |
Instance Method Details
#[](key) ⇒ Object
44 45 46 |
# File 'lib/utopia/content/builder.rb', line 44 def [](key) @attributes[key] end |
#call(document) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/utopia/content/builder.rb', line 48 def call(document) @content = @output.dup @output.clear if node.respond_to? :call node.call(document, self) else document.parse_markup(@content) end return @output end |
#defer(value = nil, &block) ⇒ Object
38 39 40 41 42 |
# File 'lib/utopia/content/builder.rb', line 38 def defer(value = nil, &block) @deferred << block XRB::Tag.closed(DEFERRED_TAG_NAME, :id => @deferred.size - 1) end |
#empty? ⇒ Boolean
Whether this state has any nested tags.
82 83 84 |
# File 'lib/utopia/content/builder.rb', line 82 def empty? .empty? end |
#tag_begin(tag) ⇒ Object
86 87 88 89 |
# File 'lib/utopia/content/builder.rb', line 86 def tag_begin(tag) << tag tag.write_opening_tag(@output) end |
#tag_complete(tag) ⇒ Object
77 78 79 |
# File 'lib/utopia/content/builder.rb', line 77 def tag_complete(tag) tag.write(@output) end |
#tag_end(tag) ⇒ Object
91 92 93 94 |
# File 'lib/utopia/content/builder.rb', line 91 def tag_end(tag) raise UnbalancedTagError.new(tag) unless .pop.name == tag.name tag.write_closing_tag(@output) end |
#text(content) ⇒ Object
Override text to handle build_markup protocol
67 68 69 70 71 72 73 74 75 |
# File 'lib/utopia/content/builder.rb', line 67 def text(content) return unless content if content.respond_to?(:build_markup) content.build_markup(self) else XRB::Markup.append(@output, content) end end |
#write(string) ⇒ Object
Override write to directly append to output
62 63 64 |
# File 'lib/utopia/content/builder.rb', line 62 def write(string) @output << string end |