Class: Datey::Formatter

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

Instance Method Summary collapse

Constructor Details

#initialize(time, context = :past) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
10
# File 'lib/datey/formatter.rb', line 7

def initialize(time, context = :past)
  @time = time
  @context = context
end

Instance Method Details

#date(options = {}) ⇒ Object

Return the formatted date (no time)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/datey/formatter.rb', line 33

def date(options = {})
  non_relative_prefix = options[:non_relative_prefix] || "on "
  if interrogator.today?
    result = "today"
  elsif interrogator.yesterday?
    result = "yesterday"
  elsif interrogator.tomorrow?
    result = "tomorrow"
  elsif @context == :future && interrogator.next_x_days?(6)
    result = non_relative_prefix + name_of_day
  elsif @context == :future && interrogator.next_week?
    result = "next #{name_of_day}"
  elsif @context == :past && interrogator.last_x_days?(6)
    result = non_relative_prefix + name_of_day
  elsif @context == :past && interrogator.last_x_days?(12)
    result = "last #{name_of_day}"
  elsif @context == :past && interrogator.next_x_days?(6)
    result = "next #{name_of_day}"
  else
    result = non_relative_prefix + "#{name_of_day} #{day_of_month} #{name_of_month}"
    result += " #{@time.year}" if include_year_in_dates?
    result
  end

  unless options[:capitalize] == false
    result[0] = result[0].upcase
  end

  result
end

#date_and_time(options = {}) ⇒ Object

Return the date and time



15
16
17
# File 'lib/datey/formatter.rb', line 15

def date_and_time(options = {})
  @time.is_a?(Time) ? "#{date(options)} at #{time(options)}" : date(options = {})
end

#day_of_monthObject

Return the day of the month ordinalized



67
68
69
70
71
72
73
74
75
# File 'lib/datey/formatter.rb', line 67

def day_of_month
  ordinal = case @time.day
  when 1 then "st"
  when 2 then "nd"
  when 3 then "rd"
  else "th"
  end
  "#{@time.day}#{ordinal}"
end

#name_of_day(style = :full) ⇒ Object

Return the name of the day



80
81
82
# File 'lib/datey/formatter.rb', line 80

def name_of_day(style = :full)
  @time.strftime(style == :full ? "%A" : "%a")
end

#name_of_month(style = :full) ⇒ Object

Return the name of the month



87
88
89
# File 'lib/datey/formatter.rb', line 87

def name_of_month(style = :full)
  @time.strftime(style == :full ? "%B" : "%b")
end

#time(options = {}) ⇒ Object

Return the time nicely



22
23
24
25
26
27
28
# File 'lib/datey/formatter.rb', line 22

def time(options = {})
  if @time.is_a?(Time)
    @time.strftime("%l#{@time.min > 0 ? ":%M" : ''}%P").strip
  else
    nil
  end
end