Class: DateUtils::Month

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

Overview

Represents a ‘Month’

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

extract_options_from_args!, #include?

Constructor Details

#initialize(val = nil) ⇒ Month

create a new Month of given Date



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/date_utils.rb', line 183

def initialize(val=nil)
  if val.nil?
    _date = Date.today
  else
    if val.is_a?(Date) 
      _date = val
    elsif val.is_a?(Fixnum) && val <= 12
      _date = Date::civil(Date.today.year.to_i,val,1)
    else
      raise ArgumentError.new("neither Fixnum nor Date given.")
    end
  end 
  @date = _date
  create_instance_variables
end

Instance Attribute Details

#dateObject (readonly)

the initial / regular Date instance



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

def date
  @date
end

#first_dayObject (readonly)

the first day of the Month -instance



170
171
172
# File 'lib/date_utils.rb', line 170

def first_day
  @first_day
end

#last_dayObject (readonly)

the last day of the Month -instance



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

def last_day
  @last_day
end

#monthObject (readonly)

the Month -number



176
177
178
# File 'lib/date_utils.rb', line 176

def month
  @month
end

#num_daysObject (readonly)

the number of days in Month



179
180
181
# File 'lib/date_utils.rb', line 179

def num_days
  @num_days
end

Class Method Details

.create(*args) ⇒ Object

create a Month-instance call with hash: year and month :call-seq: Month.create(:year => x, :month => y) Month.create(:month => x) Month.create(:year => x)



206
207
208
209
210
211
# File 'lib/date_utils.rb', line 206

def self.create(*args)
  options = Common::extract_options_from_args!(args)
  int_year = options.has_key?(:year) && options[:year].is_a?(Fixnum) ? options[:year] : nil
  int_month = options.has_key?(:month) && options[:month].is_a?(Fixnum) && options[:month] <= 12 ? options[:month] : nil
  return Month.new(Date::civil(int_year || Date.today.year, int_month || 1))
end

Instance Method Details

#daysObject

returns collection of days as Date -instances of self



227
228
229
230
231
# File 'lib/date_utils.rb', line 227

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

#nextObject

returns new Month -instance one Month later than self



215
216
217
# File 'lib/date_utils.rb', line 215

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

#previousObject

returns a new Month -instance one Month prior to self



221
222
223
# File 'lib/date_utils.rb', line 221

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