Class: Plurimath::Formatter::Numbers::BaseNotation

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

Overview

Describes base prefix/postfix notation as a resolved value object. Constructed from FormatOptions via .from_options; carries the resolved prefix and postfix strings so renderers can decide how to represent the base (inline text, MathML subscript, LaTeX subscript, etc.).

Constant Summary collapse

DEFAULT_PREFIXES =
{
  2 => "0b",
  8 => "0o",
  10 => "",
  16 => "0x",
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base:, prefix: "", postfix: "", hex_capital: nil, explicit_prefix: false, explicit_postfix: false) ⇒ BaseNotation

Returns a new instance of BaseNotation.



20
21
22
23
24
25
26
27
28
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 20

def initialize(base:, prefix: "", postfix: "", hex_capital: nil,
               explicit_prefix: false, explicit_postfix: false)
  @base = base
  @prefix = prefix
  @postfix = postfix
  @hex_capital = hex_capital
  @explicit_prefix = explicit_prefix
  @explicit_postfix = explicit_postfix
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



18
19
20
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 18

def base
  @base
end

#hex_capitalObject (readonly)

Returns the value of attribute hex_capital.



18
19
20
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 18

def hex_capital
  @hex_capital
end

#postfixObject (readonly)

Returns the value of attribute postfix.



18
19
20
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 18

def postfix
  @postfix
end

#prefixObject (readonly)

Returns the value of attribute prefix.



18
19
20
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 18

def prefix
  @prefix
end

Class Method Details

.from_options(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 30

def self.from_options(options)
  base = options.base
  validate!(base)

  new(
    base: base,
    prefix: resolve_prefix(options, base),
    postfix: options.base_postfix.to_s,
    hex_capital: options.hex_capital,
    explicit_prefix: options.base_prefix?,
    explicit_postfix: options.base_postfix?,
  )
end

.supported?(base) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 44

def self.supported?(base)
  DEFAULT_PREFIXES.key?(base)
end

Instance Method Details

#default?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 48

def default?
  base == Base::DEFAULT_BASE
end

#literal?Boolean

Caller overrode prefix or postfix at format-call time. Renderers that honor semantic base notation (msub, mathrm{}_#base, _(base)) must defer to the literal prefix/postfix in that case.

Returns:

  • (Boolean)


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

def literal?
  !default? && (explicit_prefix? || explicit_postfix?)
end

#semantic?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 59

def semantic?
  !default? && !literal?
end

#upcase_hex?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 63

def upcase_hex?
  base == 16 && hex_capital == true
end

#wrap(digits) ⇒ Object



67
68
69
# File 'lib/plurimath/formatter/numbers/base_notation.rb', line 67

def wrap(digits)
  "#{prefix}#{digits}#{postfix}"
end