Class: HexaPDF::Document::Layout::ChildrenCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/document/layout.rb

Overview

This class is used when a box can contain child boxes and the creation of such boxes should be seemlessly doable when creating the parent node. It is yieled, for example, by Layout#box to collect the children for the created box.

A box can be added to the list of collected children in the following ways:

#<<

This appends the given box to the list.

text_box, formatted_text_box, image_box, …

Any method accepted by the Layout class.

text, formatted_text, image, …

Any method accepted by the Layout class without the _box suffix.

list, column, …

Any name registered with the configuration option layout.boxes.map.

The special method #multiple allows adding multiple boxes as a single array to the collected children.

Example:

document.layout.box(:list) do |list|  # list is a ChildrenCollector
  list.text_box("Some text here")     # layout method
  list.image(image_path)              # layout method without _box suffix
  list.column(columns: 3) do |column| # registered box name
    column.text("Text in column")
    column << document.layout.lorem_ipsum_box   # adding a Box instance
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout) ⇒ ChildrenCollector

Create a new ChildrenCollector for the given layout (a HexaPDF::Document::Layout) instance.



146
147
148
149
# File 'lib/hexapdf/document/layout.rb', line 146

def initialize(layout)
  @layout = layout
  @children = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object

:nodoc:



152
153
154
155
156
157
158
159
160
# File 'lib/hexapdf/document/layout.rb', line 152

def method_missing(name, *args, **kwargs, &block)
  if @layout.box_creation_method?(name)
    box = @layout.send(name, *args, **kwargs, &block)
    @children << box
    box
  else
    super
  end
end

Instance Attribute Details

#childrenObject (readonly)

The collected children



142
143
144
# File 'lib/hexapdf/document/layout.rb', line 142

def children
  @children
end

Class Method Details

.collect(layout) {|collector| ... } ⇒ Object

Creates a children collector, yields it and then returns the collected children.

Yields:

  • (collector)


135
136
137
138
139
# File 'lib/hexapdf/document/layout.rb', line 135

def self.collect(layout)
  collector = new(layout)
  yield(collector)
  collector.children
end

Instance Method Details

#<<(box) ⇒ Object

Appends the given box to the list of collected children.



168
169
170
# File 'lib/hexapdf/document/layout.rb', line 168

def <<(box)
  @children << box
end

#multiple(&block) ⇒ Object

Yields a ChildrenCollector instance and adds the collected children as a single array to the list of collected children.



174
175
176
# File 'lib/hexapdf/document/layout.rb', line 174

def multiple(&block)
  @children << self.class.collect(@layout, &block)
end

#respond_to_missing?(name, _private) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


163
164
165
# File 'lib/hexapdf/document/layout.rb', line 163

def respond_to_missing?(name, _private)
  @layout.box_creation_method?(name) || super
end