Class: Trifle::Ruby::Nocturnal
- Inherits:
-
Object
- Object
- Trifle::Ruby::Nocturnal
- Defined in:
- lib/trifle/ruby/nocturnal.rb
Overview
rubocop:disable Metrics/ClassLength
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
Returns a new instance of Nocturnal.
23 24 25 26 |
# File 'lib/trifle/ruby/nocturnal.rb', line 23 def initialize(at, config: nil) @at = at @config = config end |
Class Method Details
.timeline(from:, to:, range:, config: nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/trifle/ruby/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 |
Instance Method Details
#change(**fractions) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/trifle/ruby/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 ⇒ Object
28 29 30 |
# File 'lib/trifle/ruby/nocturnal.rb', line 28 def config @config || Trifle::Ruby.default end |
#day ⇒ Object
66 67 68 |
# File 'lib/trifle/ruby/nocturnal.rb', line 66 def day change(hour: 0, minute: 0) end |
#days_to_week_start ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/trifle/ruby/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 ⇒ Object
55 56 57 |
# File 'lib/trifle/ruby/nocturnal.rb', line 55 def hour change(minute: 0) end |
#minute ⇒ Object
44 45 46 |
# File 'lib/trifle/ruby/nocturnal.rb', line 44 def minute change end |
#month ⇒ Object
98 99 100 |
# File 'lib/trifle/ruby/nocturnal.rb', line 98 def month change(day: 1, hour: 0, minute: 0) end |
#next_day ⇒ Object
70 71 72 73 74 75 |
# File 'lib/trifle/ruby/nocturnal.rb', line 70 def next_day Nocturnal.new( day + 60 * 60 * 24, config: config ).day end |
#next_hour ⇒ Object
59 60 61 62 63 64 |
# File 'lib/trifle/ruby/nocturnal.rb', line 59 def next_hour Nocturnal.new( hour + 60 * 60, config: config ).hour end |
#next_minute ⇒ Object
48 49 50 51 52 53 |
# File 'lib/trifle/ruby/nocturnal.rb', line 48 def next_minute Nocturnal.new( minute + 60, config: config ).minute end |
#next_month ⇒ Object
102 103 104 105 106 107 |
# File 'lib/trifle/ruby/nocturnal.rb', line 102 def next_month Nocturnal.new( month + 60 * 60 * 24 * 31, config: config ).month end |
#next_quarter ⇒ Object
120 121 122 123 124 125 |
# File 'lib/trifle/ruby/nocturnal.rb', line 120 def next_quarter Nocturnal.new( quarter + 60 * 60 * 24 * 31 * 3, config: config ).quarter end |
#next_week ⇒ Object
83 84 85 86 87 88 |
# File 'lib/trifle/ruby/nocturnal.rb', line 83 def next_week Nocturnal.new( week + 60 * 60 * 24 * 7, config: config ).week end |
#next_year ⇒ Object
131 132 133 134 135 136 |
# File 'lib/trifle/ruby/nocturnal.rb', line 131 def next_year Nocturnal.new( year + 60 * 60 * 24 * 31 * 12, config: config ).year end |
#quarter ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/trifle/ruby/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 ⇒ Object
77 78 79 80 81 |
# File 'lib/trifle/ruby/nocturnal.rb', line 77 def week today = day (today.to_date - days_to_week_start).to_time end |
#year ⇒ Object
127 128 129 |
# File 'lib/trifle/ruby/nocturnal.rb', line 127 def year change(month: 1, day: 1, hour: 0, minute: 0) end |