Class: Serial::RFC1982

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/serialnumber/rfc1982.rb

Overview

This class implements a serialnumber with it’s operations as described in rfc1982. This may for example be used to manage the serial part of SOA-Records.

It defines only two basic operation; addition and comparison. Note that due to wrap-arounds you might encounter interesting results if you increment the serialnumber. As serialnumber may wrap the following expression may not always hold: sn > sn2 => (sn + 1) > sn2

Examples

sn  = Serial::RFC1982.new(100)
sn2 = sn + 12
sn2 > sn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, serial_bits = 32) ⇒ RFC1982

Creates a serialnumber object from the fixnum given

Raises:



49
50
51
52
53
54
# File 'lib/serialnumber/rfc1982.rb', line 49

def initialize(number,serial_bits = 32)
  raise OutOfBound,"The number of serial_bits must be >= 1" unless serial_bits >= 1
  ensure_valid_serial!(number,serial_bits)
  @serial_bits = serial_bits
  @value       = number
end

Instance Attribute Details

#serial_bitsObject (readonly)

Returns the value of attribute serial_bits.



46
47
48
# File 'lib/serialnumber/rfc1982.rb', line 46

def serial_bits
  @serial_bits
end

#valueObject (readonly)

Returns the value of attribute value.



46
47
48
# File 'lib/serialnumber/rfc1982.rb', line 46

def value
  @value
end

Instance Method Details

#+(number) ⇒ Object

Add a fixnum value to the serialnumber. Not that this may actually wrap if it exceeds the serialnumber’s boundary given by serial_bits



59
60
61
62
63
64
65
# File 'lib/serialnumber/rfc1982.rb', line 59

def +(number)
  if summand_out_of_bound?(number)
    raise OutOfBound,"The summand #{number} is out of bound"
  end
  sum = (value + number) % (2 ** serial_bits)
  Serial::RFC1982.new(sum,serial_bits)
end

#to_iObject



71
72
73
# File 'lib/serialnumber/rfc1982.rb', line 71

def to_i
  value.to_i
end

#to_sObject



67
68
69
# File 'lib/serialnumber/rfc1982.rb', line 67

def to_s
  value.to_s
end