Class: Noid::Template
- Inherits:
-
Object
- Object
- Noid::Template
- Defined in:
- lib/noid/template.rb
Constant Summary collapse
- VALID_PATTERN =
/\A(.*)\.([rsz])([ed]+)(k?)\Z/
Instance Attribute Summary collapse
-
#characters ⇒ Object
readonly
Returns the value of attribute characters.
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#checkdigit(str) ⇒ String
calculate a checkdigit for the str.
-
#initialize(template) ⇒ Template
constructor
A new instance of Template.
-
#max ⇒ Object
maximum sequence value for the template.
-
#min ⇒ Object
minimum sequence value.
- #mint(n) ⇒ Object
- #to_s ⇒ Object
-
#valid?(str) ⇒ Boolean
Is the string valid against this template string and checksum?.
Constructor Details
#initialize(template) ⇒ Template
Returns a new instance of Template.
8 9 10 11 |
# File 'lib/noid/template.rb', line 8 def initialize(template) @template = template parse! end |
Instance Attribute Details
#characters ⇒ Object (readonly)
Returns the value of attribute characters.
3 4 5 |
# File 'lib/noid/template.rb', line 3 def characters @characters end |
#generator ⇒ Object (readonly)
Returns the value of attribute generator.
3 4 5 |
# File 'lib/noid/template.rb', line 3 def generator @generator end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
3 4 5 |
# File 'lib/noid/template.rb', line 3 def prefix @prefix end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
3 4 5 |
# File 'lib/noid/template.rb', line 3 def template @template end |
Instance Method Details
#==(other) ⇒ Object
49 50 51 52 |
# File 'lib/noid/template.rb', line 49 def ==(other) return false unless other.is_a? Noid::Template template == other.template end |
#checkdigit(str) ⇒ String
calculate a checkdigit for the str
35 36 37 |
# File 'lib/noid/template.rb', line 35 def checkdigit(str) Noid::XDIGIT[str.split('').map { |x| Noid::XDIGIT.index(x).to_i }.each_with_index.map { |n, idx| n * (idx + 1) }.inject { |sum, n| sum + n } % Noid::XDIGIT.length] end |
#max ⇒ Object
maximum sequence value for the template
56 57 58 59 60 61 62 |
# File 'lib/noid/template.rb', line 56 def max @max ||= if generator == 'z' nil else size_list.inject(1) { |total, x| total * x } end end |
#min ⇒ Object
minimum sequence value
41 42 43 |
# File 'lib/noid/template.rb', line 41 def min @min ||= 0 end |
#mint(n) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/noid/template.rb', line 13 def mint(n) str = prefix str += n2xdig(n) str += checkdigit(str) if checkdigit? str end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/noid/template.rb', line 45 def to_s template end |
#valid?(str) ⇒ Boolean
Is the string valid against this template string and checksum?
24 25 26 27 28 29 |
# File 'lib/noid/template.rb', line 24 def valid?(str) match = validation_regex.match(str) return false if match.nil? return checkdigit(match[1]) == match[3] if checkdigit? true end |