Class: Utopia::Content::Builder

Inherits:
XRB::Builder
  • Object
show all
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

Instance Method Summary collapse

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, **options)
  super(**options)
  
  @parent = parent
  @tag = tag
  @node = node
  @attributes = attributes
  
  @content = nil
  @deferred = []
  @tags = []
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



29
30
31
# File 'lib/utopia/content/builder.rb', line 29

def attributes
  @attributes
end

#contentObject (readonly)

Returns the value of attribute content.



30
31
32
# File 'lib/utopia/content/builder.rb', line 30

def content
  @content
end

#deferredObject (readonly)

Returns the value of attribute deferred.



36
37
38
# File 'lib/utopia/content/builder.rb', line 36

def deferred
  @deferred
end

#nodeObject (readonly)

Returns the value of attribute node.



31
32
33
# File 'lib/utopia/content/builder.rb', line 31

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



27
28
29
# File 'lib/utopia/content/builder.rb', line 27

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



28
29
30
# File 'lib/utopia/content/builder.rb', line 28

def tag
  @tag
end

#tagsObject (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 tags
  @tags
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.

Returns:

  • (Boolean)


82
83
84
# File 'lib/utopia/content/builder.rb', line 82

def empty?
  @tags.empty?
end

#tag_begin(tag) ⇒ Object



86
87
88
89
# File 'lib/utopia/content/builder.rb', line 86

def tag_begin(tag)
  @tags << 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

Raises:



91
92
93
94
# File 'lib/utopia/content/builder.rb', line 91

def tag_end(tag)
  raise UnbalancedTagError.new(tag) unless @tags.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