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
49 50 51 |
# File 'lib/monthify/month.rb', line 49 def initialize(year, month) @year, @month = year, month end |
Instance Attribute Details
#month ⇒ Object (readonly)
Returns the value of attribute month.
7 8 9 |
# File 'lib/monthify/month.rb', line 7 def month @month end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
7 8 9 |
# File 'lib/monthify/month.rb', line 7 def year @year end |
Class Method Details
.containing(datish) ⇒ Month
Returns the month containing the given date.
27 28 29 |
# File 'lib/monthify/month.rb', line 27 def self.containing(datish) Month.new(datish.year, datish.month) end |
.current ⇒ Month
Returns the current month.
10 11 12 13 |
# File 'lib/monthify/month.rb', line 10 def self.current today = Date.current new(today.year, today.month) end |
.dump(month) ⇒ String
Used by ActiveRecord::Base.serialize
42 43 44 |
# File 'lib/monthify/month.rb', line 42 def self.dump(month) YAML.dump(month.first_day) end |
.load(date_yaml) ⇒ Month
Used by ActiveRecord::Base.serialize
34 35 36 37 |
# File 'lib/monthify/month.rb', line 34 def self.load(date_yaml) date = YAML.load(date_yaml) Month.containing(date) end |
.next ⇒ Month
Returns the month after the current month.
16 17 18 |
# File 'lib/monthify/month.rb', line 16 def self.next current.next end |
.previous ⇒ Month
Returns the month before the current month.
21 22 23 |
# File 'lib/monthify/month.rb', line 21 def self.previous current.previous end |
Instance Method Details
#+(duration) ⇒ Month
Adds the duration to the month
104 105 106 |
# File 'lib/monthify/month.rb', line 104 def +(duration) Month.containing(first_day + duration) end |
#-(duration) ⇒ Month
Subtracts the duration from the month
111 112 113 |
# File 'lib/monthify/month.rb', line 111 def -(duration) Month.containing(first_day - duration) end |
#contains?(datish) ⇒ Boolean
Returns true if the month contains the date, false otherwise.
96 97 98 99 |
# File 'lib/monthify/month.rb', line 96 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.
85 86 87 |
# File 'lib/monthify/month.rb', line 85 def date_range Range.new(first_day, last_day) end |
#first_day ⇒ Date
Returns the first day of the month.
54 55 56 |
# File 'lib/monthify/month.rb', line 54 def first_day Date.new(year, month, 1) end |
#first_moment ⇒ Time
Returns the very beginning of the month.
64 65 66 |
# File 'lib/monthify/month.rb', line 64 def first_moment first_day.beginning_of_day end |
#last_day ⇒ Date
Returns the last day of the month.
59 60 61 |
# File 'lib/monthify/month.rb', line 59 def last_day first_day.end_of_month end |
#last_moment ⇒ Time
Returns the very end of the month.
69 70 71 |
# File 'lib/monthify/month.rb', line 69 def last_moment last_day.end_of_day end |
#next ⇒ Month Also known as: succ
Returns the month following this one.
79 80 81 |
# File 'lib/monthify/month.rb', line 79 def next self + 1.month end |
#previous ⇒ Month
Returns the month preceeding this one.
74 75 76 |
# File 'lib/monthify/month.rb', line 74 def previous self - 1.month end |
#time_range ⇒ Range<Time, Time>
Returns the range of time in this month.
90 91 92 |
# File 'lib/monthify/month.rb', line 90 def time_range Range.new(first_moment, last_moment) end |