Class: Hbtrack::CompletionRateSF

Inherits:
Object
  • Object
show all
Defined in:
lib/hbtrack/stat_formatter.rb

Instance Method Summary collapse

Instance Method Details

#format(hash) ⇒ String

Format in terms of the completion rate of the habit.

Parameters:

  • hash (Hash)

Options Hash (hash):

  • :done (String)

    total of done

  • :undone (String)

    total of undone

Returns:

  • (String)

    formatted result



37
38
39
40
# File 'lib/hbtrack/stat_formatter.rb', line 37

def format(hash)
  percentage = to_percentage(hash)[:done]
  sprintf("Completion rate: %.2f%%", percentage)
end

#to_percentage(hash) ⇒ Hash

Convert the value in the hash into percentage

Parameters:

  • hash (Hash)

Options Hash (hash):

  • :done (String)

    total of done

  • :undone (String)

    total of undone

Returns:

  • (Hash)

    formatted result



47
48
49
50
51
52
53
54
55
56
# File 'lib/hbtrack/stat_formatter.rb', line 47

def to_percentage(hash)
  total = hash[:done] + hash[:undone]
  done_p = 0
  undone_p = 0
  unless total.zero?
    done_p = hash[:done] / total.to_f * 100
    undone_p = hash[:undone] / total.to_f * 100
  end
  { done: done_p, undone: undone_p }
end