module Zenaton::Traits::WithTimestamp

Module to calculate unix timestamps for events

Constants

MODE_AT
MODE_MONTH_DAY
MODE_TIMESTAMP
MODE_WEEK_DAY
WEEKDAYS

Public Instance Methods

_get_timestamp_or_duration() click to toggle source

Calculates the timestamp based on either timestamp or duration methods @return [Array<Integer, NilClass>]

# File lib/zenaton/traits/with_timestamp.rb, line 33
def _get_timestamp_or_duration
  return [nil, nil] unless @buffer

  now, now_dup = _init_now_then
  @_mode = nil
  @buffer.each do |time_unit, time_value|
    now_dup = _apply(time_unit, time_value, now, now_dup)
  end
  return [nil, diff_in_seconds(now, now_dup)] if @_mode.nil?

  [now_dup.to_i, nil]
end

Private Instance Methods

_apply(method, value, now, now_dup) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/zenaton/traits/with_timestamp.rb, line 59
def _apply(method, value, now, now_dup)
  if WEEKDAYS.include?(method)
    _weekday(value, method, now, now_dup)
  elsif method == :timestamp
    _timestamp(value)
  elsif method == :at
    _at(value, now, now_dup)
  elsif method == :day_of_month
    _day_of_month(value, now, now_dup)
  else
    _apply_duration(method, value, now_dup)
  end
end
_at(time, now, now_dup) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 88
def _at(time, now, now_dup)
  _set_mode(MODE_AT)
  now_dup = set_time_from_string(now_dup, time)
  now_dup += delay if now > now_dup
  now_dup
end
_day_of_month(day, now, now_dup) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 108
def _day_of_month(day, now, now_dup)
  _set_mode(MODE_MONTH_DAY)
  now_dup = now_dup.change(day: day)
  now_dup += 1.month if now >= now_dup && !later_today?(now, day)
  now_dup
end
_set_mode(mode) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 115
def _set_mode(mode)
  error = 'Incompatible definition in Wait methods'
  raise ExternalError,  error if mode == @_mode
  raise ExternalError, error if timestamp_mode_set?(mode)

  @_mode = mode if @_mode.nil? || @_mode == MODE_AT
end
_timestamp(timestamp) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 83
def _timestamp(timestamp)
  _set_mode(MODE_TIMESTAMP)
  timestamp
end
_weekday(value, day, now, now_dup) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/zenaton/traits/with_timestamp.rb, line 74
def _weekday(value, day, now, now_dup)
  _set_mode(MODE_WEEK_DAY)
  value -= 1 if later_today?(now, day)
  current_day_number = now.wday != 0 ? now.wday - 1 : 6
  from_now =  WEEKDAYS.index(day) - current_day_number
  from_now += 7 * value unless from_now.positive?
  now_dup.advance(days: from_now)
end
delay() click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 95
def delay
  case @_mode
  when MODE_AT
    1.day
  when MODE_WEEK_DAY
    1.week
  when MODE_MONTH_DAY
    1.month
  else
    raise InternalError "Unknown mode: #{@_mode}"
  end
end
later?(now) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 136
def later?(now)
  time = @buffer[:at]
  return false unless time

  now < set_time_from_string(now.dup, time)
end
later_today?(now, day) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 127
def later_today?(now, day)
  today?(now, day) && later?(now)
end
set_time_from_string(now, string_time) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 143
def set_time_from_string(now, string_time)
  hour, min, sec = string_time.split(':').map(&:to_i)
  now.change(hour: hour, min: min, sec: sec || 0)
end
timestamp_mode_set?(mode) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 123
def timestamp_mode_set?(mode)
  (!@_mode.nil? && MODE_TIMESTAMP == mode) || (@_mode == MODE_TIMESTAMP)
end
timezone=(timezone) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 149
def timezone=(timezone)
  error = 'Unknown timezone'
  raise ExternalError, error unless valid_timezone?(timezone)

  @@_timezone = timezone
end
today?(now, day) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 131
def today?(now, day)
  wday = WEEKDAYS[now.wday - 1]
  now.mday == day || wday == day
end
valid_timezone?(timezone) click to toggle source
# File lib/zenaton/traits/with_timestamp.rb, line 156
def valid_timezone?(timezone)
  timezone.nil? || ActiveSupport::TimeZone::MAPPING.value?(timezone)
end