Class: Month
Overview
Synopsis
Representation of a month
Usage
m = Month.new(2009,2)
m = m + 1#
m.to_s -> "2009-03"
m1 = Month.new(2009,2)
m2 = Month.new(2009,12)
(m1..m2).to_a -> [<Month: 2009-02>, .. <Month: 2009-12>]
d = Date.new(2009,8,6)
d.to_month -> <Month: 2009-08>
Author
S Burkhard
Version
0.1
Instance Attribute Summary collapse
-
#month ⇒ Object
Returns the value of attribute month.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
-
#+(months) ⇒ Object
If months is a Numeric value, create a new Month object that is x months later than the current one.
-
#-(months) ⇒ Object
If months is a Numeric value, create a new Month object that is x months earlier than the current one.
-
#<=>(other) ⇒ Object
Compare this month with another month.
-
#first_cday ⇒ Object
Returns the first weekday (everyday except saturday or sunday) of the current month.
-
#first_cwday(wday) ⇒ Object
Returns the first commercial weekday with the given weekday number.
-
#first_day ⇒ Object
Returns the first day of current month as a Date object.
- #first_weekday ⇒ Object
-
#initialize(date_or_year, month = nil) ⇒ Month
constructor
Month.new(2009,2).
-
#last_cday ⇒ Object
Returns the last weekday (everyday except saturday or sunday) of the current month.
-
#last_day ⇒ Object
Returns the last day of current month as a Date object.
- #last_weekday ⇒ Object
-
#next ⇒ Object
Creates a new Month object that is 1 month later than the current one.
-
#previous ⇒ Object
Creates a new Month object that is 1 month earlier than the current one.
-
#strftime(string) ⇒ Object
Formats current month according to the directives in the given format string.
-
#succ ⇒ Object
alias to next.
-
#to_month ⇒ Object
Returns self.
-
#to_s(string = "%m/%y") ⇒ Object
alias to strftime.
- #weekdays ⇒ Object
Constructor Details
#initialize(date_or_year, month = nil) ⇒ Month
Month.new(2009,2)
Month.new(“2009”,“2”)
Month.new(Date.new(2009,2,3))
34 35 36 37 38 39 40 41 42 |
# File 'lib/month.rb', line 34 def initialize(date_or_year, month = nil) year = date_or_year if date_or_year.is_a? Date year = date_or_year.year month = date_or_year.month end @year = year.to_i @month = month.to_i end |
Instance Attribute Details
#month ⇒ Object
Returns the value of attribute month.
27 28 29 |
# File 'lib/month.rb', line 27 def month @month end |
#year ⇒ Object
Returns the value of attribute year.
27 28 29 |
# File 'lib/month.rb', line 27 def year @year end |
Instance Method Details
#+(months) ⇒ Object
If months is a Numeric value, create a new Month object that is x months later than the current one.
If months is not a Numeric, a TypeError is raised.
56 57 58 59 60 |
# File 'lib/month.rb', line 56 def +(months) raise TypeError, 'expected numeric' unless months.is_a?(Numeric) d = first_day >> months d.to_month end |
#-(months) ⇒ Object
If months is a Numeric value, create a new Month object that is x months earlier than the current one.
If _months_ is not a Numeric, a TypeError is raised.
47 48 49 50 51 |
# File 'lib/month.rb', line 47 def -(months) raise TypeError, 'expected numeric' unless months.is_a?(Numeric) d = first_day << months d.to_month end |
#<=>(other) ⇒ Object
Compare this month with another month.
89 90 91 |
# File 'lib/month.rb', line 89 def <=>(other) self.first_day <=> other.first_day end |
#first_cday ⇒ Object
Returns the first weekday (everyday except saturday or sunday) of the current month.
99 100 101 |
# File 'lib/month.rb', line 99 def first_cday first_weekday end |
#first_cwday(wday) ⇒ Object
Returns the first commercial weekday with the given weekday number
104 105 106 107 108 109 110 |
# File 'lib/month.rb', line 104 def first_cwday(wday) date = first_day while date.cwday != wday date = date += 1 end date end |
#first_day ⇒ Object
Returns the first day of current month as a Date object.
130 131 132 |
# File 'lib/month.rb', line 130 def first_day Date.new year, month, 1 end |
#first_weekday ⇒ Object
114 115 116 117 |
# File 'lib/month.rb', line 114 def first_weekday d = first_day Weekday.civil_or_newer(d.year, d.month, d.day) end |
#last_cday ⇒ Object
Returns the last weekday (everyday except saturday or sunday) of the current month.
94 95 96 |
# File 'lib/month.rb', line 94 def last_cday last_weekday end |
#last_day ⇒ Object
Returns the last day of current month as a Date object.
124 125 126 127 |
# File 'lib/month.rb', line 124 def last_day first_of_next_month = first_day + 32 Date.new(first_of_next_month.year, first_of_next_month.month, 1) - 1 # back off one day from first of that month end |
#last_weekday ⇒ Object
119 120 121 |
# File 'lib/month.rb', line 119 def last_weekday last_day.to_weekday end |
#next ⇒ Object
Creates a new Month object that is 1 month later than the current one
74 75 76 |
# File 'lib/month.rb', line 74 def next self + 1 end |
#previous ⇒ Object
Creates a new Month object that is 1 month earlier than the current one
79 80 81 |
# File 'lib/month.rb', line 79 def previous self - 1 end |
#strftime(string) ⇒ Object
Formats current month according to the directives in the given format string.
63 64 65 |
# File 'lib/month.rb', line 63 def strftime(string) first_day.strftime(string) end |
#succ ⇒ Object
alias to next
84 85 86 |
# File 'lib/month.rb', line 84 def succ self.next end |
#to_month ⇒ Object
Returns self
135 136 137 |
# File 'lib/month.rb', line 135 def to_month return self end |
#to_s(string = "%m/%y") ⇒ Object
alias to strftime. default format string: %Y-%m
69 70 71 |
# File 'lib/month.rb', line 69 def to_s(string = "%m/%y") strftime(string) end |
#weekdays ⇒ Object
112 |
# File 'lib/month.rb', line 112 def weekdays; (first_weekday..last_weekday).to_a; end |