Class: PrettyTime::Core

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

Overview

Core class which provides the entry point to other useful methods

Instance Method Summary collapse

Instance Method Details

#dump(time_as_pretty_string) ⇒ Object

Core method that does all the work



164
165
166
167
168
169
170
171
172
# 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) ⇒ Object

Core method that does all the work



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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