serialnumber 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/serialnumber.rb +2 -1
- data/lib/serialnumber/rfc1982.rb +44 -11
- data/serialnumber.gemspec +2 -2
- data/spec/serial_spec.rb +13 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.10
|
data/lib/serialnumber.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# Author
|
1
|
+
# Author:: David Krentzlin <david.krentzlin@lisp-unleashed.de>
|
2
|
+
# Copyright:: Copyright (c) 2011 David Krentzlin
|
2
3
|
#
|
3
4
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
4
5
|
# copy of this software and associated documentation files (the "Software"),
|
data/lib/serialnumber/rfc1982.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# Author
|
1
|
+
# Author:: David Krentzlin <david.krentzlin@lisp-unleashed.de>
|
2
|
+
# Copyright:: Copyright (c) 2011 David Krentzlin
|
2
3
|
#
|
3
4
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
4
5
|
# copy of this software and associated documentation files (the "Software"),
|
@@ -19,15 +20,35 @@
|
|
19
20
|
# OTHER DEALINGS IN THE SOFTWARE.
|
20
21
|
|
21
22
|
|
22
|
-
|
23
23
|
module Serial
|
24
|
+
|
24
25
|
class OutOfBound < RuntimeError; end
|
25
26
|
class Invalid < RuntimeError; end
|
26
27
|
|
28
|
+
# For some serial-numbers a comparision is
|
29
|
+
# simply not defined. In this case an Exception of this
|
30
|
+
# kind will be raised
|
31
|
+
class ComparisonImpossible < RuntimeError; end
|
32
|
+
|
33
|
+
# This class implements a serialnumber with it's operations as
|
34
|
+
# described in rfc1982. This may for example be used to manage
|
35
|
+
# serials as present in SOA-Records
|
36
|
+
#
|
37
|
+
# It defines only one basic operation, that is adding a fixnum.
|
38
|
+
# It is does also define an order.
|
39
|
+
#
|
40
|
+
# == Examples
|
41
|
+
# sn = Serial::RFC1982.new(100)
|
42
|
+
# sn2 = sn + 12
|
43
|
+
# sn2 > sn
|
44
|
+
#
|
27
45
|
class RFC1982
|
28
46
|
include Comparable
|
29
47
|
attr_reader :serial_bits,:value
|
30
48
|
|
49
|
+
# Creates a serialnumber object from the fixnum given
|
50
|
+
# * the serial_bits define the domain of integers that can be
|
51
|
+
# represented with the fixnum.
|
31
52
|
def initialize(number,serial_bits = 32)
|
32
53
|
raise OutOfBound,"The number of serial_bits must be >= 2" unless serial_bits >= 2
|
33
54
|
@serial_bits = serial_bits
|
@@ -35,14 +56,9 @@ module Serial
|
|
35
56
|
@value = number
|
36
57
|
end
|
37
58
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
if bits_needed_for(number) > serial_bits
|
42
|
-
raise OutOfBound,"Given number #{number} can not be represented with #{serial_bits} serial_bits"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
59
|
+
# Add a fixnum value to the serialnumber.
|
60
|
+
# Note that the resulting value may be smaller than
|
61
|
+
# the initial value due to a wrap-around on the serial-bits boundary
|
46
62
|
def +(number)
|
47
63
|
if summand_out_of_bound?(number)
|
48
64
|
raise OutOfBound,"The summand #{number} is out of bound"
|
@@ -51,6 +67,15 @@ module Serial
|
|
51
67
|
Serial::RFC1982.new(sum,serial_bits)
|
52
68
|
end
|
53
69
|
|
70
|
+
def to_s
|
71
|
+
value.to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_i
|
75
|
+
value.to_i
|
76
|
+
end
|
77
|
+
|
78
|
+
protected
|
54
79
|
def <=>(other)
|
55
80
|
unless other.respond_to?(:value)
|
56
81
|
raise Invalid,"Expected #{other} to respond_to :value"
|
@@ -58,9 +83,17 @@ module Serial
|
|
58
83
|
return 0 if equal?(value,other.value)
|
59
84
|
return -1 if less?(value,other.value)
|
60
85
|
return 1 if greater?(value,other.value)
|
86
|
+
raise ComparisonImpossible, "The comparison is undefined"
|
87
|
+
end
|
88
|
+
|
89
|
+
def ensure_valid!(number)
|
90
|
+
raise Invalid,"Serial must be a fixnum" unless %w(Fixnum Bignum).include?(number.class.name)
|
91
|
+
raise OutOfBound,"Negative numbers are not allowd" if number < 0
|
92
|
+
if bits_needed_for(number) > serial_bits
|
93
|
+
raise OutOfBound,"Given number #{number} can not be represented with #{serial_bits} serial_bits"
|
94
|
+
end
|
61
95
|
end
|
62
96
|
|
63
|
-
protected
|
64
97
|
def boundary
|
65
98
|
@boundary ||= (2 ** (serial_bits - 1))
|
66
99
|
end
|
data/serialnumber.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{serialnumber}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.10"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["David Krentzlin"]
|
9
|
-
s.date = %q{2011-07-
|
9
|
+
s.date = %q{2011-07-14}
|
10
10
|
s.description = %q{Serialarithmetic as described in rfc1982}
|
11
11
|
s.email = ["david.krentzlin@lisp-unleashed.de"]
|
12
12
|
s.files = [".hgignore", ".hgtags", "Gemfile", "Gemfile.lock", "README", "Rakefile", "VERSION", "autotest/discover.rb", "lib/serialnumber.rb", "lib/serialnumber/rfc1982.rb", "serialnumber.gemspec", "spec/serial_spec.rb", "spec/spec_helper.rb"]
|
data/spec/serial_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# Author
|
1
|
+
# Author:: David Krentzlin <david.krentzlin@lisp-unleashed.de>
|
2
|
+
# Copyright:: Copyright (c) 2011 David Krentzlin
|
2
3
|
#
|
3
4
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
4
5
|
# copy of this software and associated documentation files (the "Software"),
|
@@ -19,7 +20,7 @@
|
|
19
20
|
# OTHER DEALINGS IN THE SOFTWARE.
|
20
21
|
|
21
22
|
|
22
|
-
require '
|
23
|
+
require 'serialnumber/rfc1982'
|
23
24
|
|
24
25
|
describe Serial::RFC1982 do
|
25
26
|
|
@@ -90,5 +91,15 @@ describe Serial::RFC1982 do
|
|
90
91
|
s101.should > s100
|
91
92
|
end
|
92
93
|
end
|
94
|
+
|
95
|
+
it "allows the value to be retrieved as a string" do
|
96
|
+
Serial::RFC1982.new(100).to_s.should eq("100")
|
97
|
+
end
|
98
|
+
|
99
|
+
it "allows the value to be retrieved as a integer" do
|
100
|
+
Serial::RFC1982.new(100).to_i.should eq(100)
|
101
|
+
end
|
102
|
+
|
103
|
+
|
93
104
|
end
|
94
105
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serialnumber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 10
|
10
|
+
version: 0.0.10
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David Krentzlin
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-14 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|