Class: DateUtils::Month
Overview
Represents a ‘Month’
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
the initial / regular Date instance.
-
#first_day ⇒ Object
readonly
the first day of the Month -instance.
-
#last_day ⇒ Object
readonly
the last day of the Month -instance.
-
#month ⇒ Object
readonly
the Month -number.
-
#num_days ⇒ Object
readonly
the number of days in Month.
Instance Method Summary collapse
-
#days ⇒ Object
returns collection of days as Date -instances of self.
-
#initialize(date = nil) ⇒ Month
constructor
create a new Month of given Date.
-
#next ⇒ Object
returns new Month -instance one Month later than self.
-
#previous ⇒ Object
returns a new Month -instance one Month prior to self.
Methods included from Common
Constructor Details
#initialize(date = nil) ⇒ Month
create a new Month of given Date
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/date_utils.rb', line 152 def initialize(date=nil) date = Date.today if date.nil? if date.kind_of?(Date) @date = date @month = date.month @first_day = date.mday > 1 ? (date - ( date.mday - 1)) : date @num_days = 31 if [1,3,5,7,8,10,12].include?(@month) @num_days = 30 if [4,6,9,11].include?(@month) ( date.leap? ? (@num_days = 29) : (@num_days = 28) ) if @month == 2 @last_day = @first_day + ( @num_days - 1 ) else raise ArgumentError, "needs Date object as input!" end # returns new Month -instance one Month later than self # def next return Month.new(@last_day + 1) end # returns a new Month -instance one Month prior to self # def previous return Month.new((@first_day - 1).to_date) end # returns collection of days as Date -instances of self # def days arr = [] @first_day.upto(@last_day) { |date| arr << date } arr end end |
Instance Attribute Details
#date ⇒ Object (readonly)
the initial / regular Date instance
136 137 138 |
# File 'lib/date_utils.rb', line 136 def date @date end |
#first_day ⇒ Object (readonly)
the first day of the Month -instance
139 140 141 |
# File 'lib/date_utils.rb', line 139 def first_day @first_day end |
#last_day ⇒ Object (readonly)
the last day of the Month -instance
142 143 144 |
# File 'lib/date_utils.rb', line 142 def last_day @last_day end |
#month ⇒ Object (readonly)
the Month -number
145 146 147 |
# File 'lib/date_utils.rb', line 145 def month @month end |
#num_days ⇒ Object (readonly)
the number of days in Month
148 149 150 |
# File 'lib/date_utils.rb', line 148 def num_days @num_days end |
Instance Method Details
#days ⇒ Object
returns collection of days as Date -instances of self
180 181 182 183 184 |
# File 'lib/date_utils.rb', line 180 def days arr = [] @first_day.upto(@last_day) { |date| arr << date } arr end |