calendar_days 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -3
- data/lib/calendar_days/calendar_days.rb +109 -54
- data/lib/calendar_days/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31dae8899c926deae858efaef3223ba9149e6167
|
4
|
+
data.tar.gz: fbc47a0501b65486ef3932ebe3c9d6bef338208a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fe529aad1cc89d0b7dbae5cc9e439de3877aa47e36311edfb3c74e55cd34a0cc98ed525ee5ae2a056ec499849c2bdf12e0fa225a85936f23347a5b3ebb9dbbb
|
7
|
+
data.tar.gz: 809e8078df712ddbd5a4f48400929b586a01499e30acbfebf8ab5e82a3ad24a220ae357a1e409878bc80942451dc9438c6cef648d54a59a6303f74e5616d3de0
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# CalendarDays
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/calendar_days`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
3
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,12 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
```
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
```
|
28
|
+
|
26
29
|
|
27
30
|
## Development
|
28
31
|
|
@@ -37,3 +40,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/mephis
|
|
37
40
|
## License
|
38
41
|
|
39
42
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
43
|
+
|
44
|
+
|
@@ -11,7 +11,8 @@ require "calendar_days/net_cache"
|
|
11
11
|
#
|
12
12
|
#
|
13
13
|
#
|
14
|
-
class CalendarDays < ::DateTime
|
14
|
+
# class CalendarDays < ::DateTime
|
15
|
+
class CalendarDays
|
15
16
|
|
16
17
|
include NetCache
|
17
18
|
|
@@ -22,45 +23,76 @@ end
|
|
22
23
|
#
|
23
24
|
#
|
24
25
|
class CalendarDays
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
def self.new( in_year = DateTime.now.year, since_month = nil )
|
29
|
-
since_month_tmp = since_month || 1
|
30
|
-
tmp = super(in_year, since_month_tmp, 1)
|
31
|
-
tmp.instance_eval{ initialize(in_year, since_month) }
|
26
|
+
|
27
|
+
def to_s_date( date )
|
28
|
+
date.to_s.gsub(/T.+$/, '')
|
32
29
|
end
|
33
|
-
alias :in_year :year
|
34
|
-
alias :since_month :month
|
35
|
-
alias :since_day :day
|
36
|
-
def since; DateTime.new(in_year, since_month, since_day); end
|
37
30
|
|
38
|
-
def
|
31
|
+
def date_to_datetime( date )
|
32
|
+
date = DateTime.parse(date) if date.is_a? ::String
|
33
|
+
date
|
34
|
+
end
|
39
35
|
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
def valid_date?(date_since = self.since, date_until = self.until)
|
37
|
+
if ics_start <= date_since and date_until <= ics_end
|
38
|
+
true
|
39
|
+
else
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
43
|
|
44
|
-
|
44
|
+
#
|
45
|
+
# ==== Args
|
46
|
+
# _y :: year[opt.]
|
47
|
+
# _m :: month[opt.]
|
48
|
+
#
|
49
|
+
def initialize( _y = nil, _m = nil )
|
45
50
|
|
46
51
|
#
|
52
|
+
self.prepare_repo
|
47
53
|
ics_file = File.open(repo_file_fullpath)
|
48
54
|
@ics_events = Icalendar::Event.parse(ics_file).sort_by{|ev| ev.dtstart.to_datetime}
|
49
55
|
ics_file.close
|
50
56
|
|
57
|
+
@since_year = _y || self.ics_start.year
|
58
|
+
@since_month = _m || 1
|
59
|
+
@since_day = 1
|
60
|
+
|
61
|
+
@until_year = _y || self.ics_end.year
|
62
|
+
#ng. @until_month = _m || (_y.nil?)? self.ics_end.month : 12
|
63
|
+
@until_month = _m || ((_y.nil?)? self.ics_end.month : 12)
|
64
|
+
@until_day = DateTime.new(@until_year, @until_month, -1).day
|
65
|
+
|
66
|
+
raise ArgumentError, "You specified #{[_y, _m].compact.join(' and ')}."+
|
67
|
+
" Specify year (and month) in the date range #{to_s_date(ics_start)}"+
|
68
|
+
" - #{to_s_date(ics_end)}." unless valid_date?
|
69
|
+
|
51
70
|
self
|
52
71
|
end
|
53
|
-
|
54
|
-
attr_reader
|
55
|
-
|
56
|
-
|
72
|
+
|
73
|
+
attr_reader :since_year, :since_month, :since_day
|
74
|
+
attr_reader :until_year, :until_month, :until_day
|
75
|
+
attr_reader :ics_events
|
76
|
+
|
77
|
+
def since; DateTime.new(since_year, since_month, since_day); end
|
78
|
+
def until; DateTime.new(until_year, until_month, until_day); end
|
79
|
+
|
80
|
+
def __ics_start
|
57
81
|
ics_events.first.dtstart.to_datetime
|
58
82
|
end
|
59
|
-
def
|
83
|
+
def __ics_end
|
60
84
|
ics_events.last.dtstart.to_datetime
|
61
85
|
end
|
62
|
-
def
|
63
|
-
|
86
|
+
def ics_start
|
87
|
+
tmp = __ics_start
|
88
|
+
DateTime.new(tmp.year, tmp.month, 1)
|
89
|
+
end
|
90
|
+
def ics_end
|
91
|
+
tmp = __ics_end
|
92
|
+
DateTime.new(tmp.year, tmp.month, -1)
|
93
|
+
end
|
94
|
+
alias :ics_since :ics_start
|
95
|
+
alias :ics_until :ics_end
|
64
96
|
|
65
97
|
end
|
66
98
|
|
@@ -70,62 +102,79 @@ end
|
|
70
102
|
#
|
71
103
|
class CalendarDays
|
72
104
|
|
73
|
-
|
74
|
-
|
75
|
-
|
105
|
+
# get the weekdays from the specified date range except saturday, sunday, and holiday.
|
106
|
+
# 指定した年(もしくは月)の平日 Weekday (土日祝日を抜いた日) を得る
|
107
|
+
#
|
108
|
+
#
|
109
|
+
def __weekday_list( dt_since = self.since, dt_until = self.until )
|
110
|
+
(dt_since..dt_until).select{|d| yield(d) }
|
76
111
|
end
|
77
112
|
|
78
|
-
#
|
113
|
+
# get the weekdays from the user-specified date range.
|
114
|
+
#
|
115
|
+
#
|
116
|
+
def weekday_list
|
117
|
+
__weekday_list{|dt|
|
118
|
+
not(is_weekend?(dt)) and not(is_holiday?(dt))
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
# get the weekdays defined in the ics file.
|
79
123
|
#
|
80
124
|
#
|
81
|
-
def
|
82
|
-
|
125
|
+
def ics_weekday_list
|
126
|
+
__weekday_list(ics_start, ics_end){|dt|
|
127
|
+
not(is_weekend?(dt)) and not(is_holiday?(dt))
|
128
|
+
}
|
129
|
+
end
|
83
130
|
|
84
|
-
|
85
|
-
|
131
|
+
alias :week_days :weekday_list
|
132
|
+
alias :working_days :weekday_list
|
133
|
+
alias :buisiness_days :weekday_list
|
86
134
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
if yield(dt_tmp)
|
91
|
-
ret.push dt_tmp
|
92
|
-
end
|
135
|
+
alias :weekdays :weekday_list
|
136
|
+
alias :workingdays :weekday_list
|
137
|
+
alias :buisinessdays :weekday_list
|
93
138
|
|
94
|
-
|
95
|
-
|
139
|
+
alias :ics_week_days :ics_weekday_list
|
140
|
+
alias :ics_working_days :ics_weekday_list
|
141
|
+
alias :ics_business_days :ics_weekday_list
|
96
142
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
ret = __weekday_list{|dt| not(is_weekend?(dt)) and not(is_holiday?(dt)) }
|
101
|
-
ret
|
102
|
-
end
|
143
|
+
alias :ics_weekdays :ics_weekday_list
|
144
|
+
alias :ics_workingdays :ics_weekday_list
|
145
|
+
alias :ics_businessdays :ics_weekday_list
|
103
146
|
|
104
|
-
#
|
147
|
+
# get saturdays and sundays.
|
105
148
|
# ==== Attention
|
106
149
|
# there exists such days which are both weekend and holiday.
|
107
150
|
def weekend_list
|
108
|
-
|
109
|
-
|
151
|
+
__weekday_list{|dt| is_weekend?(dt) }
|
152
|
+
end
|
153
|
+
def ics_weekend_list
|
154
|
+
__weekday_list(ics_start, ics_end){|dt| is_weekend?(dt) }
|
110
155
|
end
|
111
|
-
alias :
|
112
|
-
alias :
|
113
|
-
alias :weekends :weekend_list
|
156
|
+
alias :weekends :weekend_list
|
157
|
+
alias :ics_weekends :ics_weekend_list
|
114
158
|
|
159
|
+
#
|
160
|
+
#
|
161
|
+
#
|
115
162
|
def is_saturday?( date )
|
116
163
|
date_to_datetime(date).saturday?
|
117
164
|
end
|
118
|
-
alias :is_sat?
|
119
|
-
alias :
|
165
|
+
alias :is_sat? :is_saturday?
|
166
|
+
alias :saturday? :is_saturday?
|
120
167
|
|
121
168
|
def is_sunday?( date )
|
122
169
|
date_to_datetime(date).sunday?
|
123
170
|
end
|
124
171
|
alias :is_sun? :is_sunday?
|
172
|
+
alias :sunday? :is_sunday?
|
125
173
|
|
126
174
|
def is_weekend?(date)
|
127
175
|
is_saturday?(date) or is_sunday?(date)
|
128
176
|
end
|
177
|
+
alias :weekend? :is_weekend?
|
129
178
|
|
130
179
|
end
|
131
180
|
|
@@ -178,6 +227,7 @@ class CalendarDays
|
|
178
227
|
dt = __dt.to_s.gsub(/T.+$/, '')
|
179
228
|
holiday_list.map{|e| e.first.to_s }.grep( /^#{dt}/ ).size > 0
|
180
229
|
end
|
230
|
+
alias :holiday? :is_holiday?
|
181
231
|
|
182
232
|
def holiday_name( date )
|
183
233
|
if date.is_a? ::String
|
@@ -193,6 +243,11 @@ class CalendarDays
|
|
193
243
|
end
|
194
244
|
end
|
195
245
|
|
246
|
+
# get the date of holiday from name.
|
247
|
+
# ==== Args
|
248
|
+
# name :: name of holiday.
|
249
|
+
# ==== Return
|
250
|
+
# Name or [ Name, ... ] (in case two or more dates are matched)
|
196
251
|
def holiday_date( name )
|
197
252
|
ret = unless block_given?
|
198
253
|
holiday_list.select{|e| e.last =~ /#{name}/i }.map{|e| e.first }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calendar_days
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- YAMAMOTO, Masayuki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|