Module: Flocks::Ordering

Included in:
Flocks
Defined in:
lib/flocks/ordering.rb

Instance Method Summary collapse

Instance Method Details

#rank_username(username) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/flocks/ordering.rb', line 3

def rank_username(username)
  # Selects the number of characters to be evaluated for sorting
  percision_index = string_score_percision - 1

  # Base score
  score = 0

  # Get the first 5 characters in an array
  characters = username.to_s.downcase[0..percision_index].split('')

  # If the username is short, tack on the lowest ascii value
  characters << '0' until characters.size == string_score_percision

  # Reverse them for easy multiplication scoring
  characters.reverse.each_with_index do |char, i|

    # Use the mutiplier, increase magnitude for weighting first letters more
    # multiplier = (character_multiplier[0..(i + 1)].to_i)

    multiplier = 10 ** i
    # Square the multiplier to avoid large ascii values from thwrowing off calculations
    char_score = "#{char}"[0].ord * multiplier * multiplier

    # Aggregate the swcore
    score += char_score
  end

  score
end