Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/thefox-ext/ext/date.rb

Instance Method Summary collapse

Instance Method Details

#today?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/thefox-ext/ext/date.rb', line 5

def today?
  self == Date.today
end

#weekObject

Get all days (as Date objects) for the current week.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/thefox-ext/ext/date.rb', line 10

def week
  cweek = self.cweek
  year = self.year
  month = self.month

  next_year = year + 1
  previous_year = year - 1

  days = Date.new(year)
    .step(Date.new(year, -1, -1))
    .select{ |d| d.cweek == cweek }

  if cweek == 1 && month == 12 ||
    cweek == 1 && month == 1 ||
    cweek >= 52 && month == 12 ||
    cweek >= 52 && month == 1

    days.keep_if{ |d| d.year == year && d.month == month }
  end

  if days.count < 7
    rest = 7 - days.count

    rest_days = nil
    if month == 1
      rest_days = Date.new(previous_year, 12, 31 - rest + 1).step(Date.new(previous_year, 12, 31))
    elsif month == 12
      rest_days = Date.new(next_year).step(Date.new(next_year, 1, rest))
    end

    if !rest_days.nil?
      days += rest_days.to_a
    end
  end

  days.sort[0, 7]
end