module Prawn::Extras::DynamicRepeater
Exemplo de utilização: Deseja-se agrupar alunos por curso, e cada vez que o curso muda há uma quebra de página, de forma que o nome do curso deve ser impresso no cabeçalho do relatório, sempre o curso do grupo atual de alunos.
Nesse caso, no cabeçalho terá algo do tipo: ‘text(valor_na_pagina(:nome_curso, page_number))’
E, dentro do ‘.each’ que itera sobre os cursos, algo do tipo: cursos.each do |curso|
start_new_line set_valor_na_pagina(:nome_curso, curso, page_number) /* Tabela de alunos */
end
¶ ↑
Public Instance Methods
Saves a named value for a specific page of the generated PDF. This will save the value for the specified page and also fill any page gaps with the latest previous value submitted.
Example:
store_value_in_page
(:name, ‘John’, 3)
This will save the value “John” at the :name key for the pages 1, 2 and 3. Any subsequent calls to this method (for the same key) will not override these values.
# File lib/prawn/extras/dynamic_repeater.rb, line 44 def store_value_in_page(key, value, page = page_number) latest_value = value_in_page(key, page) # Gets the last value submitted (page - 1).downto(max_index(key)).each do |page_index| repeater_values(key)[page_index] = latest_value end repeater_values(key)[page] = value end
Returns the value for a key at a specific page. If the page is greater than the highest saved page, the highest value is returned.
Examples:
save_repeater_value(:name, ‘John’, 3) save_repeater_value(:name, ‘Jane’, 5)
value_in_page
(:name, 1) => “John” value_in_page
(:name, 2) => “John” value_in_page
(:name, 3) => “John” value_in_page
(:name, 4) => “Jane” value_in_page
(:name, 5) => “Jane” value_in_page
(:name, 6) => “Jane” value_in_page
(:name, -1) => “”
# File lib/prawn/extras/dynamic_repeater.rb, line 68 def value_in_page(key, page, default_value = '') repeater_values(key)[[page, max_index(key)].min] || default_value end
Private Instance Methods
# File lib/prawn/extras/dynamic_repeater.rb, line 74 def max_index(key) repeater_values(key).keys.max.to_i end
# File lib/prawn/extras/dynamic_repeater.rb, line 78 def repeater_values(key) @repeater_values ||= {} @repeater_values[key] ||= {} @repeater_values[key] end