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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/date_utils.rb', line 192

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



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

def date
  @date
end

#first_dayObject (readonly)

the first day of the Month -instance



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

def first_day
  @first_day
end

#last_dayObject (readonly)

the last day of the Month -instance



182
183
184
# File 'lib/date_utils.rb', line 182

def last_day
  @last_day
end

#monthObject (readonly)

the Month -number



185
186
187
# File 'lib/date_utils.rb', line 185

def month
  @month
end

#num_daysObject (readonly)

the number of days in Month



188
189
190
# File 'lib/date_utils.rb', line 188

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)



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

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



236
237
238
239
240
# File 'lib/date_utils.rb', line 236

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

#nextObject

returns new Month -instance one Month later than self



224
225
226
# File 'lib/date_utils.rb', line 224

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

#previousObject

returns a new Month -instance one Month prior to self



230
231
232
# File 'lib/date_utils.rb', line 230

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