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

Constant Summary collapse

DEFAULTS =
{
  nan: 'NaN',
  infinity: 'Infinity',
  plus: '+',
  minus: '-',
  exponent: 'e',
  point: '.',
  group_separator: ',',
  zero: nil,
  repeat_begin: '<',
  repeat_end: '>',
  repeat_suffix: '...',
  #repeat_detect: false,
  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
}

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.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/numerals/format/symbols.rb', line 192

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[]

  # TODO: justification/padding
  # width, adjust_mode (left, right, internal), fill_symbol

  # TODO: base_suffixes, base_preffixes, show_base

  set! *args
end

Instance Attribute Details

#digitsObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def digits
  @digits
end

#exponentObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def exponent
  @exponent
end

#group_separatorObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def group_separator
  @group_separator
end

#groupingObject

Returns the value of attribute grouping.



217
218
219
# File 'lib/numerals/format/symbols.rb', line 217

def grouping
  @grouping
end

#infinityObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def infinity
  @infinity
end

#insignificant_digitObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def insignificant_digit
  @insignificant_digit
end

#lowercaseObject

Returns the value of attribute lowercase.



215
216
217
# File 'lib/numerals/format/symbols.rb', line 215

def lowercase
  @lowercase
end

#minusObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def minus
  @minus
end

#nanObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def nan
  @nan
end

#plusObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def plus
  @plus
end

#pointObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def point
  @point
end

#repeat_beginObject

Returns the value of attribute repeat_begin.



214
215
216
# File 'lib/numerals/format/symbols.rb', line 214

def repeat_begin
  @repeat_begin
end

#repeat_countObject

Returns the value of attribute repeat_count.



217
218
219
# File 'lib/numerals/format/symbols.rb', line 217

def repeat_count
  @repeat_count
end

#repeat_delimitedObject

Returns the value of attribute repeat_delimited.



214
215
216
# File 'lib/numerals/format/symbols.rb', line 214

def repeat_delimited
  @repeat_delimited
end

#repeat_endObject

Returns the value of attribute repeat_end.



214
215
216
# File 'lib/numerals/format/symbols.rb', line 214

def repeat_end
  @repeat_end
end

#repeat_suffixObject

Returns the value of attribute repeat_suffix.



214
215
216
# File 'lib/numerals/format/symbols.rb', line 214

def repeat_suffix
  @repeat_suffix
end

#repeatingObject

Returns the value of attribute repeating.



217
218
219
# File 'lib/numerals/format/symbols.rb', line 217

def repeating
  @repeating
end

#show_exponent_plusObject

Returns the value of attribute show_exponent_plus.



215
216
217
# File 'lib/numerals/format/symbols.rb', line 215

def show_exponent_plus
  @show_exponent_plus
end

#show_plusObject

Returns the value of attribute show_plus.



215
216
217
# File 'lib/numerals/format/symbols.rb', line 215

def show_plus
  @show_plus
end

#show_pointObject

Returns the value of attribute show_point.



215
216
217
# File 'lib/numerals/format/symbols.rb', line 215

def show_point
  @show_point
end

#show_zeroObject

Returns the value of attribute show_zero.



215
216
217
# File 'lib/numerals/format/symbols.rb', line 215

def show_zero
  @show_zero
end

#uppercaseObject

Returns the value of attribute uppercase.



215
216
217
# File 'lib/numerals/format/symbols.rb', line 215

def uppercase
  @uppercase
end

#zeroObject

TODO: transmit uppercase/lowercase to digits



212
213
214
# File 'lib/numerals/format/symbols.rb', line 212

def zero
  @zero
end

Instance Method Details

#case_sensitiveObject



291
292
293
# File 'lib/numerals/format/symbols.rb', line 291

def case_sensitive
  @digits.case_sensitive
end

#case_sensitive=(v) ⇒ Object



299
300
301
# File 'lib/numerals/format/symbols.rb', line 299

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

#case_sensitive?Boolean

Returns:

  • (Boolean)


295
296
297
# File 'lib/numerals/format/symbols.rb', line 295

def case_sensitive?
  @digits.case_sensitive
end

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



399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/numerals/format/symbols.rb', line 399

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



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/numerals/format/symbols.rb', line 462

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



376
377
378
# File 'lib/numerals/format/symbols.rb', line 376

def dup
  Format::Symbols[parameters]
end

#group_digits(digits) ⇒ Object

Group digits (inserting nil values as separators)



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

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)


250
251
252
# File 'lib/numerals/format/symbols.rb', line 250

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

#inspectObject



372
373
374
# File 'lib/numerals/format/symbols.rb', line 372

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

#negative_infinityObject



235
236
237
238
239
240
# File 'lib/numerals/format/symbols.rb', line 235

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

#parameters(abbreviated = false) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/numerals/format/symbols.rb', line 354

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
  params
end

#positive_infinityObject



228
229
230
231
232
233
# File 'lib/numerals/format/symbols.rb', line 228

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


444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/numerals/format/symbols.rb', line 444

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| send(s.to_sym) }
  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



368
369
370
# File 'lib/numerals/format/symbols.rb', line 368

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