Class: EncodedId::Alphabet
- Inherits:
-
Object
- Object
- EncodedId::Alphabet
- Defined in:
- lib/encoded_id/alphabet.rb
Overview
Represents a character set (alphabet) used for encoding IDs, with optional character equivalences.
Constant Summary collapse
- MIN_UNIQUE_CHARACTERS =
16
Instance Attribute Summary collapse
-
#characters ⇒ Object
readonly
: String.
-
#equivalences ⇒ Object
readonly
: Hash[String, String]?.
-
#unique_characters ⇒ Object
readonly
: Array.
Class Method Summary collapse
Instance Method Summary collapse
- #include?(character) ⇒ Boolean
-
#initialize(characters, equivalences = nil) ⇒ Alphabet
constructor
A new instance of Alphabet.
- #inspect ⇒ Object
- #size ⇒ Object (also: #length)
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(characters, equivalences = nil) ⇒ Alphabet
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/encoded_id/alphabet.rb', line 34 def initialize(characters, equivalences = nil) raise_invalid_alphabet! unless valid_input_characters?(characters) @unique_characters = unique_character_alphabet(characters) raise_invalid_characters! unless valid_characters? raise_character_set_too_small! unless sufficient_characters? raise_invalid_equivalences! unless valid_equivalences?(equivalences) @characters = unique_characters.join @equivalences = equivalences end |
Instance Attribute Details
#characters ⇒ Object (readonly)
: String
46 47 48 |
# File 'lib/encoded_id/alphabet.rb', line 46 def characters @characters end |
#equivalences ⇒ Object (readonly)
: Hash[String, String]?
47 48 49 |
# File 'lib/encoded_id/alphabet.rb', line 47 def equivalences @equivalences end |
#unique_characters ⇒ Object (readonly)
: Array
45 46 47 |
# File 'lib/encoded_id/alphabet.rb', line 45 def unique_characters @unique_characters end |
Class Method Details
.alphanum ⇒ Object
24 25 26 |
# File 'lib/encoded_id/alphabet.rb', line 24 def alphanum new("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") end |
.modified_crockford ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/encoded_id/alphabet.rb', line 12 def modified_crockford new( "0123456789abcdefghjkmnpqrstuvwxyz", { "o" => "0", "i" => "j", "l" => "1" } ) end |
Instance Method Details
#include?(character) ⇒ Boolean
50 51 52 |
# File 'lib/encoded_id/alphabet.rb', line 50 def include?(character) unique_characters.include?(character) end |
#inspect ⇒ Object
65 66 67 |
# File 'lib/encoded_id/alphabet.rb', line 65 def inspect "#<#{self.class.name} chars: #{unique_characters.inspect}>" end |
#size ⇒ Object Also known as: length
70 71 72 |
# File 'lib/encoded_id/alphabet.rb', line 70 def size unique_characters.size end |
#to_a ⇒ Object
55 56 57 |
# File 'lib/encoded_id/alphabet.rb', line 55 def to_a unique_characters.dup end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/encoded_id/alphabet.rb', line 60 def to_s @characters.dup end |