class Numerals::Format::Mode

Formatting mode

:max_trailing parameters.

The special value :engineering can be used as a shortcut for :scientific mode with :engineering :sci_int_digits.

The modes can be abbreviated as :sci, :fix, :gen and :end.

Constants

DEFAULTS
MODE_SHORTCUTS

Attributes

base_scale[RW]
max_leading[RW]
max_trailing[RW]
mode[R]
sci_int_digits[R]

Public Class Methods

new(*args) click to toggle source
# File lib/numerals/format/mode.rb, line 48
def initialize(*args)
  DEFAULTS.each do |param, value|
    instance_variable_set "@#{param}", value
  end
  set! *args
end

Public Instance Methods

engineering?() click to toggle source
# File lib/numerals/format/mode.rb, line 90
def engineering?
  @mode == :scientific && @sci_int_digits == :engineering
end
fixed?() click to toggle source
# File lib/numerals/format/mode.rb, line 98
def fixed?
  @mode == :fixed
end
general?() click to toggle source
# File lib/numerals/format/mode.rb, line 102
def general?
  @mode == :general
end
inspect() click to toggle source
# File lib/numerals/format/mode.rb, line 125
def inspect
  "Format::#{self}"
end
mode=(mode) click to toggle source
# File lib/numerals/format/mode.rb, line 73
def mode=(mode)
  @mode = MODE_SHORTCUTS[mode] || mode
  if @mode == :engineering
    @mode = :scientific
    @sci_int_digits = :engineering
  end
end
parameters(abbreviated=false) click to toggle source
# File lib/numerals/format/mode.rb, line 106
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 && engineering?
    params[:mode] = :engineering
    params.delete :sci_int_digits
  end
  params
end
sci_int_digits=(v) click to toggle source
# File lib/numerals/format/mode.rb, line 81
def sci_int_digits=(v)
  @sci_int_digits = v
  if @sci_int_digits == :eng
    @sci_int_digits = :engineering
  end
end
scientific?() click to toggle source
# File lib/numerals/format/mode.rb, line 94
def scientific?
  @mode == :scientific
end
to_s() click to toggle source
# File lib/numerals/format/mode.rb, line 121
def to_s
  "Mode[#{parameters(true).inspect.unwrap('{}')}]"
end

Private Instance Methods

extract_options(*args) click to toggle source

Note: since Mode has no mutable attributes, default dup is OK otherwise we'd need to redefine it: def dup

Mode[parameters]

end

# File lib/numerals/format/mode.rb, line 137
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 Symbol
      options[:mode] = arg
    when Format::Mode
      options.merge! arg.parameters
    else
      raise "Invalid Mode definition"
    end
  end
  options
end