Class: Opencdd::ValueFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/opencdd/value_format.rb

Constant Summary collapse

CODES =
%w[NR1 NR2 NR3 M].freeze
PATTERN =
/\A
  (?<code>NR1|NR2|NR3|M)
  (?:
    \s*(?<signed>S)?
    \.\.(?<total>\d+)
    (?:\.(?<frac>\d+))?
  )?
\z/x.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, signed: false, total: nil, fractional: nil) ⇒ ValueFormat

Returns a new instance of ValueFormat.



9
10
11
12
13
14
15
16
17
# File 'lib/opencdd/value_format.rb', line 9

def initialize(code:, signed: false, total: nil, fractional: nil)
  unless CODES.include?(code.to_s)
    raise ArgumentError, "unknown value_format code: #{code.inspect}"
  end
  @code = code.to_s
  @signed = signed
  @total = total
  @fractional = fractional
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/opencdd/value_format.rb', line 7

def code
  @code
end

#fractionalObject (readonly)

Returns the value of attribute fractional.



7
8
9
# File 'lib/opencdd/value_format.rb', line 7

def fractional
  @fractional
end

#signedObject (readonly)

Returns the value of attribute signed.



7
8
9
# File 'lib/opencdd/value_format.rb', line 7

def signed
  @signed
end

#totalObject (readonly)

Returns the value of attribute total.



7
8
9
# File 'lib/opencdd/value_format.rb', line 7

def total
  @total
end

Class Method Details

.parse(raw) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/opencdd/value_format.rb', line 58

def parse(raw)
  return nil if raw.nil? || raw.to_s.strip.empty?
  s = raw.to_s.strip
  match = s.match(PATTERN)
  return nil unless match
  code = match[:code]
  signed = !match[:signed].nil?
  total = match[:total]&.to_i
  fractional = match[:frac]&.to_i
  new(code: code, signed: signed, total: total, fractional: fractional)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



35
36
37
38
39
40
41
# File 'lib/opencdd/value_format.rb', line 35

def ==(other)
  other.is_a?(Opencdd::ValueFormat) &&
    @code == other.code &&
    @signed == other.signed &&
    @total == other.total &&
    @fractional == other.fractional
end

#hashObject



44
45
46
# File 'lib/opencdd/value_format.rb', line 44

def hash
  [@code, @signed, @total, @fractional].hash
end

#numeric?Boolean

Returns:

  • (Boolean)


19
# File 'lib/opencdd/value_format.rb', line 19

def numeric? = @code != "M"

#string?Boolean

Returns:

  • (Boolean)


20
# File 'lib/opencdd/value_format.rb', line 20

def string?  = @code == "M"

#to_sObject Also known as: inspect



22
23
24
25
26
27
28
29
30
31
# File 'lib/opencdd/value_format.rb', line 22

def to_s
  if @code == "M"
    @total ? "M..#{@total}" : "M"
  else
    sign = @signed ? "S" : ""
    frac = @fractional ? ".#{@fractional}" : ""
    total_part = @total ? "..#{@total}" : ""
    "#{@code} #{sign}#{total_part}#{frac}".strip
  end
end