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.



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

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.koyomi_month = Koyomi::Month.new(self.month, self.year)
  self.week_start = week_start||DEFAULT_WEEK_START
end

Dynamic Method Handling

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

Instance Attribute Details

#koyomi_monthObject

Returns the value of attribute koyomi_month.



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

def koyomi_month
  @koyomi_month
end

#monthObject

Returns the value of attribute month.



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

def month
  @month
end

#week_startObject

——————–# instance methods



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

def week_start
  @week_start
end

#weeksObject (readonly)

Returns the value of attribute weeks.



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

def weeks
  @weeks
end

#yearObject

Returns the value of attribute year.



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

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:



96
97
98
99
100
101
102
103
104
# File 'lib/koyomi/calendar.rb', line 96

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

#firstDate

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

Returns:



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

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:



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

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:



79
80
81
# File 'lib/koyomi/calendar.rb', line 79

def nth_wday(nth, wday_name)
  self.weeks[nth - 1].wday(wday_name)
end

#rangeRange

range of the calendar.

Returns:

  • (Range)


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

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

#the_monthKoyomi::Month

Koyomi::Month of the calendar’s month.

Returns:



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

def the_month
  self.koyomi_month
end

#wdays(wday_name) ⇒ Array<Date>

week days

Parameters:

  • wday_name (Object)

Returns:



87
88
89
# File 'lib/koyomi/calendar.rb', line 87

def wdays(wday_name)
  self.weeks.collect { |w| w.wday(wday_name) }
end