Class: Gtiny::Encoding

Inherits:
Object
  • Object
show all
Defined in:
lib/gtiny/encoding.rb

Overview

An abstract class that defines the base for various types of text-based identifier encodings like GTIN-8, ISBN-10, etc.

Direct Known Subclasses

GTIN, ISBN

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, check: true) ⇒ Encoding

Returns a new instance of Encoding.

Raises:



29
30
31
32
33
# File 'lib/gtiny/encoding.rb', line 29

def initialize(string, check: true)
  raise(EncodingError, "not a valid encoding") if check && !self.class.matches?(string)

  @string = self.class.normalize(string.freeze)
end

Class Method Details

.matches?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/gtiny/encoding.rb', line 7

def self.matches?(candidate)
  regex.match?(candidate)
end

.normalize(string) ⇒ Object



11
12
13
# File 'lib/gtiny/encoding.rb', line 11

def self.normalize(string)
  string
end

.regexObject

Raises:

  • (NotImplementedError)


15
16
17
# File 'lib/gtiny/encoding.rb', line 15

def self.regex
  raise(NotImplementedError, "not implemented")
end

Instance Method Details

#to_sObject



35
36
37
# File 'lib/gtiny/encoding.rb', line 35

def to_s
  string
end

#typeObject



39
# File 'lib/gtiny/encoding.rb', line 39

def type; end

#valid?Boolean

Returns:

  • (Boolean)


40
# File 'lib/gtiny/encoding.rb', line 40

def valid?; end

#zeroes?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/gtiny/encoding.rb', line 19

def zeroes?
  digits.match?(/\A0+\z/)
end