Class: Wixy::Alphabet

Inherits:
Object
  • Object
show all
Defined in:
lib/wixy/alphabet.rb

Instance Method Summary collapse

Constructor Details

#initialize(chars, options = {}) ⇒ Alphabet

chars is an array of character strings, like [“A”, “B”, “C” …]



4
5
6
7
8
# File 'lib/wixy/alphabet.rb', line 4

def initialize(chars, options = {})
  @chars = chars.to_a.rotate(options[:shift] || 0)
  @index = Hash[@chars.each_with_index.to_a]
  @filters = options[:filters] || []
end

Instance Method Details

#[](index) ⇒ Object



10
11
12
# File 'lib/wixy/alphabet.rb', line 10

def [](index)
  @chars[index % @chars.length]
end

#char(char, other_alphabet) ⇒ Object

Given a char and another alphabet, find the char in this alphabet at the corresponding index



37
38
39
# File 'lib/wixy/alphabet.rb', line 37

def char(char, other_alphabet)
  self[other_alphabet.index(char)]
end

#filtered(char) ⇒ Object



22
23
24
# File 'lib/wixy/alphabet.rb', line 22

def filtered(char)
  @filters.inject(char) { |c, f| c.public_send(f) }
end

#include?(char) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/wixy/alphabet.rb', line 18

def include?(char)
  index(filtered char)
end

#index(char) ⇒ Object



14
15
16
# File 'lib/wixy/alphabet.rb', line 14

def index(char)
  i = @index[filtered char]
end

#sanitize(text_or_chars) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/wixy/alphabet.rb', line 26

def sanitize(text_or_chars)
  chars = if text_or_chars.respond_to?(:chars)
            text_or_chars.chars
          else
            text_or_chars
          end
  chars.map {|c| filtered(c) }.map { |c| char_if_present(c) }.compact
end

#to_sObject



41
42
43
# File 'lib/wixy/alphabet.rb', line 41

def to_s
  "<Alphabet:[#{@chars.join}]>"
end