Class: Asciidoctor::PDF::RomanNumeral

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/pdf/roman_numeral.rb

Constant Summary collapse

BaseDigits =
{
  1 => 'I',
  4 => 'IV',
  5 => 'V',
  9 => 'IX',
  10 => 'X',
  40 => 'XL',
  50 => 'L',
  90 => 'XC',
  100 => 'C',
  400 => 'CD',
  500 => 'D',
  900 => 'CM',
  1000 => 'M',
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_value, letter_case) ⇒ RomanNumeral

Returns a new instance of RomanNumeral.



51
52
53
54
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 51

def initialize initial_value, letter_case
  @integer_value = ::Integer === initial_value ? initial_value : (RomanNumeral.roman_to_int initial_value)
  @letter_case = letter_case
end

Class Method Details

.int_to_roman(value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 85

def self.int_to_roman value
  result = []
  BaseDigits.keys.reverse_each do |ival|
    while value >= ival
      value -= ival
      result << BaseDigits[ival]
    end
  end
  result.join
end

.roman_to_int(value) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 96

def self.roman_to_int value
  value = value.upcase
  result = 0
  BaseDigits.values.reverse_each do |rval|
    while value.start_with? rval
      offset = rval.length
      value = value[offset..offset]
      result += BaseDigits.key rval
    end
  end
  result
end

Instance Method Details

#nextObject



68
69
70
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 68

def next
  RomanNumeral.new @integer_value + 1, @letter_case
end

#next!Object



72
73
74
75
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 72

def next!
  @integer_value += 1
  self
end

#nil_or_empty?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 81

def nil_or_empty?
  false
end

#predObject



77
78
79
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 77

def pred
  RomanNumeral.new @integer_value - 1, @letter_case
end

#to_rObject



60
61
62
63
64
65
66
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 60

def to_r
  if (int = @integer_value) < 1
    return int.to_s
  end
  roman = RomanNumeral.int_to_roman int
  @letter_case == :lower ? roman.downcase : roman
end

#to_sObject



56
57
58
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 56

def to_s
  to_r
end