Class: GreekABC::LetterLookup

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

Overview

Letter lookup utility class

Constant Summary collapse

LOOKUP_PARAMETERS =

All available letter attributes.

See Also:

%i[name position lower lower_alt upper].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(letters) ⇒ LetterLookup

Returns a new instance of LetterLookup.

Parameters:

  • letters (Array<Letters>)

    An array of letters generated by GreekABC::Alphabet.new.letters



18
19
20
# File 'lib/greek_abc/letter_lookup.rb', line 18

def initialize(letters)
  @letters = letters
end

Instance Attribute Details

#lettersObject (readonly)

Returns the value of attribute letters.



8
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
# File 'lib/greek_abc/letter_lookup.rb', line 8

class LetterLookup
  # @dynamic letters
  attr_reader :letters

  # All available letter attributes.
  # @see #letter_parameter?
  LOOKUP_PARAMETERS = %i[name position lower lower_alt upper].freeze

  # @param [Array<Letters>] letters
  #   An array of letters generated by GreekABC::Alphabet.new.letters
  def initialize(letters)
    @letters = letters
  end

  # Finds a letter by given lookup parameteres
  # @example
  #   letters = GreekABC::Alphabet.new.letters
  #   GreekABC::LetterLookup.new(letters).find_letter(name: 'Lambda') # => #<GreekABC::Letter:...
  # @param [Hash{Symbol => String, Integer}] lookup_params
  #   A hash, with some {Letter} attribute as a key and desired lookup value
  # @return [Letter]
  # @raise [LetterLookupParamsError] if passed parameter is not an existing
  #   letter attribute
  # @raise [LetterNotFoundError] if a letter with given parameters does not exist
  # @see Alphabet#find_letter
  def find_letter(**lookup_params) # rubocop:disable Metrics/AbcSize
    # @type var lookup_params: Hash[Symbol | untyped, String | Integer | nil]
    parameter = lookup_params.keys.first.to_sym
    value = lookup_params.values.first

    raise LetterLookupParamsError, parameter unless letter_parameter?(parameter)

    result = letters.find do |letter|
      letter if letter.send(parameter).to_s.downcase == value.to_s.downcase
    end

    raise LetterNotFoundError.new(parameter, value) unless result

    result
  end

  private

  def letter_parameter?(parameter)
    LOOKUP_PARAMETERS.include?(parameter)
  end
end

Instance Method Details

#find_letter(**lookup_params) ⇒ Letter

Finds a letter by given lookup parameteres

Examples:

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

Parameters:

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

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

Returns:

Raises:

See Also:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/greek_abc/letter_lookup.rb', line 33

def find_letter(**lookup_params) # rubocop:disable Metrics/AbcSize
  # @type var lookup_params: Hash[Symbol | untyped, String | Integer | nil]
  parameter = lookup_params.keys.first.to_sym
  value = lookup_params.values.first

  raise LetterLookupParamsError, parameter unless letter_parameter?(parameter)

  result = letters.find do |letter|
    letter if letter.send(parameter).to_s.downcase == value.to_s.downcase
  end

  raise LetterNotFoundError.new(parameter, value) unless result

  result
end

#letter_parameter?(parameter) ⇒ Boolean (private)

Returns:

  • (Boolean)


51
52
53
# File 'lib/greek_abc/letter_lookup.rb', line 51

def letter_parameter?(parameter)
  LOOKUP_PARAMETERS.include?(parameter)
end