Skip to content

Instantly share code, notes, and snippets.

@LouisaBarrett
Created January 30, 2014 15:48
Show Gist options
  • Save LouisaBarrett/8711477 to your computer and use it in GitHub Desktop.
Save LouisaBarrett/8711477 to your computer and use it in GitHub Desktop.
module ApplicationHelper
class Formatter
def self.format_pace(seconds)
hours = seconds/3600.to_i
minutes = (seconds/60 - hours * 60).to_i
seconds_remaining = (seconds - (minutes * 60 + hours * 3600))
if hours >= 1
"%01d:%02d:%02d" % [hours, minutes, seconds_remaining]
elsif minutes <= 9
"%01d:%02d" % [minutes, seconds_remaining]
else
"%02d:%02d" % [minutes, seconds_remaining]
end
end
def self.format_date(run)
run.workout_datetime.to_time.strftime("%a, %m/%d/%y")
end
def self.format_distance(run)
run.miles.round(2)
end
def self.format_run_time(seconds)
hours = seconds/3600.to_i
minutes = (seconds/60 - hours * 60).to_i
seconds_remaining = (seconds - (minutes * 60 + hours * 3600))
if hours >= 1
"%01d:%02d:%02d" % [hours, minutes, seconds_remaining]
elsif minutes <= 9
"%01d:%02d" % [minutes, seconds_remaining]
else
"%02d:%02d" % [minutes, seconds_remaining]
end
end
end
end
require "spec_helper"
describe "Formatter" do
it "formats for seconds that are more than 1 hour" do
seconds = 4500
formatter = ApplicationHelper::Formatter.format_pace(seconds)
expect("1:15:00").to eq(formatter)
end
it "formats for seconds that are less than 1 hour" do
seconds = 1500
formatter = ApplicationHelper::Formatter.format_pace(seconds)
expect("25:00").to eq(formatter)
end
it "formats for seconds that are less than 1 minute" do
seconds = 54
formatter = ApplicationHelper::Formatter.format_pace(seconds)
expect("00:54").to eq(formatter)
end
it "can accept seconds that are integers or floats" do
seconds = 1159.2
formatter = ApplicationHelper::Formatter.format_pace(seconds)
expect("19:32")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment