Class: Plurimath::Formatter::Numbers::FormatOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/formatter/numbers/format_options.rb

Overview

Per-render view of merged formatter symbols and resolved precision.

Constant Summary collapse

DEFAULT_EXPONENT_SEPARATOR =
:e
DEFAULT_DECIMAL =
"."
DEFAULT_FRACTION_PRECISION =
3
DEFAULT_GROUP =
","
DEFAULT_GROUP_DIGITS =
3
DEFAULT_PADDING =
"0"
DEFAULT_TIMES =
"\u{d7}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil, symbols: {}, precision: nil, precision_resolver: nil) ⇒ FormatOptions

Returns a new instance of FormatOptions.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 19

def initialize(
  source = nil,
  symbols: {},
  precision: nil,
  precision_resolver: nil
)
  @symbols = symbols.dup
  @notation = symbol_option(:notation)
  @exponent_separator = symbol_option(:e) || DEFAULT_EXPONENT_SEPARATOR
  @times = symbol_option(:times) || DEFAULT_TIMES
  @precision = resolve_precision(source, precision, precision_resolver)
  @exponent_sign = symbol_option(:exponent_sign)
  validate_padding_options!
end

Instance Attribute Details

#exponent_separatorObject (readonly)

Returns the value of attribute exponent_separator.



16
17
18
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 16

def exponent_separator
  @exponent_separator
end

#exponent_signObject (readonly)

Returns the value of attribute exponent_sign.



16
17
18
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 16

def exponent_sign
  @exponent_sign
end

#notationObject (readonly)

Returns the value of attribute notation.



16
17
18
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 16

def notation
  @notation
end

#precisionObject (readonly)

Returns the value of attribute precision.



16
17
18
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 16

def precision
  @precision
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



16
17
18
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 16

def symbols
  @symbols
end

#timesObject (readonly)

Returns the value of attribute times.



16
17
18
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 16

def times
  @times
end

Instance Method Details

#baseObject



34
35
36
37
38
39
40
41
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 34

def base
  value = symbols[:base]
  return Base::DEFAULT_BASE if value.nil?

  # Numeric String/Symbol forms are normalized; anything else is left
  # raw so BaseNotation reports it through UnsupportedBase.
  coerce_integer(value) { return value }
end

#base_postfixObject



51
52
53
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 51

def base_postfix
  separator_option(:base_postfix)
end

#base_postfix?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 55

def base_postfix?
  symbols.key?(:base_postfix)
end

#base_prefixObject



43
44
45
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 43

def base_prefix
  separator_option(:base_prefix).to_s
end

#base_prefix?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 47

def base_prefix?
  symbols.key?(:base_prefix)
end

#decimalObject



59
60
61
62
63
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 59

def decimal
  # An explicitly passed nil renders without a separator; output
  # correctness is then the caller's responsibility.
  separator_option(:decimal, default: DEFAULT_DECIMAL)
end

#digit_countObject



65
66
67
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 65

def digit_count
  integer_option(:digit_count, default: 0)
end

#explicit_precision?Boolean

Whether precision came from the caller (kwarg or symbols) rather than being inferred from the source by the resolver.

Returns:

  • (Boolean)


132
133
134
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 132

def explicit_precision?
  @explicit_precision
end

#fraction_groupObject



69
70
71
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 69

def fraction_group
  separator_option(:fraction_group).to_s
end

#fraction_group_digitsObject



73
74
75
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 73

def fraction_group_digits
  integer_option(:fraction_group_digits)
end

#groupObject



77
78
79
80
81
82
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 77

def group
  # Unlike decimal, an explicit nil group falls back to the default
  # separator; use "" to disable grouping.
  (separator_option(:group,
                    default: DEFAULT_GROUP) || DEFAULT_GROUP).to_s
end

#group_digitsObject



84
85
86
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 84

def group_digits
  integer_option(:group_digits, default: DEFAULT_GROUP_DIGITS)
end

#hex_capitalObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 88

def hex_capital
  value = symbols[:hex_capital]
  # nil is handled first: under Opal `when nil` spuriously matches
  # non-nil values (nil === 1.5 is true there), so it must not appear
  # in the type whitelist below.
  return if value.nil?

  # Accept only Boolean/String/Symbol and reject any other type.
  # Attribute-parsing callers deliver String/Symbol (:true,
  # "numbers_only"), so the accepted forms normalize through to_s.
  case value
  when true, false, String, Symbol
    case value.to_s
    when "true" then true
    when "numbers_only" then :numbers_only
    end
  else
    invalid_option!(:hex_capital, value)
  end
end

#notation_supported?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 126

def notation_supported?
  NotationRenderer.supported?(notation)
end

#number_signObject



109
110
111
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 109

def number_sign
  symbol_option(:number_sign)
end

#paddingObject



113
114
115
116
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 113

def padding
  value = separator_option(:padding, default: DEFAULT_PADDING).to_s
  value.empty? ? DEFAULT_PADDING : value[0]
end

#padding_digitsObject



118
119
120
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 118

def padding_digits
  integer_option(:padding_digits, default: 0)
end

#padding_group_digitsObject



122
123
124
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 122

def padding_group_digits
  integer_option(:padding_group_digits, default: 0)
end

#significantObject



136
137
138
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 136

def significant
  integer_option(:significant, default: 0)
end

#to_hObject



140
141
142
# File 'lib/plurimath/formatter/numbers/format_options.rb', line 140

def to_h
  symbols.dup
end