Class: Koyomi::Month

Inherits:
Period show all
Defined in:
lib/koyomi/month.rb

Instance Attribute Summary collapse

Attributes inherited from Period

#range

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(month = nil, year = nil) ⇒ Month

initialize instance.

Parameters:

  • month (Integer) (defaults to: nil)

    optional, default use the month of instance created.

  • year (Integer) (defaults to: nil)

    optional, default use the year of instance created.



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

def initialize(month = nil, year = nil)
  super()
  @year   = year||self.created_at.year
  @month  = month||self.created_at.month
  @first  = Date.new(self.year, self.month, 1)
  @last   = (first_date_of_next_month - 1)
  @range  = Range.new(self.first, self.last)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



198
199
200
# File 'lib/koyomi/month.rb', line 198

def method_missing(name, *args, &block)
  date("#{name}".sub(/^_/, "")) if "#{name}".match(/^_[0-9]+$/)
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



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

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



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

def last
  @last
end

#monthObject (readonly)

——————–# instance methods



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

def month
  @month
end

#yearObject (readonly)

——————–# instance methods



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

def year
  @year
end

Class Method Details

.of(date) ⇒ Koyomi::Month

create instance from date

Parameters:

Returns:



14
15
16
# File 'lib/koyomi/month.rb', line 14

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

Instance Method Details

#calendar(week_start = Koyomi::Week::DEFAULT_START) ⇒ Koyomi::Calendar

Create Koyomi::Calendar

Parameters:

  • week_start (Object) (defaults to: Koyomi::Week::DEFAULT_START)

Returns:



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

def calendar(week_start = Koyomi::Week::DEFAULT_START)
  Koyomi::Calendar.new(year, month, week_start)
end

#cycleKoyomi::Cycle

Create Koyomi::MonthCycle

Returns:

  • (Koyomi::Cycle)


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

def cycle
  Koyomi::MonthCycle.new(self)
end

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

week days of nth weeks.

Parameters:

  • nth (Integer|Array<Integer>)
  • wday_name (Object|Array<Object>)

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/koyomi/month.rb', line 71

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) ⇒ Date

date

Parameters:

  • days (Integer)

Returns:



133
134
135
136
137
138
139
# File 'lib/koyomi/month.rb', line 133

def date(day)
  begin
    date!(day)
  rescue ArgumentError => e
    raise Koyomi::WrongRangeError
  end
end

#date!(day) ⇒ Date

date

don't catch exceptions

Parameters:

  • days (Integer)

Returns:



146
147
148
# File 'lib/koyomi/month.rb', line 146

def date!(day)
  Date.new(self.year, self.month, "#{day}".to_i)
end

#nextKoyomi::Month

next month

Returns:



40
41
42
# File 'lib/koyomi/month.rb', line 40

def next
  self.class.of(self.last + 1)
end

#nth_wday(nth, wday_name) ⇒ Date

week day of nth week.

Parameters:

  • nth (Integer)
  • wday_name (Object)

Returns:



92
93
94
95
96
97
98
99
100
101
# File 'lib/koyomi/month.rb', line 92

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) ⇒ Date

week day of nth week.

Parameters:

  • nth (Integer)
  • wday_name (Object)

Returns:

Raises:



108
109
110
111
112
# File 'lib/koyomi/month.rb', line 108

def nth_wday!(nth, wday_name)
  date = nth_wday(nth, wday_name)
  raise Koyomi::WrongRangeError if date.month != self.month
  date
end

#prevKoyomi::Month

previous month

Returns:



47
48
49
# File 'lib/koyomi/month.rb', line 47

def prev
  self.class.of(self.first - 1)
end

#wdays(wday_name) ⇒ Array<Date>

week days

Parameters:

  • wday_name (Object)

Returns:



118
119
120
121
122
123
124
125
126
127
# File 'lib/koyomi/month.rb', line 118

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