Class: Numerals::Format::Symbols::Digits

Inherits:
Numerals::FormattingAspect show all
Includes:
ModalSupport::StateEquivalent
Defined in:
lib/numerals/format/symbols/digits.rb

Constant Summary collapse

DEFAULT_DIGITS =
%w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numerals::FormattingAspect

#[], [], aspect, #set, set

Constructor Details

#initialize(*args) ⇒ Digits

Returns a new instance of Digits.



7
8
9
10
11
12
13
14
15
# File 'lib/numerals/format/symbols/digits.rb', line 7

def initialize(*args)
  @digits = DEFAULT_DIGITS
  @downcase_digits = @digits.map(&:downcase)
  @max_base = @digits.size
  @case_sensitive = false
  @uppercase = false
  @lowercase = false
  set! *args
end

Instance Attribute Details

#case_sensitiveObject

Returns the value of attribute case_sensitive.



26
27
28
# File 'lib/numerals/format/symbols/digits.rb', line 26

def case_sensitive
  @case_sensitive
end

#digits_stringObject (readonly)

Returns the value of attribute digits_string.



26
27
28
# File 'lib/numerals/format/symbols/digits.rb', line 26

def digits_string
  @digits_string
end

#lowercaseObject

Returns the value of attribute lowercase.



26
27
28
# File 'lib/numerals/format/symbols/digits.rb', line 26

def lowercase
  @lowercase
end

#max_baseObject (readonly)

Returns the value of attribute max_base.



26
27
28
# File 'lib/numerals/format/symbols/digits.rb', line 26

def max_base
  @max_base
end

#uppercaseObject

Returns the value of attribute uppercase.



26
27
28
# File 'lib/numerals/format/symbols/digits.rb', line 26

def uppercase
  @uppercase
end

Instance Method Details

#case_sensitive?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/numerals/format/symbols/digits.rb', line 63

def case_sensitive?
  case_sensitive
end

#digit_symbol(v, options = {}) ⇒ Object



82
83
84
85
86
# File 'lib/numerals/format/symbols/digits.rb', line 82

def digit_symbol(v, options={})
  base = options[:base] || @max_base
  raise "Invalid base" if base > @max_base
  v >= 0 && v < base ? @digits[v] : nil
end

#digit_value(digit) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/numerals/format/symbols/digits.rb', line 74

def digit_value(digit)
  if @case_sensitive
    @digits.index(digit)
  else
    @downcase_digits.index(digit.downcase)
  end
end

#digits(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/numerals/format/symbols/digits.rb', line 29

def digits(options = {})
  base = options[:base] || @max_base
  if base >= @max_base
    @digits
  else
    @digits[0, base]
  end
end

#digits=(digits) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/numerals/format/symbols/digits.rb', line 38

def digits=(digits)
  if digits.is_a?(String)
    @digits = digits.each_char.to_a
  else
    @digits = digits
  end
  @max_base = @digits.size
  @lowercase = @digits.all? { |d| d.downcase == d }
  @uppercase = @digits.all? { |d| d.upcase == d }
  @downcase_digits = @digits.map(&:downcase)
  if @digits.uniq.size != @max_base
    raise "Inconsistent digits"
  end
end

#digits_text(digit_values, options = {}) ⇒ Object

Convert sequence of digits to its text representation. The nil value can be used in the digits sequence to represent the group separator.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/numerals/format/symbols/digits.rb', line 91

def digits_text(digit_values, options={})
  insignificant_digits = options[:insignificant_digits] || 0
  num_digits = digit_values.reduce(0) { |num, digit|
    digit.nil? ? num : num + 1
  }
  num_digits -= insignificant_digits
  digit_values.map { |d|
    if d.nil?
      options[:separator]
    else
      num_digits -= 1
      if num_digits >= 0
        digit_symbol(d, options)
      else
        options[:insignificant_symbol]
      end
    end
  }.join
end

#dupObject



145
146
147
# File 'lib/numerals/format/symbols/digits.rb', line 145

def dup
  Format::Symbols::Digits[parameters]
end

#inspectObject



141
142
143
# File 'lib/numerals/format/symbols/digits.rb', line 141

def inspect
  "Format::Symbols::#{self}"
end

#is_digit?(digit_symbol, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/numerals/format/symbols/digits.rb', line 67

def is_digit?(digit_symbol, options={})
  base = options[:base] || @max_base
  raise "Invalid base" if base > @max_base
  v = digit_value(digit_symbol)
  v && v < base
end

#parametersObject



111
112
113
114
115
116
117
118
# File 'lib/numerals/format/symbols/digits.rb', line 111

def parameters
  params = {}
  params[:digits] = @digits
  params[:case_sensitive] = @case_sensitive
  params[:uppercase] = @uppercase
  params[:lowercase] = @lowercase
  params
end

#to_sObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/numerals/format/symbols/digits.rb', line 120

def to_s
  # TODO: show only non-defaults
  args = []
  if @digits != DEFAULT_DIGITS
    args << @digits.to_s
  end
  if @max_base != @digits.size
    args << "max_base: #{@max_base}"
  end
  if @case_sensitive
    args << "case_sensitive: #{case_sensitive.inspect}"
  end
  if @uppercase
    args << "uppercase: #{uppercase.inspect}"
  end
  if @lowercase
    args << "lowercase: #{lowercase.inspect}"
  end
  "Digits[#{args.join(', ')}]"
end