Module: ActiveObject::Time

Defined in:
lib/active_object/time.rb

Instance Method Summary collapse

Instance Method Details

#count_centuries_since(time) ⇒ Object Also known as: count_centuries_until


166
167
168
# File 'lib/active_object/time.rb', line 166

def count_centuries_since(time)
  count_seconds_since(time) / CENTURY
end

#count_days_since(time) ⇒ Object Also known as: count_days_until


172
173
174
# File 'lib/active_object/time.rb', line 172

def count_days_since(time)
  count_seconds_since(time) / DAY
end

#count_decades_since(time) ⇒ Object Also known as: count_decades_until


178
179
180
# File 'lib/active_object/time.rb', line 178

def count_decades_since(time)
  count_seconds_since(time) / DECADE
end

#count_hours_since(time) ⇒ Object Also known as: count_hours_until


184
185
186
# File 'lib/active_object/time.rb', line 184

def count_hours_since(time)
  count_seconds_since(time) / HOUR
end

#count_milleniums_since(time) ⇒ Object Also known as: count_milleniums_until


190
191
192
# File 'lib/active_object/time.rb', line 190

def count_milleniums_since(time)
  count_seconds_since(time) / MILLENNIUM
end

#count_minutes_since(time) ⇒ Object Also known as: count_minutes_until


196
197
198
# File 'lib/active_object/time.rb', line 196

def count_minutes_since(time)
  count_seconds_since(time) / MINUTE
end

#count_seconds_since(time) ⇒ Object Also known as: count_seconds_until


202
203
204
205
206
# File 'lib/active_object/time.rb', line 202

def count_seconds_since(time)
  time = time.to_time if time.respond_to?(:to_time)

  (to_f - time.to_f).abs
end

#count_weeks_since(time) ⇒ Object Also known as: count_weeks_until


210
211
212
# File 'lib/active_object/time.rb', line 210

def count_weeks_since(time)
  count_seconds_since(time) / WEEK
end

#count_years_since(time) ⇒ Object Also known as: count_years_until


216
217
218
# File 'lib/active_object/time.rb', line 216

def count_years_since(time)
  count_seconds_since(time) / YEAR
end

#format(string) ⇒ Object


222
223
224
225
226
227
228
# File 'lib/active_object/time.rb', line 222

def format(string)
  delimiters = string.scan(/\W+/)
  formatters = string.scan(/[a-z0-9_]+/i)
  string = formatters.map { |unit| "%#{STRING_UNITS.fetch(unit.to_sym)}#{delimiters.shift}" }

  strftime(string.join)
end

#to_format(key = :datetime_iso) ⇒ Object Also known as: stamp


230
231
232
# File 'lib/active_object/time.rb', line 230

def to_format(key = :datetime_iso)
  strftime(KEY_UNITS.fetch(key.to_sym))
end