Class: DatePeriod::Month

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/date_period/month.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year: Date.current.year, month: Date.current.month) ⇒ Month

Returns a new instance of Month.



29
30
31
32
# File 'lib/date_period/month.rb', line 29

def initialize(year: Date.current.year, month: Date.current.month)
  @year = year
  @month = month
end

Instance Attribute Details

#monthObject (readonly)

Returns the value of attribute month.



27
28
29
# File 'lib/date_period/month.rb', line 27

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



27
28
29
# File 'lib/date_period/month.rb', line 27

def year
  @year
end

Class Method Details

.currentObject



13
14
15
# File 'lib/date_period/month.rb', line 13

def current
  including Date.current
end

.dump(month) ⇒ Object



22
23
24
# File 'lib/date_period/month.rb', line 22

def dump(month)
  JSON.dump("year" => month.year, "month" => month.month)
end

.including(datish) ⇒ Object



8
9
10
11
# File 'lib/date_period/month.rb', line 8

def including(datish)
  date = datish.to_date
  new year: date.year, month: date.month
end

.load(serialized_month) ⇒ Object



17
18
19
20
# File 'lib/date_period/month.rb', line 17

def load(serialized_month)
  data = JSON.load serialized_month
  new year: data["year"], month: data["month"]
end

Instance Method Details

#+(duration) ⇒ Object



47
48
49
# File 'lib/date_period/month.rb', line 47

def +(duration)
  self.class.including(first_day + Duration(duration))
end

#-(duration) ⇒ Object



51
52
53
# File 'lib/date_period/month.rb', line 51

def -(duration)
  self.class.including(first_day - Duration(duration))
end

#<=>(other) ⇒ Object



43
44
45
# File 'lib/date_period/month.rb', line 43

def <=>(other)
  [year, month] <=> [other.year, other.month]
end

#date_periodObject



55
56
57
# File 'lib/date_period/month.rb', line 55

def date_period
  first_day .. ((self + 1).first_day - 1.day)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/date_period/month.rb', line 63

def eql?(other)
  hash == other.hash
end

#first_dayObject



67
68
69
# File 'lib/date_period/month.rb', line 67

def first_day
  Date.new(year, month, 1)
end

#first_moment(zone: Time.zone) ⇒ Object



79
80
81
# File 'lib/date_period/month.rb', line 79

def first_moment(zone: Time.zone)
  Time.use_zone(zone){ first_day.beginning_of_day }
end

#include?(datish) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/date_period/month.rb', line 91

def include?(datish)
  datish.year == year && datish.month == month
end

#inspectObject



95
96
97
# File 'lib/date_period/month.rb', line 95

def inspect
  first_day.strftime "%b %Y"
end

#last_dayObject



71
72
73
# File 'lib/date_period/month.rb', line 71

def last_day
  first_day.end_of_month
end

#last_moment(zone: Time.zone) ⇒ Object



83
84
85
# File 'lib/date_period/month.rb', line 83

def last_moment(zone: Time.zone)
  Time.use_zone(zone){ last_day.end_of_day }
end

#prevObject



39
40
41
# File 'lib/date_period/month.rb', line 39

def prev
  self.class.new year: year, month: month - 1
end

#succObject Also known as: next



34
35
36
# File 'lib/date_period/month.rb', line 34

def succ
  self.class.new year: year, month: month + 1
end

#time_periodObject



87
88
89
# File 'lib/date_period/month.rb', line 87

def time_period
  first_moment .. last_moment
end

#to_dateObject



59
60
61
# File 'lib/date_period/month.rb', line 59

def to_date
  first_day
end