Class: Koyomi::Calendar

Inherits:
Period
  • Object
show all
Includes:
Helper::Week
Defined in:
lib/koyomi/calendar.rb

Constant Summary

Constants included from Helper::Week

Helper::Week::DEFAULT_WEEK_START, Helper::Week::WEEK_DAYS, Helper::Week::WEEK_START_RANGE, Helper::Week::WEEK_WDAYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::Week

included

Constructor Details

#initialize(year = nil, month = nil, week_start = nil) ⇒ Calendar

initialize instance

Parameters:

  • year (Integer) (defaults to: nil)

    optional, use instance create date.

  • month (Integer) (defaults to: nil)

    optional, use instance create date.

  • week_start (Object) (defaults to: nil)

    weekday which week starts with. optional, use DEFAULT_WEEK_START.



29
30
31
32
33
34
35
36
# File 'lib/koyomi/calendar.rb', line 29

def initialize(year = nil, month = nil, week_start = nil)
  super()
  self.year = year||self.created_at.year
  self.month = month||self.created_at.month
  self.week_start = week_start||DEFAULT_WEEK_START
  
  self.koyomi_month = Koyomi::Month.new(self.month, self.year)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Koyomi::Period

Instance Attribute Details

#koyomi_monthObject

——————–# instance methods



21
22
23
# File 'lib/koyomi/calendar.rb', line 21

def koyomi_month
  @koyomi_month
end

#monthObject

——————–# instance methods



21
22
23
# File 'lib/koyomi/calendar.rb', line 21

def month
  @month
end

#week_startObject

Returns the value of attribute week_start.



22
23
24
# File 'lib/koyomi/calendar.rb', line 22

def week_start
  @week_start
end

#yearObject

——————–# instance methods



21
22
23
# File 'lib/koyomi/calendar.rb', line 21

def year
  @year
end

Class Method Details

.of(date, week_start = nil) ⇒ Koyomi::Calendar

create Koyomi::Calendar instance from date.

Parameters:

  • date (Date)
  • week_start (Object) (defaults to: nil)

Returns:



15
16
17
# File 'lib/koyomi/calendar.rb', line 15

def self.of(date, week_start = nil)
  self.new(date.year, date.month, week_start)
end

Instance Method Details

#cycles(weeks, wdays) ⇒ Array<Date>

cycle dates

Parameters:

  • weeks (Array<Integer>|Integer)
  • wdays (Array<Object>|Object)

Returns:



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

def cycles(weeks, wdays)
  self.koyomi_month.cycles(weeks, wdays)
end

#firstDate

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

Returns:



48
49
50
# File 'lib/koyomi/calendar.rb', line 48

def first
  Koyomi::Week.new(self.koyomi_month.first, self.week_start).first
end

#lastDate

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

Returns:



55
56
57
# File 'lib/koyomi/calendar.rb', line 55

def last
  Koyomi::Week.new(self.koyomi_month.last, self.week_start).last
end

#nth_wday(nth, wday_name) ⇒ Date

week day of nth week.

Parameters:

  • nth (Integer)
  • wday_name (Object)

Returns:



93
94
95
# File 'lib/koyomi/calendar.rb', line 93

def nth_wday(nth, wday_name)
  self.koyomi_month.nth_wday(nth, wday_name)
end

#rangeRange

range of the calendar.

Returns:

  • (Range)


62
63
64
# File 'lib/koyomi/calendar.rb', line 62

def range
  Range.new(self.first, self.last)
end

#the_monthKoyomi::Month

Koyomi::Month of the calendar’s month.

Returns:



69
70
71
# File 'lib/koyomi/calendar.rb', line 69

def the_month
  self.koyomi_month
end

#wdays(wday_name) ⇒ Array<Date>

week days

Parameters:

  • wday_name (Object)

Returns:



101
102
103
104
105
106
107
108
109
# File 'lib/koyomi/calendar.rb', line 101

def wdays(wday_name)
  _wdays = []
  a_date = self.nth_wday(1, wday_name)
  while ((a_date.month == self.month))
    _wdays << a_date
    a_date += WEEK_DAYS
  end
  _wdays
end

#weeksArray<Week>

weeks of the calendar.

Returns:



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/koyomi/calendar.rb', line 76

def weeks
  a_date = self.first
  the_last = self.last
  weeks = []
  
  while (a_date < the_last)
    weeks << Koyomi::Week.new(a_date, self.week_start)
    a_date += WEEK_DAYS
  end
  weeks
end