Class: Uniword::Caption::Counter
- Inherits:
-
Object
- Object
- Uniword::Caption::Counter
- 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
- #counts ⇒ Hash{String => Integer} readonly
Instance Method Summary collapse
-
#current(label) ⇒ Integer
Current count without incrementing.
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
-
#labels ⇒ Array<String>
All labels currently tracked.
-
#next_value(label) ⇒ Integer
The next sequence value for this label.
-
#reset(label = nil) ⇒ void
Reset one or all counters.
Constructor Details
#initialize ⇒ Counter
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
#counts ⇒ Hash{String => Integer} (readonly)
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.
30 31 32 |
# File 'lib/uniword/caption/counter.rb', line 30 def current(label) @counts[normalize_label(label)] end |
#labels ⇒ Array<String>
All labels currently tracked.
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.
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.
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 |