Class: Gtiny::Encoding
- Inherits:
-
Object
- Object
- Gtiny::Encoding
- 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.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(string, check: true) ⇒ Encoding
constructor
A new instance of Encoding.
- #to_s ⇒ Object
- #type ⇒ Object
- #valid? ⇒ Boolean
- #zeroes? ⇒ Boolean
Constructor Details
#initialize(string, check: true) ⇒ Encoding
Returns a new instance of Encoding.
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
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 |
.regex ⇒ Object
15 16 17 |
# File 'lib/gtiny/encoding.rb', line 15 def self.regex raise(NotImplementedError, "not implemented") end |
Instance Method Details
#to_s ⇒ Object
35 36 37 |
# File 'lib/gtiny/encoding.rb', line 35 def to_s string end |
#type ⇒ Object
39 |
# File 'lib/gtiny/encoding.rb', line 39 def type; end |
#valid? ⇒ Boolean
40 |
# File 'lib/gtiny/encoding.rb', line 40 def valid?; end |
#zeroes? ⇒ Boolean
19 20 21 |
# File 'lib/gtiny/encoding.rb', line 19 def zeroes? digits.match?(/\A0+\z/) end |