Skip to content

Instantly share code, notes, and snippets.

@paulca
Created January 24, 2014 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulca/8606241 to your computer and use it in GitHub Desktop.
Save paulca/8606241 to your computer and use it in GitHub Desktop.
def display_date_or_range(event, sup = true, html = true, short = false)
return if event.start_date.blank? and event.end_date.blank?
if event.single_day?
if short
out = event.start_date.strftime("%b")
else
out = event.start_date.strftime("%B")
end
out << " "
if sup === false
out << event.start_date.day.ordinalize
else
out << ordinalize_with_sup(event.start_date)
end
out << ", #{event.start_date.year}"
return out.html_safe
end
if event.multi_day?
out = ''
if short
out << event.start_date.strftime("%b ")
else
out << event.start_date.strftime("%B ")
end
if sup === false
out << event.start_date.day.ordinalize
else
out << ordinalize_with_sup(event.start_date)
end
if event.spans_different_years?
out << ", #{event.start_date.year}"
end
if html
out << '&ndash;'
else
out << HTMLEntities.new.decode('&ndash;')
end
if event.spans_different_months? or (!event.spans_different_months? && event.spans_different_years?)
out << event.end_date.strftime("%B ")
end
if sup === false
out << event.end_date.day.ordinalize
else
out << ordinalize_with_sup(event.end_date)
end
out << ", #{event.end_date.year}"
out.html_safe
end
end
class DateOrRange
attr_accessor :start_date, :end_date, :options
def initialize(options = {})
@start_date = options.delete(:start_date)
@end_date = options.delete(:end_date)
@end_date = nil if @start_date == @end_date
@options = options
end
def single_day?
start_date.present? && end_date.blank?
end
def start_date_month
return if start_date.blank?
start_date.strftime("%B")
end
def start_date_month_word
start_date_month
end
def start_date_number
start_date.day.ordinalize
end
def start_date_year
start_date.year
end
def start_date_year_word
start_date_year if end_date.blank? || (start_date_year != end_date_year)
end
def start_date_part
return if start_date.blank?
[
start_date_month_word,
"#{start_date_number}#{',' if start_date_year != end_date_year}",
(start_date_year_word)
].reject(&:nil?).join(' ')
end
def end_date_month
end_date.strftime("%B")
end
def end_date_month_word
end_date_month if (end_date_month != start_date_month) || (start_date_year != end_date_year)
end
def end_date_number
end_date.day.ordinalize
end
def end_date_year
end_date.year if end_date.present?
end
def end_date_part
return if end_date.blank?
[
end_date_month_word,
"#{end_date_number},",
end_date_year
].reject(&:nil?).join(' ')
end
def to_s
[
start_date_part,
end_date_part
].reject(&:nil?).join('&ndash;')
end
end
@peterc
Copy link

peterc commented Jan 24, 2014

.reject(&:nil?) => .compact

@paulca
Copy link
Author

paulca commented Jan 24, 2014

@peterc — Mind. Blown. I've never used #compact, I always just gloss over it when I see it

@billhorsman
Copy link

I like reject(&:blank?) because it also copes with empty strings.

> ["", "Last"].compact.join(" ")
=> " Last"

whereas:

> ["", "Last"].reject(&:blank?).join(" ")
=> "Last"

I've sometimes done it like this:

> ["", "Last"].compact.join(" ").strip
=> "Last"

That doesn't work as well with more than two words where one in the middle is of zero length (you get a double space).

To my shame, I've also used the very inelegant .select {|w| !w.blank? } so I'm glad to have been reminded about reject.

See also my gist about titleizing names, which could incorporate some of these ideas too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment