class Numerals::Format::Symbols

Constants

DEFAULTS

Attributes

base_prefix[RW]
base_suffix[RW]
digits[RW]
exponent[RW]
group_separator[RW]
grouping[RW]
infinity[RW]
insignificant_digit[RW]
lowercase[RW]
minus[RW]
nan[RW]
padding[R]
plus[RW]
point[RW]
repeat_begin[RW]
repeat_count[RW]
repeat_delimited[RW]
repeat_end[RW]
repeat_suffix[RW]
repeating[RW]
show_exponent_plus[RW]
show_plus[RW]
show_point[RW]
show_zero[RW]
uppercase[RW]
zero[RW]

Public Class Methods

new(*args) click to toggle source
# 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

Public Instance Methods

case_sensitive() click to toggle source
# File lib/numerals/format/symbols.rb, line 179
def case_sensitive
  @digits.case_sensitive
end
case_sensitive=(v) click to toggle source
# File lib/numerals/format/symbols.rb, line 187
def case_sensitive=(v)
  @digits.set! case_sensitive: v
end
case_sensitive?() click to toggle source
# File lib/numerals/format/symbols.rb, line 183
def case_sensitive?
  @digits.case_sensitive
end
digits_text(digit_values, options={}) click to toggle source
# 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 = {}) click to toggle source
# 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
dup() click to toggle source
# File lib/numerals/format/symbols.rb, line 275
def dup
  Format::Symbols[parameters]
end
fill() click to toggle source
# 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) click to toggle source

Group digits (inserting nil values as separators)

# 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?() click to toggle source
# File lib/numerals/format/symbols.rb, line 97
def grouping?
  !@grouping.empty? && @group_separator && !@group_separator.empty?
end
inspect() click to toggle source
# File lib/numerals/format/symbols.rb, line 271
def inspect
  "Format::Symbols::#{self}"
end
negative_infinity() click to toggle source
# File lib/numerals/format/symbols.rb, line 82
def negative_infinity
  txt = ""
  txt << @minus
  txt << @infinity
  txt
end
padded?() click to toggle source
# File lib/numerals/format/symbols.rb, line 101
def padded?
  @padding.padded?
end
paddings(number_size) click to toggle source

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

# 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) click to toggle source
# 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() click to toggle source
# File lib/numerals/format/symbols.rb, line 75
def positive_infinity
  txt = ""
  txt << @plus if @show_plus
  txt << @infinity
  txt
end
regexp(*args) click to toggle source

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
# 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_s() click to toggle source
# File lib/numerals/format/symbols.rb, line 267
def to_s
  "Digits[#{parameters(true).inspect.unwrap('{}')}]"
end

Private Instance Methods

apply_case!() click to toggle source
# File lib/numerals/format/symbols.rb, line 461
def apply_case!
  if @uppercase
    @nan = @nan.upcase
    @infinity = @infinity.upcase
    @plus = @plus.upcase
    @exponent = @exponent.upcase
    @point = @point.upcase
    @group_separator = @group_separator.upcase
    @zero = @zero.upcase if @zero
    @repeat_begin = @repeat_begin.upcase
    @repeat_end   = @repeat_end.upcase
    @repeat_suffix = @repeat_suffix.upcase
    @digits.set! uppercase: true
    @padding.fill = @padding.fill.upcase if @padding.fill.is_a?(String)
  elsif @lowercase
    @nan = @nan.downcase
    @infinity = @infinity.downcase
    @plus = @plus.downcase
    @exponent = @exponent.downcase
    @point = @point.downcase
    @group_separator = @group_separator.downcase
    @zero = @zero.downcase if @zero
    @repeat_begin = @repeat_begin.downcase
    @repeat_end   = @repeat_end.downcase
    @repeat_suffix = @repeat_suffix.downcase
    @digits.set! lowercase: true
    @padding.fill = @padding.fill.downcase if @padding.filll.is_a?(String)
  end
end
cased(symbol) click to toggle source
# File lib/numerals/format/symbols.rb, line 491
def cased(symbol)
  @uppercase ? symbol.upcase : @lowercase ? symbol.downcase : symbol
end
extract_options(*args) click to toggle source
# File lib/numerals/format/symbols.rb, line 439
def extract_options(*args)
  options = {}
  args = args.first if args.size == 1 && args.first.kind_of?(Array)
  args.each do |arg|
    case arg
    when Hash
      options.merge! arg
    when Format::Symbols::Digits
      options[:digits] = arg
    when Format::Symbols
      options.merge! arg.parameters
    when :group_thousands
      options[:grouping] = [3]
    when :case_sensitive
      options[:case_sensitive] = true
    else
      raise "Invalid Symbols definition"
    end
  end
  options
end
regexp_char(c, options = {}) click to toggle source
# File lib/numerals/format/symbols.rb, line 402
def regexp_char(c, options = {})
  c_upcase = c.upcase
  c_downcase = c.downcase
  if c_downcase != c_upcase && !case_sensitive? && options[:case_sensitivity]
    "(?:#{Regexp.escape(c_upcase)}|#{Regexp.escape(c_downcase)})"
  else
    Regexp.escape(c)
  end
end
regexp_group(symbols, options = {}) click to toggle source
# File lib/numerals/format/symbols.rb, line 416
def regexp_group(symbols, options = {})
  capture = !options[:no_capture]
  symbols = Array(symbols).compact.select { |s| !s.empty? }
                          .map{ |d| regexp_symbol(d, options) }.join('|')
  if capture
    symbols = "(#{symbols})"
  else
    if symbols != ''
      symbols = "(?:#{symbols})"
      if options[:optional]
        if options[:multiple]
          symbols = "#{symbols}*"
        else
          symbols = "#{symbols}?"
        end
      elsif options[:multiple]
        symbols = "#{symbols}+"
      end
    end
  end
  symbols
end
regexp_symbol(symbol, options = {}) click to toggle source
# File lib/numerals/format/symbols.rb, line 412
def regexp_symbol(symbol, options = {})
  symbol.each_char.map { |c| regexp_char(c, options) }.join
end