Class: DateUtils::Month

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

Overview

Represents a ‘Month’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date = nil) ⇒ Month

create a new Month of given Date



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/date_utils.rb', line 120

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



104
105
106
# File 'lib/date_utils.rb', line 104

def date
  @date
end

#first_dayObject (readonly)

the first day of the Month -instance



107
108
109
# File 'lib/date_utils.rb', line 107

def first_day
  @first_day
end

#last_dayObject (readonly)

the last day of the Month -instance



110
111
112
# File 'lib/date_utils.rb', line 110

def last_day
  @last_day
end

#monthObject (readonly)

the Month -number



113
114
115
# File 'lib/date_utils.rb', line 113

def month
  @month
end

#num_daysObject (readonly)

the number of days in Month



116
117
118
# File 'lib/date_utils.rb', line 116

def num_days
  @num_days
end

Instance Method Details

#daysObject

returns collection of days as Date -instances of self



148
149
150
151
152
# File 'lib/date_utils.rb', line 148

def days
  arr = []
  @first_day.upto(@last_day) { |date| arr << date }
  arr
end

#nextObject

returns new Month -instance one Month later than self



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

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

#previousObject

returns a new Month -instance one Month prior to self



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

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