Class: Numerals::Format::Symbols

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

Overview

  • insignificant_digit : symbol to represent insignificant digits; use nil (the default) to omit insignificant digits and 0 for a zero digit. Insignificant digits are digits which, in an approximate value, are not determined: they could change to any other digit and the approximated value would be the same.

  • repeating : (boolean) support repeating decimals?

Defined Under Namespace

Classes: Digits, Padding

Constant Summary collapse

DEFAULTS =
{
  nan: 'NaN',
  infinity: 'Infinity',
  plus: '+',
  minus: '-',
  exponent: 'e',
  point: '.',
  group_separator: ',',
  zero: nil,
  repeat_begin: '<',
  repeat_end: '>',
  repeat_suffix: '...',
  show_plus: false,
  show_exponent_plus: false,
  uppercase: false,
  lowercase: false,
  show_zero: true,
  show_point: false,
  repeat_delimited: false,
  repeat_count: 3,
  grouping: [],
  insignificant_digit: nil,
  repeating: true,
  base_prefix: nil,
  base_suffix: nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numerals::FormattingAspect

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

Constructor Details

#initialize(*args) ⇒ Symbols

Returns a new instance of Symbols.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/numerals/format/symbols.rb', line 41

def initialize(*args)
  DEFAULTS.each do |param, value|
    instance_variable_set "@#{param}", value
  end

  # @digits is a mutable Object, so we don't want
  # to set it from DEFAULTS (which would share the
  # default Digits among all Symbols)
  @digits = Format::Symbols::Digits[]

  # same with @padding
  @padding = Format::Symbols::Padding[]

  set! *args
end

Instance Attribute Details

#base_prefixObject

Returns the value of attribute base_prefix.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def base_prefix
  @base_prefix
end

#base_suffixObject

Returns the value of attribute base_suffix.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def base_suffix
  @base_suffix
end

#digitsObject

Returns the value of attribute digits.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def digits
  @digits
end

#exponentObject

Returns the value of attribute exponent.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def exponent
  @exponent
end

#group_separatorObject

Returns the value of attribute group_separator.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def group_separator
  @group_separator
end

#groupingObject

Returns the value of attribute grouping.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def grouping
  @grouping
end

#infinityObject

Returns the value of attribute infinity.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def infinity
  @infinity
end

#insignificant_digitObject

Returns the value of attribute insignificant_digit.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def insignificant_digit
  @insignificant_digit
end

#lowercaseObject

Returns the value of attribute lowercase.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def lowercase
  @lowercase
end

#minusObject

Returns the value of attribute minus.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def minus
  @minus
end

#nanObject

Returns the value of attribute nan.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def nan
  @nan
end

#paddingObject (readonly)

Returns the value of attribute padding.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def padding
  @padding
end

#plusObject

Returns the value of attribute plus.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def plus
  @plus
end

#pointObject

Returns the value of attribute point.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def point
  @point
end

#repeat_beginObject

Returns the value of attribute repeat_begin.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def repeat_begin
  @repeat_begin
end

#repeat_countObject

Returns the value of attribute repeat_count.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def repeat_count
  @repeat_count
end

#repeat_delimitedObject

Returns the value of attribute repeat_delimited.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def repeat_delimited
  @repeat_delimited
end

#repeat_endObject

Returns the value of attribute repeat_end.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def repeat_end
  @repeat_end
end

#repeat_suffixObject

Returns the value of attribute repeat_suffix.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def repeat_suffix
  @repeat_suffix
end

#repeatingObject

Returns the value of attribute repeating.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def repeating
  @repeating
end

#show_exponent_plusObject

Returns the value of attribute show_exponent_plus.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def show_exponent_plus
  @show_exponent_plus
end

#show_plusObject

Returns the value of attribute show_plus.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def show_plus
  @show_plus
end

#show_pointObject

Returns the value of attribute show_point.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def show_point
  @show_point
end

#show_zeroObject

Returns the value of attribute show_zero.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def show_zero
  @show_zero
end

#uppercaseObject

Returns the value of attribute uppercase.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def uppercase
  @uppercase
end

#zeroObject

Returns the value of attribute zero.



57
58
59
# File 'lib/numerals/format/symbols.rb', line 57

def zero
  @zero
end

Instance Method Details

#case_sensitiveObject



179
180
181
# File 'lib/numerals/format/symbols.rb', line 179

def case_sensitive
  @digits.case_sensitive
end

#case_sensitive=(v) ⇒ Object



187
188
189
# File 'lib/numerals/format/symbols.rb', line 187

def case_sensitive=(v)
  @digits.set! case_sensitive: v
end

#case_sensitive?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/numerals/format/symbols.rb', line 183

def case_sensitive?
  @digits.case_sensitive
end

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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/numerals/format/symbols.rb', line 298

def digits_text(digit_values, options={})
  if options[:with_grouping]
    digit_values = group_digits(digit_values)
  end
  insignificant_symbol = @insignificant_digit
  insignificant_symbol = zero if insignificant_symbol == 0
  @digits.digits_text(
    digit_values,
    options.merge(
      separator: @group_separator,
      insignificant_symbol: insignificant_symbol
    )
  )
end

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



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/numerals/format/symbols.rb', line 363

def digits_values(digits_text, options = {})
  digit_pattern = Regexp.new(
    regexp(
      :grouped_digits,
      options.merge(no_capture: true)
    ),
    !case_sensitive? ? Regexp::IGNORECASE : 0
  )
  digits_text.scan(digit_pattern).map { |digit|
    case digit
    when /\A#{regexp(:insignificant_digit, case_sensitivity: true)}\Z/
      0
    when /\A#{regexp(:group_separator, case_sensitivity: true)}\Z/
      nil
    else
      @digits.digit_value(digit)
    end
  }.compact
end

#dupObject



275
276
277
# File 'lib/numerals/format/symbols.rb', line 275

def dup
  Format::Symbols[parameters]
end

#fillObject



105
106
107
108
109
110
111
112
# File 'lib/numerals/format/symbols.rb', line 105

def fill
  fill = @padding.fill
  if fill.is_a?(Integer)
    @digits.digit_symbol(fill)
  else
    fill
  end
end

#group_digits(digits) ⇒ Object

Group digits (inserting nil values as separators)



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/numerals/format/symbols.rb', line 280

def group_digits(digits)
  if grouping?
    grouped = []
    i = 0
    while digits.size > 0
      l = @grouping[i]
      l = digits.size if l > digits.size
      grouped = [nil] + grouped if grouped.size > 0
      grouped = digits[-l, l] + grouped
      digits = digits[0, digits.length - l]
      i += 1 if i < @grouping.size - 1
    end
    grouped
  else
    digits
  end
end

#grouping?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/numerals/format/symbols.rb', line 97

def grouping?
  !@grouping.empty? && @group_separator && !@group_separator.empty?
end

#inspectObject



271
272
273
# File 'lib/numerals/format/symbols.rb', line 271

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

#negative_infinityObject



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

def negative_infinity
  txt = ""
  txt << @minus
  txt << @infinity
  txt
end

#padded?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/numerals/format/symbols.rb', line 101

def padded?
  @padding.padded?
end

#paddings(number_size) ⇒ Object

Returns left, internal and right padding for a number of given size (number of characters)



385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/numerals/format/symbols.rb', line 385

def paddings(number_size)
  left_padding = internal_padding = right_padding = ''
  if padded?
    left_padding_size, internal_padding_size, right_padding_size = padding.padding_sizes(number_size)
    right_padding_size = right_padding_size/fill.size
    right_padding = fill*right_padding_size
    d = right_padding_size - right_padding.size
    left_padding_size = (left_padding_size + d)/fill.size
    left_padding = fill*left_padding_size
    internal_padding_size = internal_padding_size/fill.size
    internal_padding = fill*internal_padding_size
  end
  [left_padding, internal_padding, right_padding ]
end

#parameters(abbreviated = false) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/numerals/format/symbols.rb', line 250

def parameters(abbreviated=false)
  params = {}
  DEFAULTS.each do |param, default|
    value = instance_variable_get("@#{param}")
    if !abbreviated || value != default
      params[param] = value
    end
  end
  if !abbreviated || @digits != Format::Symbols::Digits[]
    params[:digits] = @digits
  end
  if !abbreviated || @padding != Format::Symbols::Padding[]
    params[:padding] = @padding
  end
  params
end

#positive_infinityObject



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

def positive_infinity
  txt = ""
  txt << @plus if @show_plus
  txt << @infinity
  txt
end

#regexp(*args) ⇒ Object

Generate a regular expression to match any of the passed symbols.

symbols.regexp(:nan, :plus, :minus) #=> "(NaN|+|-)"

The special symbol :digits can also be passed to generate all the digits, in which case the :base option can be used to generate digits only for some base smaller than the maximum defined for digits.

symbols.regexp(:digits, :point, base: 10) # => "(0|1|...|9)"

The option :no_capture can be used to avoid generating a capturing group; otherwise the result is captured group (surrounded by parenthesis)

symbols.regexp(:digits, no_capture: true) # => "(?:...)"

The :case_sensitivity option is used to generate a regular expression that matches the case of the text as defined by ghe case_sensitive attribute of the Symbols. If this option is used the result should not be used in a case-insensitive regular expression (/…/i).

/#{symbols.regexp(:digits, case_sensitivity: true)}/

If the options is not used, than the regular expression should be be made case-insensitive according to the Symbols:

if symbols.case_sensitive?
  /#{symbols.regexp(:digits)}/

else

/#{symbols.regexp(:digits)}/i


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/numerals/format/symbols.rb', line 343

def regexp(*args)
  options = args.pop if args.last.is_a?(Hash)
  options ||= {}
  symbols = args
  digits = symbols.delete(:digits)
  grouped_digits = symbols.delete(:grouped_digits)
  symbols = symbols.map { |s|
    s.is_a?(Symbol) ? send(s)  : s
  }
  if grouped_digits
    symbols += [group_separator, insignificant_digit]
  elsif digits
    symbols += [insignificant_digit]
  end
  if digits || grouped_digits
    symbols += @digits.digits(options)
  end
  regexp_group(symbols, options)
end

#to_sObject



267
268
269
# File 'lib/numerals/format/symbols.rb', line 267

def to_s
  "Digits[#{parameters(true).inspect.unwrap('{}')}]"
end