module HublotAr

Constants

VERSION

Public Instance Methods

pretty(clock=Time.now) click to toggle source

Clock argument for testing; defaults to Time

# File lib/hublot_ar/pretty.rb, line 5
def pretty(clock=Time.now)
  @expired = (clock-self).to_i
  @today = clock.strftime('%A')
  @created = self.to_time.strftime('%A')

  @days = {
    "Monday" => 1,
    "Tuesday" => 2,
    "Wednesday" => 3,
    "Thursday" => 4,
    "Friday" => 5,
    "Saturday" => 6,
    "Sunday" => 7
  }

  @days_ar = {
    "Monday" => "الإثنين",
    "Tuesday" => "الثلاثاء",
    "Wednesday" => "الأربعاء",
    "Thursday" => "الخميس",
    "Friday" => "الجمعة",
    "Saturday" => "السبت",
    "Sunday" => "الأحد"
  }

  return just_now     if just_now?
  return a_second_ago if a_second_ago?
  return seconds_ago  if seconds_ago?
  return a_minute_ago if a_minute_ago?
  return minutes_ago  if minutes_ago?
  return an_hour_ago  if an_hour_ago?
  return today        if is_today?
  return yesterday    if is_yesterday?
  return this_week    if this_week?
  return last_week    if last_week?
  return datetimefiesta
end

Private Instance Methods

a_minute_ago() click to toggle source
# File lib/hublot_ar/pretty.rb, line 68
def a_minute_ago
  "منذ دقيقة"
end
a_minute_ago?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 72
def a_minute_ago?
  @expired >= 60 && @expired <= 119 #120 = 2 minutes
end
a_second_ago() click to toggle source
# File lib/hublot_ar/pretty.rb, line 52
def a_second_ago
  "منذ ثانية"
end
a_second_ago?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 56
def a_second_ago?
  @expired == 1
end
an_hour_ago() click to toggle source
# File lib/hublot_ar/pretty.rb, line 84
def an_hour_ago
  "منذ ساعة"
end
an_hour_ago?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 88
def an_hour_ago?
  @expired >= 3600 && @expired <= 7199 # 3600 = 1 hour
end
ar_num(nums) click to toggle source
# File lib/hublot_ar/pretty.rb, line 142
def ar_num(nums)
  nums.to_s
  .gsub('0', '٠')
  .gsub('1', '١')
  .gsub('2', '٢')
  .gsub('3', '٣')
  .gsub('4', '٤')
  .gsub('5', '٥')
  .gsub('6', '٦')
  .gsub('7', '٧')
  .gsub('8', '٨')
  .gsub('9', '٩')
end
datetimefiesta() click to toggle source
# File lib/hublot_ar/pretty.rb, line 128
def datetimefiesta
  self.strftime("%A, %B %e at %l:%M %p")
end
is_today?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 100
def is_today?
  @days[@today] - @days[@created] == 0 && @expired >= 7200 && @expired <= 82800
end
is_yesterday?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 108
def is_yesterday?
  (@days[@today] - @days[@created] == 1 || @days[@created] + @days[@today] == 8) && @expired <= 172800
end
just_now() click to toggle source
# File lib/hublot_ar/pretty.rb, line 44
def just_now
  "الآن"
end
just_now?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 48
def just_now?
  @expired == 0
end
last_week() click to toggle source
# File lib/hublot_ar/pretty.rb, line 120
def last_week
  localize_day(@created) + "الماضي، الساعة " + localize_time(timeify)
end
last_week?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 124
def last_week?
  @expired >= 518400 && @expired <= 1123200
end
localize_day(day) click to toggle source
# File lib/hublot_ar/pretty.rb, line 138
def localize_day(day)
  @days_ar[day]
end
localize_time(timeify) click to toggle source
# File lib/hublot_ar/pretty.rb, line 132
def localize_time(timeify)
  clock = timeify.split(' ').first.split(':').map{|e| ar_num(e.to_i)}.join(':')
  time = timeify.split(' ').last == 'AM' ? "ص" : "م"
  " #{clock} #{time}"
end
minutes_ago() click to toggle source
# File lib/hublot_ar/pretty.rb, line 76
def minutes_ago
  "منذ #{ar_num (@expired/60)} دقائق"
end
minutes_ago?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 80
def minutes_ago?
  @expired >= 120 && @expired <= 3599
end
seconds_ago() click to toggle source
# File lib/hublot_ar/pretty.rb, line 60
def seconds_ago
  "منذ #{ar_num @expired} ثواني"
end
seconds_ago?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 64
def seconds_ago?
  @expired >= 2 && @expired <= 59
end
this_week() click to toggle source
# File lib/hublot_ar/pretty.rb, line 112
def this_week
  localize_day(@created) + "، الساعة " + localize_time(timeify)
end
this_week?() click to toggle source
# File lib/hublot_ar/pretty.rb, line 116
def this_week?
  @expired <= 604800 && @days[@today] - @days[@created] != 0
end
timeify() click to toggle source
# File lib/hublot_ar/pretty.rb, line 96
def timeify
  "#{self.to_time.strftime("%l:%M %p")}"
end
today() click to toggle source
# File lib/hublot_ar/pretty.rb, line 92
def today
  "اليوم، الساعة " + localize_time(timeify)
end
yesterday() click to toggle source
# File lib/hublot_ar/pretty.rb, line 104
def yesterday
  "الأمس، الساعة " + localize_time(timeify)
end