Class: Arrolio::Content::IndexEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/content/index_entry.rb

Overview

A single index entry — a term and the page(s) where it appears. Part of a back-of-book index section. The adapter extracts these from / elements; the FlowBuilder renders them alphabetically with page numbers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term:, page_numbers:, see_also: [], style_id: :index_entry) ⇒ IndexEntry

Returns a new instance of IndexEntry.



12
13
14
15
16
17
18
# File 'lib/arrolio/content/index_entry.rb', line 12

def initialize(term:, page_numbers:, see_also: [], style_id: :index_entry)
  @term = term.to_s
  @page_numbers = Array(page_numbers).map(&:to_i).sort.uniq.freeze
  @see_also = Array(see_also).map(&:to_s).freeze
  @style_id = style_id.to_sym
  freeze
end

Instance Attribute Details

#page_numbersObject (readonly)

Returns the value of attribute page_numbers.



10
11
12
# File 'lib/arrolio/content/index_entry.rb', line 10

def page_numbers
  @page_numbers
end

#see_alsoObject (readonly)

Returns the value of attribute see_also.



10
11
12
# File 'lib/arrolio/content/index_entry.rb', line 10

def see_also
  @see_also
end

#style_idObject (readonly)

Returns the value of attribute style_id.



10
11
12
# File 'lib/arrolio/content/index_entry.rb', line 10

def style_id
  @style_id
end

#termObject (readonly)

Returns the value of attribute term.



10
11
12
# File 'lib/arrolio/content/index_entry.rb', line 10

def term
  @term
end

Instance Method Details

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



39
40
41
42
43
44
# File 'lib/arrolio/content/index_entry.rb', line 39

def ==(other)
  other.is_a?(self.class) &&
    term == other.term &&
    page_numbers == other.page_numbers &&
    see_also == other.see_also
end

#add_page(number) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/arrolio/content/index_entry.rb', line 20

def add_page(number)
  return self if @page_numbers.include?(number.to_i)

  self.class.new(
    term: @term,
    page_numbers: @page_numbers + [number.to_i],
    see_also: @see_also,
    style_id: @style_id
  )
end

#first_letterObject



31
32
33
# File 'lib/arrolio/content/index_entry.rb', line 31

def first_letter
  @term[0]&.upcase || ''
end

#hashObject



47
48
49
# File 'lib/arrolio/content/index_entry.rb', line 47

def hash
  [self.class, term, page_numbers, see_also].hash
end

#page_numbers_strObject



35
36
37
# File 'lib/arrolio/content/index_entry.rb', line 35

def page_numbers_str
  @page_numbers.join(', ')
end