Class: HyperCast::NumFormat

Inherits:
Data
  • Object
show all
Defined in:
lib/hypercast.rb

Overview

Caller-declared numeric notation for the integer and real doors — declared out loud (INVARIANT, or a literal), never defaulted, the same stance every binding takes. Equal separators are a caller bug (ArgumentError), never a verdict.

Constant Summary collapse

INVARIANT =

The invariant profile — '.' decimal, ',' grouping, every lenience on.

NumFormat.new(decimal_sep: ".", group_sep: ",", flags: ALL_STYLES)
DETECT =

The detection profile — every lenience on, ./, roles resolved per input by SEPARATOR_DETECT's structural rules.

NumFormat.new(decimal_sep: ".", group_sep: ",",
flags: ALL_STYLES | SEPARATOR_DETECT)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decimal_sep:, group_sep:, flags:) ⇒ NumFormat

Validates the declared separators up front — single characters, and distinct from each other — so a malformed format fails loudly as the caller bug it is.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
# File 'lib/hypercast.rb', line 45

def initialize(decimal_sep:, group_sep:, flags:)
  raise ArgumentError, "separators must be single characters" unless
    decimal_sep.is_a?(String) && decimal_sep.length == 1 &&
    group_sep.is_a?(String) && group_sep.length == 1
  raise ArgumentError, "decimal and group separators must differ; both are #{decimal_sep.inspect}" if
    decimal_sep == group_sep
  super
end

Instance Attribute Details

#decimal_sepObject (readonly)

Returns the value of attribute decimal_sep

Returns:

  • (Object)

    the current value of decimal_sep



42
43
44
# File 'lib/hypercast.rb', line 42

def decimal_sep
  @decimal_sep
end

#flagsObject (readonly)

Returns the value of attribute flags

Returns:

  • (Object)

    the current value of flags



42
43
44
# File 'lib/hypercast.rb', line 42

def flags
  @flags
end

#group_sepObject (readonly)

Returns the value of attribute group_sep

Returns:

  • (Object)

    the current value of group_sep



42
43
44
# File 'lib/hypercast.rb', line 42

def group_sep
  @group_sep
end

Instance Method Details

#packedObject

The 12-byte little-endian form the native ABI's NumFormat struct expects.



55
56
57
# File 'lib/hypercast.rb', line 55

def packed
  [decimal_sep.ord, group_sep.ord, flags].pack("L<L<L<")
end