class Koyomi::Month
Attributes
Public Class Methods
a date next month of the date
@param [Date] date @return [Date]
# File lib/koyomi/month.rb, line 22 def self.a_date_of_next_month(date) date + 32 end
first date of next month
@param [Date] date @return [Date]
# File lib/koyomi/month.rb, line 30 def self.first_date_of_next_month(date) a_date = self.a_date_of_next_month(date) Date.new(a_date.year, a_date.month, 1) end
initialize instance.
@param [Integer] month optional, default use the month of instance created. @param [Integer] year optional, default use the year of instance created.
Koyomi::Period::new
# File lib/koyomi/month.rb, line 44 def initialize(month = nil, year = nil) super() @year = year||created_at.year @month = month||created_at.month @first = Date.new(@year, @month, 1) @last = (self.class.first_date_of_next_month(@first) - 1) end
create instance from date
@param [Date] date @return [Koyomi::Month]
# File lib/koyomi/month.rb, line 14 def self.of(date) self.new(date.month, date.year) end
Public Instance Methods
Create Koyomi::Calendar
@param [Object] week_start @return [Koyomi::Calendar]
# File lib/koyomi/month.rb, line 70 def calendar(week_start = Koyomi::Week::DEFAULT_START) Koyomi::Calendar.new(year, month, week_start) end
Create Koyomi::MonthCycle
@return [Koyomi::Cycle]
# File lib/koyomi/month.rb, line 77 def cycle Koyomi::MonthCycle.new(self) end
week days of nth weeks.
@param [Integer|Array<Integer>] nth @param [Object|Array<Object>] wday_name @return [Array<Date>]
# File lib/koyomi/month.rb, line 86 def cycles(weeks, wdays) _dates = [] cycle_weeks_filter(weeks).each do |n| [wdays].flatten.each do |w| begin a_date = self.nth_wday!(n, w) rescue Koyomi::WrongRangeError => e next else _dates << a_date end end end _dates.sort end
date
@param [Integer] days @return [Date]
# File lib/koyomi/month.rb, line 148 def date(day) begin date!(day) rescue ArgumentError => e raise Koyomi::WrongRangeError end end
date
don't catch exceptions
@param [Integer] days @return [Date]
# File lib/koyomi/month.rb, line 161 def date!(day) Date.new(self.year, self.month, "#{day}".to_i) end
next month
@return [Koyomi::Month]
# File lib/koyomi/month.rb, line 55 def next self.class.of(self.last + 1) end
week day of nth week.
@param [Integer] nth @param [Object] wday_name @return [Date]
# File lib/koyomi/month.rb, line 107 def nth_wday(nth, wday_name) case "#{nth}" when /first/ calc_nth_wday(1, wday_name) when /last/ self.next.nth_wday(1, wday_name) - Koyomi::Week::DAYS else calc_nth_wday(nth, wday_name) end end
week day of nth week.
@param [Integer] nth @param [Object] wday_name @return [Date]
# File lib/koyomi/month.rb, line 123 def nth_wday!(nth, wday_name) date = nth_wday(nth, wday_name) raise Koyomi::WrongRangeError if date.month != self.month date end
previous month
@return [Koyomi::Month]
# File lib/koyomi/month.rb, line 62 def prev self.class.of(self.first - 1) end
week days
@param [Object] wday_name @return [Array<Date>]
# File lib/koyomi/month.rb, line 133 def wdays(wday_name) _dates = [] a_date = self.nth_wday(1, wday_name) while (a_date.month == self.month) _dates << a_date a_date += Koyomi::Week::DAYS end _dates.sort end
Private Instance Methods
# File lib/koyomi/month.rb, line 191 def calc_nth_wday(nth, wday_name) a_date = self.first + ((nth - 1) * Koyomi::Week::DAYS) Koyomi::Week.new(a_date, a_date.wday).wday(wday_name) end
filter cycle weeks
@param [Object] weeks @return [Array|Range]
# File lib/koyomi/month.rb, line 172 def cycle_weeks_filter(weeks) case when weeks.to_s =~ /every/ _weeks = (1..max_week) else _weeks = [weeks].flatten end _weeks end
maximumn of weeks
@return [Integer]
# File lib/koyomi/month.rb, line 185 def max_week _weeks = (self.last.day / Koyomi::Week::DAYS).to_i _fraction = (self.last.day % Koyomi::Week::DAYS) (_weeks + (_fraction == 0 ? 0 : 1)) end
# File lib/koyomi/month.rb, line 196 def method_missing(name, *args, &block) date("#{name}".sub(/^_/, "")) if "#{name}".match(/^_[0-9]+$/) end