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 for the configuration option layout.boxes.map.

Example:

document.layout.box(:list) do |list|
  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.



127
128
129
130
131
# File 'lib/hexapdf/document/layout.rb', line 127

def initialize(layout)
  @layout = layout
  @layout_boxes_map = layout.instance_variable_get(:@document).config['layout.boxes.map']
  @children = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hexapdf/document/layout.rb', line 134

def method_missing(name, *args, **kwargs, &block)
  if @layout.respond_to?(name)
    @children << @layout.send(name, *args, **kwargs, &block)
  elsif @layout.respond_to?("#{name}_box")
    @children << @layout.send("#{name}_box", *args, **kwargs, &block)
  elsif @layout_boxes_map.key?(name)
    @children << @layout.box(name, *args, **kwargs, &block)
  else
    super
  end
end

Instance Attribute Details

#childrenObject (readonly)

The collected children



123
124
125
# File 'lib/hexapdf/document/layout.rb', line 123

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)


116
117
118
119
120
# File 'lib/hexapdf/document/layout.rb', line 116

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.



155
156
157
# File 'lib/hexapdf/document/layout.rb', line 155

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.



161
162
163
# File 'lib/hexapdf/document/layout.rb', line 161

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

#respond_to_missing?(name, _private) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


147
148
149
150
151
152
# File 'lib/hexapdf/document/layout.rb', line 147

def respond_to_missing?(name, _private)
  @layout.respond_to?(name) ||
    @layout.respond_to?("#{name}_box") ||
    @layout_boxes_map.key?(name) ||
    super
end