Class: Noid::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/noid/template.rb

Constant Summary collapse

VALID_PATTERN =
/\A(.*)\.([rsz])([ed]+)(k?)\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Template

Returns a new instance of Template.

Parameters:

  • template (String)

    A Template is a coded string of the form Prefix.Mask that governs how identifiers will be minted.



8
9
10
11
# File 'lib/noid/template.rb', line 8

def initialize(template)
  @template = template
  parse!
end

Instance Attribute Details

#charactersObject (readonly)

Returns the value of attribute characters.



3
4
5
# File 'lib/noid/template.rb', line 3

def characters
  @characters
end

#generatorObject (readonly)

Returns the value of attribute generator.



3
4
5
# File 'lib/noid/template.rb', line 3

def generator
  @generator
end

#prefixObject (readonly)

Returns the value of attribute prefix.



3
4
5
# File 'lib/noid/template.rb', line 3

def prefix
  @prefix
end

#templateObject (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

Parameters:

  • str (String)

Returns:

  • (String)

    checkdigit



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

#maxObject

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

#minObject

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_sObject



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?

Parameters:

  • str (String)

Returns:

  • (Boolean)

    bool



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