Class: Asciidoctor::PDF::RomanNumeral
- 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
-
#initialize(initial_value, letter_case) ⇒ RomanNumeral
constructor
A new instance of RomanNumeral.
- #next ⇒ Object
- #next! ⇒ Object
- #nil_or_empty? ⇒ Boolean
- #pred ⇒ Object
- #to_r ⇒ Object
- #to_s ⇒ Object
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
#next ⇒ Object
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
81 82 83 |
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 81 def nil_or_empty? false end |
#pred ⇒ Object
77 78 79 |
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 77 def pred RomanNumeral.new @integer_value - 1, @letter_case end |
#to_r ⇒ Object
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_s ⇒ Object
56 57 58 |
# File 'lib/asciidoctor/pdf/roman_numeral.rb', line 56 def to_s to_r end |