class Koyomi::Month

Attributes

month[R]
year[R]

Public Class Methods

a_date_of_next_month(date) click to toggle source

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(date) click to toggle source

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
new(month = nil, year = nil) click to toggle source

initialize instance.

@param [Integer] month optional, default use the month of instance created. @param [Integer] year optional, default use the year of instance created.

Calls superclass method 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
of(date) click to toggle source

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

calendar(week_start = Koyomi::Week::DEFAULT_START) click to toggle source

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
cycle() click to toggle source

Create Koyomi::MonthCycle

@return [Koyomi::Cycle]

# File lib/koyomi/month.rb, line 77
def cycle
  Koyomi::MonthCycle.new(self)
end
cycles(weeks, wdays) click to toggle source

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(day) click to toggle source

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!(day) click to toggle source

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() click to toggle source

next month

@return [Koyomi::Month]

# File lib/koyomi/month.rb, line 55
def next
  self.class.of(self.last + 1)
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/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
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/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
prev() click to toggle source

previous month

@return [Koyomi::Month]

# File lib/koyomi/month.rb, line 62
def prev
  self.class.of(self.first - 1)
end
wdays(wday_name) click to toggle source

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

calc_nth_wday(nth, wday_name) click to toggle source
# 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
cycle_weeks_filter(weeks) click to toggle source

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
max_week() click to toggle source

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
method_missing(name, *args, &block) click to toggle source
# File lib/koyomi/month.rb, line 196
def method_missing(name, *args, &block)
  date("#{name}".sub(/^_/, "")) if "#{name}".match(/^_[0-9]+$/)
end