Class: Koyomi::Month
Instance Attribute Summary collapse
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#month ⇒ Object
readonly
——————–# instance methods.
-
#year ⇒ Object
readonly
——————–# instance methods.
Attributes inherited from Period
Class Method Summary collapse
-
.of(date) ⇒ Koyomi::Month
create instance from date.
Instance Method Summary collapse
-
#calendar(week_start = Koyomi::Week::DEFAULT_START) ⇒ Koyomi::Calendar
Create Koyomi::Calendar.
-
#cycle ⇒ Koyomi::Cycle
Create Koyomi::MonthCycle.
-
#cycles(weeks, wdays) ⇒ Array<Date>
week days of nth weeks.
-
#date(day) ⇒ Date
date.
-
#date!(day) ⇒ Date
date don’t catch exceptions.
-
#initialize(month = nil, year = nil) ⇒ Month
constructor
initialize instance.
-
#next ⇒ Koyomi::Month
next month.
-
#nth_wday(nth, wday_name) ⇒ Date
week day of nth week.
-
#nth_wday!(nth, wday_name) ⇒ Date
week day of nth week.
-
#prev ⇒ Koyomi::Month
previous month.
-
#wdays(wday_name) ⇒ Array<Date>
week days.
Constructor Details
#initialize(month = nil, year = nil) ⇒ Month
initialize instance.
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
#first ⇒ Object (readonly)
Returns the value of attribute first.
22 23 24 |
# File 'lib/koyomi/month.rb', line 22 def first @first end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
22 23 24 |
# File 'lib/koyomi/month.rb', line 22 def last @last end |
#month ⇒ Object (readonly)
——————–# instance methods
21 22 23 |
# File 'lib/koyomi/month.rb', line 21 def month @month end |
#year ⇒ Object (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
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
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 |
#cycle ⇒ Koyomi::Cycle
Create Koyomi::MonthCycle
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.
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
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
146 147 148 |
# File 'lib/koyomi/month.rb', line 146 def date!(day) Date.new(self.year, self.month, "#{day}".to_i) end |
#next ⇒ Koyomi::Month
next month
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.
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.
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 |
#prev ⇒ Koyomi::Month
previous month
47 48 49 |
# File 'lib/koyomi/month.rb', line 47 def prev self.class.of(self.first - 1) end |