Class: DatePeriod::Month
- Inherits:
-
Object
- Object
- DatePeriod::Month
- Includes:
- Comparable
- Defined in:
- lib/date_period/month.rb
Instance Attribute Summary collapse
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
- .current ⇒ Object
- .dump(month) ⇒ Object
- .including(datish) ⇒ Object
- .load(serialized_month) ⇒ Object
Instance Method Summary collapse
- #+(duration) ⇒ Object
- #-(duration) ⇒ Object
- #<=>(other) ⇒ Object
- #date_period ⇒ Object
- #eql?(other) ⇒ Boolean
- #first_day ⇒ Object
- #first_moment(zone: Time.zone) ⇒ Object
- #include?(datish) ⇒ Boolean
-
#initialize(year: Date.current.year, month: Date.current.month) ⇒ Month
constructor
A new instance of Month.
- #inspect ⇒ Object
- #last_day ⇒ Object
- #last_moment(zone: Time.zone) ⇒ Object
- #prev ⇒ Object
- #succ ⇒ Object (also: #next)
- #time_period ⇒ Object
- #to_date ⇒ Object
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
#month ⇒ Object (readonly)
Returns the value of attribute month.
27 28 29 |
# File 'lib/date_period/month.rb', line 27 def month @month end |
#year ⇒ Object (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
.current ⇒ Object
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_period ⇒ Object
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
63 64 65 |
# File 'lib/date_period/month.rb', line 63 def eql?(other) hash == other.hash end |
#first_day ⇒ Object
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
91 92 93 |
# File 'lib/date_period/month.rb', line 91 def include?(datish) datish.year == year && datish.month == month end |
#inspect ⇒ Object
95 96 97 |
# File 'lib/date_period/month.rb', line 95 def inspect first_day.strftime "%b %Y" end |
#last_day ⇒ Object
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 |
#prev ⇒ Object
39 40 41 |
# File 'lib/date_period/month.rb', line 39 def prev self.class.new year: year, month: month - 1 end |
#succ ⇒ Object 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_period ⇒ Object
87 88 89 |
# File 'lib/date_period/month.rb', line 87 def time_period first_moment .. last_moment end |
#to_date ⇒ Object
59 60 61 |
# File 'lib/date_period/month.rb', line 59 def to_date first_day end |