Class: Numerals::Format::Symbols
- Inherits:
-
Numerals::FormattingAspect
- Object
- Numerals::FormattingAspect
- Numerals::Format::Symbols
- 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
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
-
#base_prefix ⇒ Object
Returns the value of attribute base_prefix.
-
#base_suffix ⇒ Object
Returns the value of attribute base_suffix.
-
#digits ⇒ Object
Returns the value of attribute digits.
-
#exponent ⇒ Object
Returns the value of attribute exponent.
-
#group_separator ⇒ Object
Returns the value of attribute group_separator.
-
#grouping ⇒ Object
Returns the value of attribute grouping.
-
#infinity ⇒ Object
Returns the value of attribute infinity.
-
#insignificant_digit ⇒ Object
Returns the value of attribute insignificant_digit.
-
#lowercase ⇒ Object
Returns the value of attribute lowercase.
-
#minus ⇒ Object
Returns the value of attribute minus.
-
#nan ⇒ Object
Returns the value of attribute nan.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#plus ⇒ Object
Returns the value of attribute plus.
-
#point ⇒ Object
Returns the value of attribute point.
-
#repeat_begin ⇒ Object
Returns the value of attribute repeat_begin.
-
#repeat_count ⇒ Object
Returns the value of attribute repeat_count.
-
#repeat_delimited ⇒ Object
Returns the value of attribute repeat_delimited.
-
#repeat_end ⇒ Object
Returns the value of attribute repeat_end.
-
#repeat_suffix ⇒ Object
Returns the value of attribute repeat_suffix.
-
#repeating ⇒ Object
Returns the value of attribute repeating.
-
#show_exponent_plus ⇒ Object
Returns the value of attribute show_exponent_plus.
-
#show_plus ⇒ Object
Returns the value of attribute show_plus.
-
#show_point ⇒ Object
Returns the value of attribute show_point.
-
#show_zero ⇒ Object
Returns the value of attribute show_zero.
-
#uppercase ⇒ Object
Returns the value of attribute uppercase.
-
#zero ⇒ Object
Returns the value of attribute zero.
Instance Method Summary collapse
- #case_sensitive ⇒ Object
- #case_sensitive=(v) ⇒ Object
- #case_sensitive? ⇒ Boolean
- #digits_text(digit_values, options = {}) ⇒ Object
- #digits_values(digits_text, options = {}) ⇒ Object
- #dup ⇒ Object
- #fill ⇒ Object
-
#group_digits(digits) ⇒ Object
Group digits (inserting nil values as separators).
- #grouping? ⇒ Boolean
-
#initialize(*args) ⇒ Symbols
constructor
A new instance of Symbols.
- #inspect ⇒ Object
- #negative_infinity ⇒ Object
- #padded? ⇒ Boolean
-
#paddings(number_size) ⇒ Object
Returns left, internal and right padding for a number of given size (number of characters).
- #parameters(abbreviated = false) ⇒ Object
- #positive_infinity ⇒ Object
-
#regexp(*args) ⇒ Object
Generate a regular expression to match any of the passed symbols.
- #to_s ⇒ Object
Methods inherited from Numerals::FormattingAspect
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_prefix ⇒ Object
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_suffix ⇒ Object
Returns the value of attribute base_suffix.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def base_suffix @base_suffix end |
#digits ⇒ Object
Returns the value of attribute digits.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def digits @digits end |
#exponent ⇒ Object
Returns the value of attribute exponent.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def exponent @exponent end |
#group_separator ⇒ Object
Returns the value of attribute group_separator.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def group_separator @group_separator end |
#grouping ⇒ Object
Returns the value of attribute grouping.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def grouping @grouping end |
#infinity ⇒ Object
Returns the value of attribute infinity.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def infinity @infinity end |
#insignificant_digit ⇒ Object
Returns the value of attribute insignificant_digit.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def insignificant_digit @insignificant_digit end |
#lowercase ⇒ Object
Returns the value of attribute lowercase.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def lowercase @lowercase end |
#minus ⇒ Object
Returns the value of attribute minus.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def minus @minus end |
#nan ⇒ Object
Returns the value of attribute nan.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def nan @nan end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def padding @padding end |
#plus ⇒ Object
Returns the value of attribute plus.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def plus @plus end |
#point ⇒ Object
Returns the value of attribute point.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def point @point end |
#repeat_begin ⇒ Object
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_count ⇒ Object
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_delimited ⇒ Object
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_end ⇒ Object
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_suffix ⇒ Object
Returns the value of attribute repeat_suffix.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def repeat_suffix @repeat_suffix end |
#repeating ⇒ Object
Returns the value of attribute repeating.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def repeating @repeating end |
#show_exponent_plus ⇒ Object
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_plus ⇒ Object
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_point ⇒ Object
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_zero ⇒ Object
Returns the value of attribute show_zero.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def show_zero @show_zero end |
#uppercase ⇒ Object
Returns the value of attribute uppercase.
57 58 59 |
# File 'lib/numerals/format/symbols.rb', line 57 def uppercase @uppercase end |
#zero ⇒ Object
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_sensitive ⇒ Object
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
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, ={}) if [: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, .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, = {}) digit_pattern = Regexp.new( regexp( :grouped_digits, .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 |
#dup ⇒ Object
275 276 277 |
# File 'lib/numerals/format/symbols.rb', line 275 def dup Format::Symbols[parameters] end |
#fill ⇒ Object
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
97 98 99 |
# File 'lib/numerals/format/symbols.rb', line 97 def grouping? !@grouping.empty? && @group_separator && !@group_separator.empty? end |
#inspect ⇒ Object
271 272 273 |
# File 'lib/numerals/format/symbols.rb', line 271 def inspect "Format::Symbols::#{self}" end |
#negative_infinity ⇒ Object
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
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_infinity ⇒ Object
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) = args.pop if args.last.is_a?(Hash) ||= {} 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() end regexp_group(symbols, ) end |
#to_s ⇒ Object
267 268 269 |
# File 'lib/numerals/format/symbols.rb', line 267 def to_s "Digits[#{parameters(true).inspect.unwrap('{}')}]" end |