Class: ISTC
- Inherits:
-
Object
- Object
- ISTC
- Defined in:
- lib/istc.rb
Defined Under Namespace
Classes: Version
Class Method Summary collapse
-
.complete(num) ⇒ Object
Purely for generating new ISTC numbers.
- .valid?(istc) ⇒ Boolean
Instance Method Summary collapse
- #agency ⇒ Object
- #check ⇒ Object
-
#initialize(str) ⇒ ISTC
constructor
A new instance of ISTC.
-
#to_s ⇒ Object
output a hyphenated version of this ISTC.
- #valid? ⇒ Boolean
- #work ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(str) ⇒ ISTC
Returns a new instance of ISTC.
11 12 13 |
# File 'lib/istc.rb', line 11 def initialize(str) @number = str.to_s end |
Class Method Details
.complete(num) ⇒ Object
Purely for generating new ISTC numbers
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/istc.rb', line 52 def self.complete(num) num = num.to_s return nil unless num.length == 15 && num.match(/[\dA-Fa-f]{11}/) arr = (0..14).to_a.collect do |i| if i == 0 || i % 4 == 0 num[i,1].hex * 11 elsif i % 4 == 1 num[i,1].hex * 9 elsif i % 4 == 2 num[i,1].hex * 3 else num[i,1].hex * 1 end end sum = arr.inject { |sum, n| sum + n } remainder = sum % 16 check = remainder.to_s(16).upcase "#{num}#{check}" end |
Instance Method Details
#agency ⇒ Object
19 20 21 22 |
# File 'lib/istc.rb', line 19 def agency return nil unless valid? @number[0,3] end |
#check ⇒ Object
34 35 36 37 |
# File 'lib/istc.rb', line 34 def check return nil unless valid? @number[15,1] end |
#to_s ⇒ Object
output a hyphenated version of this ISTC
40 41 42 43 |
# File 'lib/istc.rb', line 40 def to_s return nil unless valid? "#{agency}-#{year}-#{work}-#{check}" end |
#work ⇒ Object
29 30 31 32 |
# File 'lib/istc.rb', line 29 def work return nil unless valid? @number[7,8] end |
#year ⇒ Object
24 25 26 27 |
# File 'lib/istc.rb', line 24 def year return nil unless valid? @number[3,4] end |