Class: CiteProc::Number

Inherits:
Variable show all
Defined in:
lib/citeproc/number.rb

Overview

A CiteProc Variable used for numeric values.

Constant Summary collapse

MAX_ROMAN =
5000
FACTORS =
[
  ['m', 1000], ['cm', 900], ['d', 500], ['cd', 400],
  ['c',  100], ['xc',  90], ['l',  50], ['xl',  40],
  ['x',   10], ['ix',   9], ['v',   5], ['iv',   4],
  ['i',    1]
].freeze

Instance Attribute Summary

Attributes inherited from Variable

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Variable

create, create!, #date?, #initialize, #initialize_copy, #inspect, #numeric?, #plural?, #replace, #romanize, #strip_markup, #strip_markup!, #to_f, #to_i, #to_json, #to_s, #tokenize, #type

Constructor Details

This class inherits a constructor from CiteProc::Variable

Class Method Details

.pluralize?(string) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/citeproc/number.rb', line 17

def pluralize?(string)
  /\S[\s,&–-]\S|\df/ === string
end

.romanize(number) ⇒ String

Returns roman equivalent of the passed-in number.

Parameters:

  • number (#to_i)

    the number to convert

Returns:

  • (String)

    roman equivalent of the passed-in number



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/citeproc/number.rb', line 23

def romanize(number)
  number, roman = number.to_i, ''

  return number unless number > 0 || number < MAX_ROMAN

  FACTORS.each do |code, factor|
    count, number = number.divmod(factor)
    roman << (code * count)
  end

  roman
end

Instance Method Details

#<=>(other) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/citeproc/number.rb', line 37

def <=>(other)
  case
  when other.nil?
    1
  when numeric?
    if other.respond_to?(:to_i)
      to_i <=> other.to_i
    else
      nil
    end
  when other.is_a?(Variable) || other.is_a?(String)
    to_s <=> other.to_s
  else
    nil
  end
end