module Jekyll::Timeago::Core

Constants

DEFAULT_DEPTH_LEVEL

Default level of detail

DEFAULT_THRESHOLD

Default threshold

MAX_DEPTH_LEVEL

Max level of detail: years > months > weeks > days

Public Instance Methods

timeago(from, to = Date.today, options = {}) click to toggle source
# File lib/jekyll-timeago/core.rb, line 17
def timeago(from, to = Date.today, options = {})
  if to.is_a?(Hash)
    options = to
    to = Date.today
  end

  @options = options

  from      = validate_date(from)
  to        = validate_date(to)
  depth     = validate_depth(@options[:depth] || @options["depth"])
  threshold = validate_threshold(@options[:threshold] || @options["threshold"])

  time_ago_to_now(from, to, depth, threshold)
end

Private Instance Methods

build_time_ago_slots(days_passed, depth, threshold, current_slots = []) click to toggle source

Builds time ranges: [‘1 month’, ‘5 days’]

  • days_passed: integer in absolute

  • depth: level of detail

  • threshold: minimum fractional difference to keep for next slot

  • current_slots: built time slots

# File lib/jekyll-timeago/core.rb, line 69
def build_time_ago_slots(days_passed, depth, threshold, current_slots = [])
  return current_slots if depth == 0 || days_passed == 0

  range     = days_to_range(days_passed)
  days      = days_in(range)
  num_elems = (days_passed / days).to_i

  current_slots << t(range, count: num_elems)

  pending_days = days_passed - (num_elems * days)

  if pending_days >= (days_passed * threshold).floor
    build_time_ago_slots(pending_days, depth - 1, threshold, current_slots)
  else
    current_slots
  end
end
days_in(range) click to toggle source
# File lib/jekyll-timeago/core.rb, line 96
def days_in(range)
  case range
  when :days then 1
  when :weeks then 7
  when :months then 30
  when :years then 365
  end
end
days_to_range(days) click to toggle source
# File lib/jekyll-timeago/core.rb, line 87
def days_to_range(days)
  case days.abs
  when 1..6 then :days
  when 7..30 then :weeks
  when 31..365 then :months
  else :years
  end
end
t(key, options = {}) click to toggle source
# File lib/jekyll-timeago/core.rb, line 60
def t(key, options = {})
  MiniI18n.t(key, @options.merge(options))
end
time_ago_to_now(from, to, depth, threshold) click to toggle source
# File lib/jekyll-timeago/core.rb, line 47
def time_ago_to_now(from, to, depth, threshold)
  days_passed = (to - from).to_i

  return t(:today)     if days_passed == 0
  return t(:yesterday) if days_passed == 1
  return t(:tomorrow)  if days_passed == -1

  past_or_future = from < to ? :past : :future
  slots = build_time_ago_slots(days_passed.abs, depth, threshold)

  t(past_or_future, date_range: to_sentence(slots))
end
to_sentence(slots) click to toggle source

Array to sentence: [‘1 month’, ‘1 week’, ‘5 days’] => “1 month, 1 week and 5 days”

# File lib/jekyll-timeago/core.rb, line 106
def to_sentence(slots)
  if slots.length == 1
    slots[0]
  else
    "#{slots[0...-1].join(t(:words_connector))} #{t(:last_word_connector)} #{slots[-1]}"
  end
end
validate_date(date) click to toggle source
# File lib/jekyll-timeago/core.rb, line 35
def validate_date(date)
  Date.parse(date.to_s)
end
validate_depth(depth) click to toggle source
# File lib/jekyll-timeago/core.rb, line 43
def validate_depth(depth)
  (1..MAX_DEPTH_LEVEL).include?(depth) ? depth : DEFAULT_DEPTH_LEVEL
end
validate_threshold(threshold) click to toggle source
# File lib/jekyll-timeago/core.rb, line 39
def validate_threshold(threshold)
  (0.0..1.0).include?(threshold) ? threshold : DEFAULT_THRESHOLD
end