Class: Trifle::Stats::Nocturnal
- Inherits:
-
Object
- Object
- Trifle::Stats::Nocturnal
- Defined in:
- lib/trifle/stats/nocturnal.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: Key
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
- #change(**fractions) ⇒ Object
- #config ⇒ Object
- #day ⇒ Object
- #days_to_week_start ⇒ Object
- #hour ⇒ Object
-
#initialize(at, config: nil) ⇒ Nocturnal
constructor
A new instance of Nocturnal.
- #minute ⇒ Object
- #month ⇒ Object
- #next_day ⇒ Object
- #next_hour ⇒ Object
- #next_minute ⇒ Object
- #next_month ⇒ Object
- #next_quarter ⇒ Object
- #next_week ⇒ Object
- #next_year ⇒ Object
- #quarter ⇒ Object
- #week ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(at, config: nil) ⇒ Nocturnal
47 48 49 50 |
# File 'lib/trifle/stats/nocturnal.rb', line 47 def initialize(at, config: nil) @at = at @config = config end |
Class Method Details
.timeline(from:, to:, range:, config: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/trifle/stats/nocturnal.rb', line 35 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 |
Instance Method Details
#change(**fractions) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/trifle/stats/nocturnal.rb', line 56 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 ⇒ Object
52 53 54 |
# File 'lib/trifle/stats/nocturnal.rb', line 52 def config @config || Trifle::Stats.default end |
#day ⇒ Object
90 91 92 |
# File 'lib/trifle/stats/nocturnal.rb', line 90 def day change(hour: 0, minute: 0) end |
#days_to_week_start ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/trifle/stats/nocturnal.rb', line 114 def days_to_week_start start_day_number = DAYS_INTO_WEEK.fetch( config.beginning_of_week ) (@at.wday - start_day_number) % 7 end |
#hour ⇒ Object
79 80 81 |
# File 'lib/trifle/stats/nocturnal.rb', line 79 def hour change(minute: 0) end |
#minute ⇒ Object
68 69 70 |
# File 'lib/trifle/stats/nocturnal.rb', line 68 def minute change end |
#month ⇒ Object
122 123 124 |
# File 'lib/trifle/stats/nocturnal.rb', line 122 def month change(day: 1, hour: 0, minute: 0) end |
#next_day ⇒ Object
94 95 96 97 98 99 |
# File 'lib/trifle/stats/nocturnal.rb', line 94 def next_day Nocturnal.new( day + 60 * 60 * 24, config: config ).day end |
#next_hour ⇒ Object
83 84 85 86 87 88 |
# File 'lib/trifle/stats/nocturnal.rb', line 83 def next_hour Nocturnal.new( hour + 60 * 60, config: config ).hour end |
#next_minute ⇒ Object
72 73 74 75 76 77 |
# File 'lib/trifle/stats/nocturnal.rb', line 72 def next_minute Nocturnal.new( minute + 60, config: config ).minute end |
#next_month ⇒ Object
126 127 128 129 130 131 |
# File 'lib/trifle/stats/nocturnal.rb', line 126 def next_month Nocturnal.new( month + 60 * 60 * 24 * 31, config: config ).month end |
#next_quarter ⇒ Object
144 145 146 147 148 149 |
# File 'lib/trifle/stats/nocturnal.rb', line 144 def next_quarter Nocturnal.new( quarter + 60 * 60 * 24 * 31 * 3, config: config ).quarter end |
#next_week ⇒ Object
107 108 109 110 111 112 |
# File 'lib/trifle/stats/nocturnal.rb', line 107 def next_week Nocturnal.new( week + 60 * 60 * 24 * 7, config: config ).week end |
#next_year ⇒ Object
155 156 157 158 159 160 |
# File 'lib/trifle/stats/nocturnal.rb', line 155 def next_year Nocturnal.new( year + 60 * 60 * 24 * 31 * 12, config: config ).year end |
#quarter ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/trifle/stats/nocturnal.rb', line 133 def quarter first_quarter_month = @at.month - (2 + @at.month) % 3 change( month: first_quarter_month, day: 1, hour: 0, minute: 0 ) end |
#week ⇒ Object
101 102 103 104 105 |
# File 'lib/trifle/stats/nocturnal.rb', line 101 def week today = day (today.to_date - days_to_week_start).to_time end |
#year ⇒ Object
151 152 153 |
# File 'lib/trifle/stats/nocturnal.rb', line 151 def year change(month: 1, day: 1, hour: 0, minute: 0) end |