Class: Monthify::Month
- Includes:
- Comparable
- Defined in:
- lib/monthify/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
-
.containing(datish) ⇒ Month
The month containing the given date.
-
.current ⇒ Month
The current month.
-
.dump(month) ⇒ String
Used by ActiveRecord::Base.serialize.
-
.load(date_yaml) ⇒ Month
Used by ActiveRecord::Base.serialize.
-
.next ⇒ Month
The month after the current month.
-
.previous ⇒ Month
The month before the current month.
Instance Method Summary collapse
-
#+(duration) ⇒ Month
Adds the duration to the month.
-
#-(duration) ⇒ Month
Subtracts the duration from the month.
-
#contains?(datish) ⇒ Boolean
True if the month contains the date, false otherwise.
-
#date_range ⇒ Range<Date, Date>
The range of dates in this month.
-
#first_day ⇒ Date
The first day of the month.
-
#first_moment ⇒ Time
The very beginning of the month.
- #initialize(year, month) ⇒ Month constructor
-
#last_day ⇒ Date
The last day of the month.
-
#last_moment ⇒ Time
The very end of the month.
-
#next ⇒ Month
(also: #succ)
The month following this one.
-
#previous ⇒ Month
The month preceeding this one.
-
#time_range ⇒ Range<Time, Time>
The range of time in this month.
Constructor Details
#initialize(year, month) ⇒ Month
51 52 53 |
# File 'lib/monthify/month.rb', line 51 def initialize(year, month) @year, @month = year, month end |
Instance Attribute Details
#month ⇒ Object (readonly)
Returns the value of attribute month.
9 10 11 |
# File 'lib/monthify/month.rb', line 9 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
9 10 11 |
# File 'lib/monthify/month.rb', line 9 def year @year end |
Class Method Details
.containing(datish) ⇒ Month
Returns the month containing the given date.
29 30 31 |
# File 'lib/monthify/month.rb', line 29 def self.containing(datish) Month.new(datish.year, datish.month) end |
.current ⇒ Month
Returns the current month.
12 13 14 15 |
# File 'lib/monthify/month.rb', line 12 def self.current today = Date.current new(today.year, today.month) end |
.dump(month) ⇒ String
Used by ActiveRecord::Base.serialize
44 45 46 |
# File 'lib/monthify/month.rb', line 44 def self.dump(month) YAML.dump(month.first_day) end |
.load(date_yaml) ⇒ Month
Used by ActiveRecord::Base.serialize
36 37 38 39 |
# File 'lib/monthify/month.rb', line 36 def self.load(date_yaml) date = YAML.load(date_yaml) Month.containing(date) end |
.next ⇒ Month
Returns the month after the current month.
18 19 20 |
# File 'lib/monthify/month.rb', line 18 def self.next current.next end |
.previous ⇒ Month
Returns the month before the current month.
23 24 25 |
# File 'lib/monthify/month.rb', line 23 def self.previous current.previous end |
Instance Method Details
#+(duration) ⇒ Month
Adds the duration to the month
106 107 108 |
# File 'lib/monthify/month.rb', line 106 def +(duration) Month.containing(first_day + duration) end |
#-(duration) ⇒ Month
Subtracts the duration from the month
113 114 115 |
# File 'lib/monthify/month.rb', line 113 def -(duration) Month.containing(first_day - duration) end |
#contains?(datish) ⇒ Boolean
Returns true if the month contains the date, false otherwise.
98 99 100 101 |
# File 'lib/monthify/month.rb', line 98 def contains?(datish) date = datish.to_date year == date.year && month == date.month end |
#date_range ⇒ Range<Date, Date>
Returns the range of dates in this month.
87 88 89 |
# File 'lib/monthify/month.rb', line 87 def date_range Range.new(first_day, last_day) end |
#first_day ⇒ Date
Returns the first day of the month.
56 57 58 |
# File 'lib/monthify/month.rb', line 56 def first_day Date.new(year, month, 1) end |
#first_moment ⇒ Time
Returns the very beginning of the month.
66 67 68 |
# File 'lib/monthify/month.rb', line 66 def first_moment first_day.beginning_of_day end |
#last_day ⇒ Date
Returns the last day of the month.
61 62 63 |
# File 'lib/monthify/month.rb', line 61 def last_day first_day.end_of_month end |
#last_moment ⇒ Time
Returns the very end of the month.
71 72 73 |
# File 'lib/monthify/month.rb', line 71 def last_moment last_day.end_of_day end |
#next ⇒ Month Also known as: succ
Returns the month following this one.
81 82 83 |
# File 'lib/monthify/month.rb', line 81 def next self + 1.month end |
#previous ⇒ Month
Returns the month preceeding this one.
76 77 78 |
# File 'lib/monthify/month.rb', line 76 def previous self - 1.month end |
#time_range ⇒ Range<Time, Time>
Returns the range of time in this month.
92 93 94 |
# File 'lib/monthify/month.rb', line 92 def time_range Range.new(first_moment, last_moment) end |