class Trifle::Stats::Nocturnal

Constants

DAYS_INTO_WEEK

Public Class Methods

new(at, config: nil) click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 23
def initialize(at, config: nil)
  @at = at
  @config = config
end
timeline(from:, to:, range:, config: nil) click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 11
def self.timeline(from:, to:, range:, config: nil)
  list = []
  from = new(from, config: config).send(range)
  to = new(to, config: config).send(range)
  item = from.dup
  while item <= to
    list << item
    item = Nocturnal.new(item, config: config).send("next_#{range}")
  end
  list
end

Public Instance Methods

change(**fractions) click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 32
def change(**fractions)
  Time.new(
    fractions.fetch(:year, @at.year),
    fractions.fetch(:month, @at.month),
    fractions.fetch(:day, @at.day),
    fractions.fetch(:hour, @at.hour),
    fractions.fetch(:minute, @at.min),
    0, # second
    config.tz.utc_offset
  )
end
config() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 28
def config
  @config || Trifle::Stats.default
end
day() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 66
def day
  change(hour: 0, minute: 0)
end
days_to_week_start() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 90
def days_to_week_start
  start_day_number = DAYS_INTO_WEEK.fetch(
    config.beginning_of_week
  )

  (@at.wday - start_day_number) % 7
end
hour() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 55
def hour
  change(minute: 0)
end
minute() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 44
def minute
  change
end
month() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 98
def month
  change(day: 1, hour: 0, minute: 0)
end
next_day() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 70
def next_day
  Nocturnal.new(
    day + 60 * 60 * 24,
    config: config
  ).day
end
next_hour() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 59
def next_hour
  Nocturnal.new(
    hour + 60 * 60,
    config: config
  ).hour
end
next_minute() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 48
def next_minute
  Nocturnal.new(
    minute + 60,
    config: config
  ).minute
end
next_month() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 102
def next_month
  Nocturnal.new(
    month + 60 * 60 * 24 * 31,
    config: config
  ).month
end
next_quarter() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 120
def next_quarter
  Nocturnal.new(
    quarter + 60 * 60 * 24 * 31 * 3,
    config: config
  ).quarter
end
next_week() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 83
def next_week
  Nocturnal.new(
    week + 60 * 60 * 24 * 7,
    config: config
  ).week
end
next_year() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 131
def next_year
  Nocturnal.new(
    year + 60 * 60 * 24 * 31 * 12,
    config: config
  ).year
end
quarter() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 109
def quarter
  first_quarter_month = @at.month - (2 + @at.month) % 3

  change(
    month: first_quarter_month,
    day: 1,
    hour: 0,
    minute: 0
  )
end
week() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 77
def week
  today = day

  (today.to_date - days_to_week_start).to_time
end
year() click to toggle source
# File lib/trifle/stats/nocturnal.rb, line 127
def year
  change(month: 1, day: 1, hour: 0, minute: 0)
end