Class: EncodedId::Alphabet

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#charactersObject (readonly)

: String



46
47
48
# File 'lib/encoded_id/alphabet.rb', line 46

def characters
  @characters
end

#equivalencesObject (readonly)

: Hash[String, String]?



47
48
49
# File 'lib/encoded_id/alphabet.rb', line 47

def equivalences
  @equivalences
end

#unique_charactersObject (readonly)

: Array



45
46
47
# File 'lib/encoded_id/alphabet.rb', line 45

def unique_characters
  @unique_characters
end

Class Method Details

.alphanumObject



24
25
26
# File 'lib/encoded_id/alphabet.rb', line 24

def alphanum
  new("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
end

.modified_crockfordObject



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

#inspectObject



65
66
67
# File 'lib/encoded_id/alphabet.rb', line 65

def inspect
  "#<#{self.class.name} chars: #{unique_characters.inspect}>"
end

#sizeObject Also known as: length



70
71
72
# File 'lib/encoded_id/alphabet.rb', line 70

def size
  unique_characters.size
end

#to_aObject



55
56
57
# File 'lib/encoded_id/alphabet.rb', line 55

def to_a
  unique_characters.dup
end

#to_sObject



60
61
62
# File 'lib/encoded_id/alphabet.rb', line 60

def to_s
  @characters.dup
end