class PrettyTime::Core

Core class which provides the entry point to other useful methods

Public Instance Methods

dump(time_as_pretty_string) click to toggle source

Core method that does all the work

# File lib/pretty_time/pretty_time.rb, line 164
def dump(time_as_pretty_string)
  time_in_secs = 0
  match_data_as_array = match_it(time_as_pretty_string).to_a
  match_data_as_array.slice(1..match_data_as_array.length).each_slice(2).each do |match_array|
    time_in_secs += "PrettyTime::#{match_array[1].strip.classify}".constantize.new(match_array[0].to_i).to_seconds
  end

  time_in_secs
end
load(time_in_secs) click to toggle source

Core method that does all the work

# File lib/pretty_time/pretty_time.rb, line 133
def load(time_in_secs)
  if time_in_secs.kind_of? Float
    time_in_secs = time_in_secs.round
  end

  hours, minutes, seconds = hours_and_minutes_and_seconds(time_in_secs)

  time_as_pretty_string = "" 

  if has_hours?(hours)
    time_as_pretty_string << "#{with_suffix(hours, PrettyTime.config.hours_suffix)}" 
  end

  if has_minutes?(minutes)
    if has_hours?(hours)
      time_as_pretty_string << " "
    end
    time_as_pretty_string << "#{with_suffix(minutes, PrettyTime.config.minutes_suffix)}"
  end

  if has_seconds?(seconds)
    if has_hours?(hours) || has_minutes?(minutes)
      time_as_pretty_string << " "
    end
    time_as_pretty_string << "#{with_suffix(seconds, PrettyTime.config.seconds_suffix)}"
  end

  time_as_pretty_string
end

Private Instance Methods

has_hours?(value)
Alias for: non_zero?
has_minutes?(value)
Alias for: non_zero?
has_seconds?(value)
Alias for: non_zero?
hours_and_minutes_and_seconds(time_in_sec) click to toggle source

Converts time in seconds to an array of hours, minutes and seconds

# File lib/pretty_time/pretty_time.rb, line 178
def hours_and_minutes_and_seconds(time_in_sec)
  minutes, seconds = minutes_and_seconds(time_in_sec)
  hours = 0
  if minutes >= 60
    hours = minutes / 60
    minutes = minutes % 60
  end
  [hours, minutes, seconds]
end
match_it(time_as_pretty_string) click to toggle source

Takes a string (i.e. time as a string) and matches it against various regular expressions and returns the match as MatchData Example:

match_it("4 hours 40 minutes")
match_it("4 hrs 40 mins")
match_it("4 hrs 1 min")
# File lib/pretty_time/pretty_time.rb, line 218
def match_it(time_as_pretty_string)
  /^(\d+) (hours?|hrs?|h) (\d+) (minutes?|mins?|m) (\d+) (seconds?|secs?|s)/.match(time_as_pretty_string) ||
  /^(\d+) (hours?|hrs?|h) (\d+) (minutes?|mins?|m)/.match(time_as_pretty_string) ||
  /^(\d+) (minutes?|mins?|m) (\d+) (seconds?|secs?|s)/.match(time_as_pretty_string) ||
  /^(\d+) (hours?|hrs?|h) (\d+) (seconds?|secs?|s)/.match(time_as_pretty_string) ||

  /^(\d+)( h| m| s)/.match(time_as_pretty_string) ||
  /^(\d+)( hours?| minutes?| seconds?)/.match(time_as_pretty_string)  ||
  /^(\d+)( hrs?| mins?| secs?)/.match(time_as_pretty_string)
end
minutes_and_seconds(time_in_sec) click to toggle source

Converts time in seconds to an array of minutes and seconds

# File lib/pretty_time/pretty_time.rb, line 189
def minutes_and_seconds(time_in_sec)
  [time_in_sec / 60, time_in_sec % 60]
end
non_zero?(value) click to toggle source

Checks if supplied value is zero

# File lib/pretty_time/pretty_time.rb, line 194
def non_zero?(value)
  value != 0
end
Also aliased as: has_hours?, has_minutes?, has_seconds?
with_suffix(value, type) click to toggle source

Adds the correct suffix Example:

1 hour # becuase it's == 1 so singular
3 hours # becuase it's > 1 so plural
# File lib/pretty_time/pretty_time.rb, line 203
def with_suffix(value, type)
  if value > 1
    return "#{value} #{type}"
  end

  return "#{value} #{type.gsub(/s$/, '')}"
end