class Koyomi::Calendar

Attributes

koyomi_month[RW]
month[RW]
week_start[R]
weeks[R]
year[RW]

Public Class Methods

new(year = nil, month = nil, week_start = Koyomi::Week::DEFAULT_START) click to toggle source

initialize instance

@param [Integer] year optional, use instance create date. @param [Integer] month optional, use instance create date. @param [Object] week_start weekday which week starts with. optional, use DEFAULT_WEEK_START.

Calls superclass method Koyomi::Period::new
# File lib/koyomi/calendar.rb, line 30
def initialize(year = nil, month = nil, week_start = Koyomi::Week::DEFAULT_START)
  _today = Date.today
  @year = year||_today.year
  @month = month||_today.month
  @koyomi_month = Koyomi::Month.new(@month, @year)
  
  _week = Koyomi::Week.new(@koyomi_month.first, @week_start)
  super(_week.first, _week.last)
  setup_week_start(week_start)
end
of(date, week_start = nil) click to toggle source

create Koyomi::Calendar instance from date.

@param [Date] date @param [Object] week_start @return [Koyomi::Calendar]

# File lib/koyomi/calendar.rb, line 15
def self.of(date, week_start = nil)
  self.new(date.year, date.month, week_start)
end

Public Instance Methods

cycles(weeks, wdays) click to toggle source

cycle dates

@param [Array<Integer>|Integer] weeks @param [Array<Object>|Object] wdays @return [Array<Date>]

# File lib/koyomi/calendar.rb, line 93
def cycles(weeks, wdays)
  _dates = []
  cycle_weeks_filter(weeks).each do |n|
    [wdays].flatten.each do |w|
      _dates << self.nth_wday(n, w)
    end
  end
  _dates.sort
end
first() click to toggle source

first date of the calendar (NOT first date of the MONTH)

@return [Date]

# File lib/koyomi/calendar.rb, line 44
def first
  Koyomi::Week.new(self.koyomi_month.first, self.week_start).first
end
last() click to toggle source

last date of the calendar (NOT last date of the MONTH)

@return [Date]

# File lib/koyomi/calendar.rb, line 51
def last
  Koyomi::Week.new(self.koyomi_month.last, self.week_start).last
end
nth_wday(nth, wday_name) click to toggle source

week day of nth week.

@param [Integer] nth @param [Object] wday_name @return [Date]

# File lib/koyomi/calendar.rb, line 75
def nth_wday(nth, wday_name)
  raise Koyomi::WrongRangeError if nth > weeks.size
  self.weeks[nth - 1].wday(wday_name)
end
the_month() click to toggle source

Koyomi::Month of the calendar's month.

@return [Koyomi::Month]

# File lib/koyomi/calendar.rb, line 66
def the_month
  self.koyomi_month
end
wdays(wday_name) click to toggle source

week days

@param [Object] wday_name @return [Array<Date>]

# File lib/koyomi/calendar.rb, line 84
def wdays(wday_name)
  self.weeks.collect { |w| w.wday(wday_name) }
end
week_start=(value) click to toggle source

set week_start

@param [Object] value

# File lib/koyomi/calendar.rb, line 58
def week_start=(value)
  setup_week_start(value)
  @week_start
end

Private Instance Methods

cycle_weeks_filter(weeks) click to toggle source

cycle weeks filter

@param [Object] weeks @return [Iterator] Array or Range

# File lib/koyomi/calendar.rb, line 138
def cycle_weeks_filter(weeks)
  case
  when weeks.to_s =~ /every/
    (1..self.weeks.size)
  else
    [weeks].flatten
  end
end
setup_week_start(value) click to toggle source

setup week start

@param [Object] value

# File lib/koyomi/calendar.rb, line 114
def setup_week_start(value)
  @week_start = self.class.windex(value)
  setup_weeks(@week_start)
end
setup_weeks(week_start) click to toggle source

setup weeks of the calendar.

@return [Array<Week>]

# File lib/koyomi/calendar.rb, line 122
def setup_weeks(week_start)
  a_date = self.first
  the_last = self.last
  @weeks = []

  while (a_date < the_last)
    @weeks << Koyomi::Week.new(a_date, week_start)
    a_date += WEEK_DAYS
  end
  @weeks
end