class Hbtrack::CompletionRateSF

Public Instance Methods

format(hash) click to toggle source

Format in terms of the completion rate of the habit. @param hash [Hash] @option hash [String] :done total of done @option hash [String] :undone total of undone @return [String] formatted result

# 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) click to toggle source

Convert the value in the hash into percentage @param hash [Hash] @option hash [String] :done total of done @option hash [String] :undone total of undone @return [Hash] formatted result

# 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