Class: ReVIEW::SecCounter
Overview
Secion Counter class
Instance Method Summary collapse
- #anchor(level) ⇒ Object
- #inc(level) ⇒ Object
-
#initialize(n, chapter) ⇒ SecCounter
constructor
A new instance of SecCounter.
- #prefix(level, secnolevel) ⇒ Object
- #reset(n) ⇒ Object
Constructor Details
#initialize(n, chapter) ⇒ SecCounter
Returns a new instance of SecCounter.
16 17 18 19 |
# File 'lib/review/sec_counter.rb', line 16 def initialize(n, chapter) @chapter = chapter reset(n) end |
Instance Method Details
#anchor(level) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/review/sec_counter.rb', line 37 def anchor(level) str = "#{@chapter.format_number(false)}" 0.upto(level-2) do |i| str << "-#{@counter[i]}" end str end |
#inc(level) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/review/sec_counter.rb', line 25 def inc(level) n = level - 2 if n >= 0 @counter[n] += 1 end if @counter.size > n (n+1 .. @counter.size).each do |i| @counter[i] = 0 end end end |
#prefix(level, secnolevel) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/review/sec_counter.rb', line 45 def prefix(level, secnolevel) return nil if @chapter.number.blank? if level == 1 if secnolevel >= 1 if @chapter.is_a? ReVIEW::Book::Part num = @chapter.number return "#{I18n.t('part', num)}#{I18n.t("chapter_postfix")}" else return "#{@chapter.format_number}#{I18n.t("chapter_postfix")}" end end else if secnolevel >= level prefix = @chapter.format_number(false) 0.upto(level - 2) do |i| prefix << ".#{@counter[i]}" end prefix << I18n.t("chapter_postfix") return prefix end end end |
#reset(n) ⇒ Object
21 22 23 |
# File 'lib/review/sec_counter.rb', line 21 def reset(n) @counter = Array.new(n, 0) end |