Class: DateUtils::Month

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/date_utils.rb

Overview

Represents a ‘Month’

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#include?

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

#dateObject (readonly)

the initial / regular Date instance



136
137
138
# File 'lib/date_utils.rb', line 136

def date
  @date
end

#first_dayObject (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_dayObject (readonly)

the last day of the Month -instance



142
143
144
# File 'lib/date_utils.rb', line 142

def last_day
  @last_day
end

#monthObject (readonly)

the Month -number



145
146
147
# File 'lib/date_utils.rb', line 145

def month
  @month
end

#num_daysObject (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

#daysObject

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

#nextObject

returns new Month -instance one Month later than self



168
169
170
# File 'lib/date_utils.rb', line 168

def next
  return Month.new(@last_day + 1)
end

#previousObject

returns a new Month -instance one Month prior to self



174
175
176
# File 'lib/date_utils.rb', line 174

def previous
  return Month.new((@first_day - 1).to_date)
end