Class: GreekABC::Alphabet

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

Overview

Alphabet representation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArray<Letter>

Initialize an Alphabet object, a collection of all letters



15
16
17
18
19
20
21
22
23
24
# File 'lib/greek_abc/alphabet.rb', line 15

def initialize
  @letters = ALPHABET.map.with_index(1) do |(name, letter_variant), index|
    # @type var letter_variant: String | Array[untyped]
    letter = letter_variant[0]
    letter_alt = letter_variant[1]

    Letter.new(position: index, name: name, lower: letter,
               lower_alt: letter_alt, upper: letter.upcase)
  end
end

Instance Attribute Details

#lettersArray<Letter> (readonly)

Returns an array of all letters of the alphabet.

Returns:

  • (Array<Letter>)

    an array of all letters of the alphabet



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/greek_abc/alphabet.rb', line 9

class Alphabet
  attr_reader :letters

  # Initialize an Alphabet object, a collection of all letters
  #
  # @return [Array<Letter>]
  def initialize
    @letters = ALPHABET.map.with_index(1) do |(name, letter_variant), index|
      # @type var letter_variant: String | Array[untyped]
      letter = letter_variant[0]
      letter_alt = letter_variant[1]

      Letter.new(position: index, name: name, lower: letter,
                 lower_alt: letter_alt, upper: letter.upcase)
    end
  end

  # Finds a letter by given lookup parameteres
  # @example
  #   GreekABC::Alphabet.new.find_letter(name: 'Lambda') # => #<GreekABC::Letter:...
  # @param [Hash{Symbol => String, Integer, nil}] lookup_params
  #   A hash, with some {Letter} attribute as a key and desired lookup value
  # @return [Letter]
  # @see LetterLookup#find_letter
  def find_letter(**lookup_params)
    LetterLookup.new(letters).find_letter(**lookup_params)
  end

  # Hash representation of an alphabet
  # @return [Hash{String => String, Integer}]
  def to_h
    letters.each_with_object({}) do |letter, hash|
      hash[letter.name.to_s] = {
        position: letter.position,
        lower: letter.lower,
        lower_alt: letter.lower_alt,
        upper: letter.upper
      }
    end
  end

  # String representation of an alphabet
  # @return [String]
  def to_s
    letters
  end
end

Instance Method Details

#find_letter(**lookup_params) ⇒ Letter

Finds a letter by given lookup parameteres

Examples:

GreekABC::Alphabet.new.find_letter(name: 'Lambda') # => #<GreekABC::Letter:...

Parameters:

  • lookup_params (Hash{Symbol => String, Integer, nil})

    A hash, with some Letter attribute as a key and desired lookup value

Returns:

See Also:



33
34
35
# File 'lib/greek_abc/alphabet.rb', line 33

def find_letter(**lookup_params)
  LetterLookup.new(letters).find_letter(**lookup_params)
end

#to_hHash{String => String, Integer}

Hash representation of an alphabet

Returns:

  • (Hash{String => String, Integer})


39
40
41
42
43
44
45
46
47
48
# File 'lib/greek_abc/alphabet.rb', line 39

def to_h
  letters.each_with_object({}) do |letter, hash|
    hash[letter.name.to_s] = {
      position: letter.position,
      lower: letter.lower,
      lower_alt: letter.lower_alt,
      upper: letter.upper
    }
  end
end

#to_sString

String representation of an alphabet

Returns:

  • (String)


52
53
54
# File 'lib/greek_abc/alphabet.rb', line 52

def to_s
  letters
end