class Almanack::Event

Public Instance Methods

description() click to toggle source
# File lib/almanack/event.rb, line 31
def description
  self[:description]&.force_encoding('UTF-8')
end
end_date() click to toggle source

Deprecated in favour of end_time

# File lib/almanack/event.rb, line 45
def end_date
  deprecated :end_date, newer_method: :end_time
end
end_time() click to toggle source
# File lib/almanack/event.rb, line 49
def end_time
  read_attribute :end_time, fallback: :end_date
end
formatted_date() click to toggle source
# File lib/almanack/event.rb, line 5
def formatted_date
  warn "formatted_date is deprecated, please use formatted_duration instead"
  formatted_duration
end
formatted_duration() click to toggle source
# File lib/almanack/event.rb, line 10
def formatted_duration
  formatted = "#{formatted_day(start_time)}"
  formatted << " at #{formatted_time(start_time)}" unless start_time.is_a?(Date)

  if end_time
    formatted << " to " unless is_date_ending_on_same_day?
    formatted << "#{formatted_day(end_time)} at " unless ends_on_same_day?
    formatted << formatted_time(end_time) unless end_time.is_a?(Date)
  end

  formatted
end
location() click to toggle source
# File lib/almanack/event.rb, line 27
def location
  self[:location]&.force_encoding('UTF-8')
end
serialized() click to toggle source
# File lib/almanack/event.rb, line 53
def serialized
  each_pair.with_object({}) do |(attr, _), hash|
    hash[attr] = serialize_attribute(attr)
  end
end
start_date() click to toggle source

Deprecated in favour of start_time

# File lib/almanack/event.rb, line 36
def start_date
  deprecated :start_date, newer_method: :start_time
end
start_time() click to toggle source
# File lib/almanack/event.rb, line 40
def start_time
  read_attribute :start_time, fallback: :start_date
end
title() click to toggle source
# File lib/almanack/event.rb, line 23
def title
  self[:title]&.force_encoding('UTF-8')
end

Private Instance Methods

deprecated(older_method, options = {}) click to toggle source
# File lib/almanack/event.rb, line 66
def deprecated(older_method, options = {})
  newer_method = options.delete(:newer_method)
  value = read_attribute(newer_method, fallback: older_method)
  warn "Event method #{older_method} is deprecated; use #{newer_method} instead"
  value
end
ends_on_same_day?() click to toggle source
# File lib/almanack/event.rb, line 92
def ends_on_same_day?
  [start_time.year, start_time.yday] == [end_time.year, end_time.yday]
end
formatted_day(time) click to toggle source
# File lib/almanack/event.rb, line 100
def formatted_day(time)
  time.strftime('%B %-d %Y')
end
formatted_time(time) click to toggle source
# File lib/almanack/event.rb, line 96
def formatted_time(time)
  time.strftime('%-l:%M%P')
end
is_date_ending_on_same_day?() click to toggle source
# File lib/almanack/event.rb, line 88
def is_date_ending_on_same_day?
  end_time.is_a?(Date) && ends_on_same_day?
end
read_attribute(newer_method, options = {}) click to toggle source
# File lib/almanack/event.rb, line 73
def read_attribute(newer_method, options = {})
  older_method = options.delete(:fallback)
  newer_value = self[newer_method]
  fallback_value = self[older_method]

  if fallback_value && newer_value
    raise "Both #{older_method} and #{newer_method} properties are set, please use #{newer_method} only instead"
  elsif newer_value
    newer_value
  elsif fallback_value
    warn "Deprecated event property #{older_method} is set; set #{newer_method} property instead"
    fallback_value
  end
end
serialize_attribute(attribute) click to toggle source
# File lib/almanack/event.rb, line 61
def serialize_attribute(attribute)
  value = send(attribute)
  value.is_a?(Time) ? value.iso8601 : value
end