Class: BellyWash::Nocturnal

Inherits:
Object
  • Object
show all
Defined in:
lib/belly_wash/nocturnal.rb

Constant Summary collapse

DAYS_INTO_WEEK =
{
  sunday: 0, monday: 1, tuesday: 2, wednesday: 3,
  thursday: 4, friday: 5, saturday: 6
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(at) ⇒ Nocturnal

Returns a new instance of Nocturnal.



20
21
22
23
# File 'lib/belly_wash/nocturnal.rb', line 20

def initialize(at)
  @at = at
  @tz = BellyWash.config.tz
end

Class Method Details

.timeline(from:, to:, range:) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/belly_wash/nocturnal.rb', line 10

def self.timeline(from:, to:, range:)
  list = []
  item = new(from).send("beginning_of_#{range}")
  while item < to
    list << item
    item = BellyWash::Nocturnal.new(list.last).send("next_#{range}")
  end
  list
end

Instance Method Details

#beginning_of_dayObject



57
58
59
# File 'lib/belly_wash/nocturnal.rb', line 57

def beginning_of_day
  change(hour: 0, minute: 0)
end

#beginning_of_hourObject



47
48
49
# File 'lib/belly_wash/nocturnal.rb', line 47

def beginning_of_hour
  change(minute: 0)
end

#beginning_of_minuteObject



37
38
39
# File 'lib/belly_wash/nocturnal.rb', line 37

def beginning_of_minute
  change
end

#beginning_of_monthObject



87
88
89
# File 'lib/belly_wash/nocturnal.rb', line 87

def beginning_of_month
  change(day: 1, hour: 0, minute: 0)
end

#beginning_of_quarterObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/belly_wash/nocturnal.rb', line 97

def beginning_of_quarter
  first_quarter_month = @at.month - (2 + @at.month) % 3

  change(
    month: first_quarter_month,
    day: 1,
    hour: 0,
    minute: 0
  )
end

#beginning_of_weekObject



67
68
69
70
71
# File 'lib/belly_wash/nocturnal.rb', line 67

def beginning_of_week
  today = beginning_of_day

  (today.to_date - days_to_week_start).to_time
end

#beginning_of_yearObject



114
115
116
# File 'lib/belly_wash/nocturnal.rb', line 114

def beginning_of_year
  change(month: 1, day: 1, hour: 0, minute: 0)
end

#change(**fractions) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/belly_wash/nocturnal.rb', line 25

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
    @tz.utc_offset
  )
end

#days_to_week_startObject



79
80
81
82
83
84
85
# File 'lib/belly_wash/nocturnal.rb', line 79

def days_to_week_start
  start_day_number = DAYS_INTO_WEEK.fetch(
    BellyWash.config.beginning_of_week
  )

  (@at.wday - start_day_number) % 7
end

#next_dayObject



61
62
63
64
65
# File 'lib/belly_wash/nocturnal.rb', line 61

def next_day
  Nocturnal.new(
    beginning_of_day + 60 * 60 * 24
  ).beginning_of_day
end

#next_hourObject



51
52
53
54
55
# File 'lib/belly_wash/nocturnal.rb', line 51

def next_hour
  Nocturnal.new(
    beginning_of_hour + 60 * 60
  ).beginning_of_hour
end

#next_minuteObject



41
42
43
44
45
# File 'lib/belly_wash/nocturnal.rb', line 41

def next_minute
  Nocturnal.new(
    beginning_of_minute + 60
  ).beginning_of_minute
end

#next_monthObject



91
92
93
94
95
# File 'lib/belly_wash/nocturnal.rb', line 91

def next_month
  Nocturnal.new(
    beginning_of_month + 60 * 60 * 24 * 31
  ).beginning_of_month
end

#next_quarterObject



108
109
110
111
112
# File 'lib/belly_wash/nocturnal.rb', line 108

def next_quarter
  Nocturnal.new(
    beginning_of_quarter + 60 * 60 * 24 * 31 * 3
  ).beginning_of_quarter
end

#next_weekObject



73
74
75
76
77
# File 'lib/belly_wash/nocturnal.rb', line 73

def next_week
  Nocturnal.new(
    beginning_of_week + 60 * 60 * 24 * 7
  ).beginning_of_week
end

#next_yearObject



118
119
120
121
122
# File 'lib/belly_wash/nocturnal.rb', line 118

def next_year
  Nocturnal.new(
    beginning_of_year + 60 * 60 * 24 * 31 * 12
  ).beginning_of_year
end