Class: Uniword::Caption::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/caption/counter.rb

Overview

Label-keyed counter for figure/table/equation captions. Persisted on the document via DocumentRoot#caption_counters; each label starts at 1 and increments on every next_value.

Open/closed: any label string works — adding a new label category is just using it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



15
16
17
# File 'lib/uniword/caption/counter.rb', line 15

def initialize
  @counts = Hash.new { |h, k| h[k] = 0 }
end

Instance Attribute Details

#countsHash{String => Integer} (readonly)

Returns:

  • (Hash{String => Integer})


13
14
15
# File 'lib/uniword/caption/counter.rb', line 13

def counts
  @counts
end

Instance Method Details

#current(label) ⇒ Integer

Current count without incrementing.

Parameters:

  • label (String)

Returns:

  • (Integer)


30
31
32
# File 'lib/uniword/caption/counter.rb', line 30

def current(label)
  @counts[normalize_label(label)]
end

#labelsArray<String>

All labels currently tracked.

Returns:

  • (Array<String>)


50
51
52
# File 'lib/uniword/caption/counter.rb', line 50

def labels
  @counts.keys
end

#next_value(label) ⇒ Integer

Returns the next sequence value for this label.

Parameters:

  • label (String)

    e.g. "Figure", "Table", "Equation"

Returns:

  • (Integer)

    the next sequence value for this label



21
22
23
24
# File 'lib/uniword/caption/counter.rb', line 21

def next_value(label)
  label = normalize_label(label)
  @counts[label] += 1
end

#reset(label = nil) ⇒ void

This method returns an undefined value.

Reset one or all counters.

Parameters:

  • label (String, nil) (defaults to: nil)

    nil resets all



38
39
40
41
42
43
44
45
# File 'lib/uniword/caption/counter.rb', line 38

def reset(label = nil)
  if label.nil?
    @counts.clear
    return
  end

  @counts.delete(normalize_label(label))
end