Class: Arrolio::LayoutSpec::PageTemplateSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/layout_spec/page_template_selector.rb

Overview

Selects the appropriate page template based on page parity (odd/even) and the configured template set. Supports three modes:

- Single template (current behavior): always returns the
same template for every page.
- Odd/even templates: returns different templates for odd
vs even pages, typically mirroring the inner/outer margins
for double-sided printing.
- Custom mapping: caller provides a proc or hash mapping
page numbers to template names.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_template:, odd_template: nil, even_template: nil) ⇒ PageTemplateSelector

default_template: template name used when no odd/even templates are configured. odd_template: template name for odd pages (1, 3, 5, ...). even_template: template name for even pages (2, 4, 6, ...).



22
23
24
25
26
27
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 22

def initialize(default_template:, odd_template: nil, even_template: nil)
  @default_template = default_template.to_sym
  @odd_template = odd_template&.to_sym
  @even_template = even_template&.to_sym
  freeze
end

Instance Attribute Details

#default_templateObject (readonly)

Returns the value of attribute default_template.



16
17
18
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 16

def default_template
  @default_template
end

#even_templateObject (readonly)

Returns the value of attribute even_template.



16
17
18
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 16

def even_template
  @even_template
end

#odd_templateObject (readonly)

Returns the value of attribute odd_template.



16
17
18
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 16

def odd_template
  @odd_template
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



45
46
47
48
49
50
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 45

def ==(other)
  other.is_a?(self.class) &&
    default_template == other.default_template &&
    odd_template == other.odd_template &&
    even_template == other.even_template
end

#alternating?Boolean

Does this selector use different templates for odd/even?

Returns:

  • (Boolean)


41
42
43
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 41

def alternating?
  !(@odd_template.nil? && @even_template.nil?)
end

#hashObject



53
54
55
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 53

def hash
  [self.class, default_template, odd_template, even_template].hash
end

#name_for(page_number) ⇒ Object

Returns the template name for the given page number.



30
31
32
33
34
35
36
37
38
# File 'lib/arrolio/layout_spec/page_template_selector.rb', line 30

def name_for(page_number)
  return @default_template unless @odd_template || @even_template

  if page_number.odd?
  (@odd_template || @default_template)
else
  (@even_template || @default_template)
end
end