Class: CalendarDays
- Inherits:
-
Object
- Object
- CalendarDays
- Includes:
- NetCache
- Defined in:
- lib/calendar_days/calendar_days.rb,
lib/calendar_days/version.rb,
lib/calendar_days/calendar_days.rb,
lib/calendar_days/calendar_days.rb,
lib/calendar_days/calendar_days.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#ics_events ⇒ Object
readonly
Returns the value of attribute ics_events.
-
#since_day ⇒ Object
readonly
Returns the value of attribute since_day.
-
#since_month ⇒ Object
readonly
Returns the value of attribute since_month.
-
#since_year ⇒ Object
readonly
Returns the value of attribute since_year.
-
#until_day ⇒ Object
readonly
Returns the value of attribute until_day.
-
#until_month ⇒ Object
readonly
Returns the value of attribute until_month.
-
#until_year ⇒ Object
readonly
Returns the value of attribute until_year.
Instance Method Summary collapse
- #__ics_end ⇒ Object
- #__ics_start ⇒ Object
-
#__weekday_list(dt_since = self.since, dt_until = self.until) ⇒ Object
get the weekdays from the specified date range except saturday, sunday, and holiday.
- #date_to_datetime(date) ⇒ Object
-
#holiday_date(name) ⇒ Object
get the date of holiday from name.
-
#holiday_list(events: self.ics_events) ⇒ Object
(also: #holidays)
holidays in since..until.
- #holiday_name(date) ⇒ Object
- #ics_end ⇒ Object (also: #ics_until)
-
#ics_holiday_list(events: self.ics_events) ⇒ Object
(also: #ics_holidays)
all holidays defined in ics file.
- #ics_start ⇒ Object (also: #ics_since)
-
#ics_weekday_list ⇒ Object
(also: #ics_week_days, #ics_working_days, #ics_business_days, #ics_weekdays, #ics_workingdays, #ics_businessdays)
get the weekdays defined in the ics file.
- #ics_weekend_list ⇒ Object (also: #ics_weekends)
- #initialize(_y = nil, _m = nil) ⇒ CalendarDays constructor
- #is_holiday?(date) ⇒ Boolean (also: #holiday?)
- #is_saturday?(date) ⇒ Boolean (also: #is_sat?, #saturday?)
- #is_sunday?(date) ⇒ Boolean (also: #is_sun?, #sunday?)
- #is_weekend?(date) ⇒ Boolean (also: #weekend?)
- #since ⇒ Object
- #to_s_date(date) ⇒ Object
- #until ⇒ Object
- #valid_date?(date_since = self.since, date_until = self.until) ⇒ Boolean
-
#weekday_list ⇒ Object
(also: #week_days, #working_days, #buisiness_days, #weekdays, #workingdays, #buisinessdays)
get the weekdays from the user-specified date range.
-
#weekend_list ⇒ Object
(also: #weekends)
get saturdays and sundays.
Methods included from NetCache
#create_repo_dir, #download_repo, #prepare_repo, #prepare_repo!, #repo_dir, #repo_dir_exist?, #repo_exist?, #repo_file, #repo_file_fullpath, #repo_uri
Constructor Details
#initialize(_y = nil, _m = nil) ⇒ CalendarDays
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/calendar_days/calendar_days.rb', line 49 def initialize( _y = nil, _m = nil ) # self.prepare_repo ics_file = File.open(repo_file_fullpath) @ics_events = Icalendar::Event.parse(ics_file).sort_by{|ev| ev.dtstart.to_datetime} ics_file.close @since_year = _y || self.ics_start.year @since_month = _m || 1 @since_day = 1 @until_year = _y || self.ics_end.year #ng. @until_month = _m || (_y.nil?)? self.ics_end.month : 12 @until_month = _m || ((_y.nil?)? self.ics_end.month : 12) @until_day = DateTime.new(@until_year, @until_month, -1).day raise ArgumentError, "You specified #{[_y, _m].compact.join(' and ')}."+ " Specify year (and month) in the date range #{to_s_date(ics_start)}"+ " - #{to_s_date(ics_end)}." unless valid_date? self end |
Instance Attribute Details
#ics_events ⇒ Object (readonly)
Returns the value of attribute ics_events.
75 76 77 |
# File 'lib/calendar_days/calendar_days.rb', line 75 def ics_events @ics_events end |
#since_day ⇒ Object (readonly)
Returns the value of attribute since_day.
73 74 75 |
# File 'lib/calendar_days/calendar_days.rb', line 73 def since_day @since_day end |
#since_month ⇒ Object (readonly)
Returns the value of attribute since_month.
73 74 75 |
# File 'lib/calendar_days/calendar_days.rb', line 73 def since_month @since_month end |
#since_year ⇒ Object (readonly)
Returns the value of attribute since_year.
73 74 75 |
# File 'lib/calendar_days/calendar_days.rb', line 73 def since_year @since_year end |
#until_day ⇒ Object (readonly)
Returns the value of attribute until_day.
74 75 76 |
# File 'lib/calendar_days/calendar_days.rb', line 74 def until_day @until_day end |
#until_month ⇒ Object (readonly)
Returns the value of attribute until_month.
74 75 76 |
# File 'lib/calendar_days/calendar_days.rb', line 74 def until_month @until_month end |
#until_year ⇒ Object (readonly)
Returns the value of attribute until_year.
74 75 76 |
# File 'lib/calendar_days/calendar_days.rb', line 74 def until_year @until_year end |
Instance Method Details
#__ics_end ⇒ Object
83 84 85 |
# File 'lib/calendar_days/calendar_days.rb', line 83 def __ics_end ics_events.last.dtstart.to_datetime end |
#__ics_start ⇒ Object
80 81 82 |
# File 'lib/calendar_days/calendar_days.rb', line 80 def __ics_start ics_events.first.dtstart.to_datetime end |
#__weekday_list(dt_since = self.since, dt_until = self.until) ⇒ Object
get the weekdays from the specified date range except saturday, sunday, and holiday. 指定した年(もしくは月)の平日 Weekday (土日祝日を抜いた日) を得る
109 110 111 |
# File 'lib/calendar_days/calendar_days.rb', line 109 def __weekday_list( dt_since = self.since, dt_until = self.until ) (dt_since..dt_until).select{|d| yield(d) } end |
#date_to_datetime(date) ⇒ Object
31 32 33 34 |
# File 'lib/calendar_days/calendar_days.rb', line 31 def date_to_datetime( date ) date = DateTime.parse(date) if date.is_a? ::String date end |
#holiday_date(name) ⇒ Object
get the date of holiday from name.
Args
- name
-
name of holiday.
Return
Name or [ Name, … ] (in case two or more dates are matched)
251 252 253 254 255 256 257 258 259 |
# File 'lib/calendar_days/calendar_days.rb', line 251 def holiday_date( name ) ret = unless block_given? holiday_list.select{|e| e.last =~ /#{name}/i }.map{|e| e.first } else holiday_list.select{|e| yield(e) }.map{|e| e.first } end ret = ret.first if ret.size == 1 ret end |
#holiday_list(events: self.ics_events) ⇒ Object Also known as: holidays
holidays in since..until
198 199 200 201 202 203 |
# File 'lib/calendar_days/calendar_days.rb', line 198 def holiday_list( events: self.ics_events ) idx_since = ics_holiday_list.bsearch_index{|al| self.since <= al.first } idx_until = ics_holiday_list.bsearch_index{|al| self.until <= al.first } - 1 # $stderr.puts "idx_since: #{idx_since}, idx_until: #{idx_until}" ics_holiday_list[idx_since..idx_until] end |
#holiday_name(date) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/calendar_days/calendar_days.rb', line 232 def holiday_name( date ) if date.is_a? ::String date = DateTime.parse(date) end __dt = date dt = __dt.to_s.gsub(/T.+$/, '') if is_holiday?(date) holiday_list.select{|e| e.first.to_s =~ /^#{dt}/}.first.last else nil end end |
#ics_end ⇒ Object Also known as: ics_until
90 91 92 93 |
# File 'lib/calendar_days/calendar_days.rb', line 90 def ics_end tmp = __ics_end DateTime.new(tmp.year, tmp.month, -1) end |
#ics_holiday_list(events: self.ics_events) ⇒ Object Also known as: ics_holidays
all holidays defined in ics file.
190 191 192 |
# File 'lib/calendar_days/calendar_days.rb', line 190 def ics_holiday_list( events: self.ics_events ) events.map{|ev| [ev.dtstart.to_datetime, ev.summary] } end |
#ics_start ⇒ Object Also known as: ics_since
86 87 88 89 |
# File 'lib/calendar_days/calendar_days.rb', line 86 def ics_start tmp = __ics_start DateTime.new(tmp.year, tmp.month, 1) end |
#ics_weekday_list ⇒ Object Also known as: ics_week_days, ics_working_days, ics_business_days, ics_weekdays, ics_workingdays, ics_businessdays
get the weekdays defined in the ics file.
125 126 127 128 129 |
# File 'lib/calendar_days/calendar_days.rb', line 125 def ics_weekday_list __weekday_list(ics_start, ics_end){|dt| not(is_weekend?(dt)) and not(is_holiday?(dt)) } end |
#ics_weekend_list ⇒ Object Also known as: ics_weekends
153 154 155 |
# File 'lib/calendar_days/calendar_days.rb', line 153 def ics_weekend_list __weekday_list(ics_start, ics_end){|dt| is_weekend?(dt) } end |
#is_holiday?(date) ⇒ Boolean Also known as: holiday?
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/calendar_days/calendar_days.rb', line 206 def is_holiday?( date ) if date.is_a? ::String date = DateTime.parse(date) end __dt = date # dt_first = ics_events().first.dtstart.to_datetime dt_last = ics_events().last.dtstart.to_datetime if __dt.year < dt_first.year # return nil raise ArgumentError, "year #{__dt.year} in #{date} is too old;" \ " specify after #{dt_first.year}." elsif __dt.year > dt_last.year # return nil raise ArgumentError, "year #{__dt.year} in #{date} is too new;" \ " specify before #{dt_last.year}." end dt = __dt.to_s.gsub(/T.+$/, '') holiday_list.map{|e| e.first.to_s }.grep( /^#{dt}/ ).size > 0 end |
#is_saturday?(date) ⇒ Boolean Also known as: is_sat?, saturday?
162 163 164 |
# File 'lib/calendar_days/calendar_days.rb', line 162 def is_saturday?( date ) date_to_datetime(date).saturday? end |
#is_sunday?(date) ⇒ Boolean Also known as: is_sun?, sunday?
168 169 170 |
# File 'lib/calendar_days/calendar_days.rb', line 168 def is_sunday?( date ) date_to_datetime(date).sunday? end |
#is_weekend?(date) ⇒ Boolean Also known as: weekend?
174 175 176 |
# File 'lib/calendar_days/calendar_days.rb', line 174 def is_weekend?(date) is_saturday?(date) or is_sunday?(date) end |
#since ⇒ Object
77 |
# File 'lib/calendar_days/calendar_days.rb', line 77 def since; DateTime.new(since_year, since_month, since_day); end |
#to_s_date(date) ⇒ Object
27 28 29 |
# File 'lib/calendar_days/calendar_days.rb', line 27 def to_s_date( date ) date.to_s.gsub(/T.+$/, '') end |
#until ⇒ Object
78 |
# File 'lib/calendar_days/calendar_days.rb', line 78 def until; DateTime.new(until_year, until_month, until_day); end |
#valid_date?(date_since = self.since, date_until = self.until) ⇒ Boolean
36 37 38 39 40 41 42 |
# File 'lib/calendar_days/calendar_days.rb', line 36 def valid_date?(date_since = self.since, date_until = self.until) if ics_start <= date_since and date_until <= ics_end true else false end end |
#weekday_list ⇒ Object Also known as: week_days, working_days, buisiness_days, weekdays, workingdays, buisinessdays
get the weekdays from the user-specified date range.
116 117 118 119 120 |
# File 'lib/calendar_days/calendar_days.rb', line 116 def weekday_list __weekday_list{|dt| not(is_weekend?(dt)) and not(is_holiday?(dt)) } end |
#weekend_list ⇒ Object Also known as: weekends
get saturdays and sundays.
Attention
there exists such days which are both weekend and holiday.
150 151 152 |
# File 'lib/calendar_days/calendar_days.rb', line 150 def weekend_list __weekday_list{|dt| is_weekend?(dt) } end |