zakuro 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -0
  3. data/lib/zakuro/calculation/base/gengou.rb +82 -0
  4. data/lib/zakuro/calculation/base/linear_gengou.rb +76 -0
  5. data/lib/zakuro/calculation/base/operated_year.rb +5 -15
  6. data/lib/zakuro/calculation/base/year.rb +26 -14
  7. data/lib/zakuro/calculation/gengou/internal/counter.rb +129 -0
  8. data/lib/zakuro/calculation/gengou/internal/reserve/interval.rb +183 -0
  9. data/lib/zakuro/calculation/gengou/internal/reserve/list.rb +382 -0
  10. data/lib/zakuro/calculation/gengou/internal/reserve.rb +42 -0
  11. data/lib/zakuro/calculation/gengou/scroll.rb +262 -0
  12. data/lib/zakuro/calculation/monthly/first_day.rb +3 -2
  13. data/lib/zakuro/calculation/monthly/month.rb +49 -2
  14. data/lib/zakuro/calculation/monthly/operated_month.rb +2 -2
  15. data/lib/zakuro/calculation/range/full_range.rb +65 -103
  16. data/lib/zakuro/calculation/range/operated_range.rb +13 -8
  17. data/lib/zakuro/calculation/range/operated_solar_terms.rb +36 -17
  18. data/lib/zakuro/calculation/range/transfer/gengou_scroller.rb +54 -0
  19. data/lib/zakuro/calculation/range/transfer/year_boundary.rb +25 -21
  20. data/lib/zakuro/calculation/specifier/single_day.rb +13 -35
  21. data/lib/zakuro/calculation/summary/single.rb +5 -2
  22. data/lib/zakuro/calculation/version/internal/crawler.rb +51 -0
  23. data/lib/zakuro/calculation/version/internal/range.rb +39 -0
  24. data/lib/zakuro/calculation/version/version.rb +24 -0
  25. data/lib/zakuro/era/japan/calendar.rb +133 -0
  26. data/lib/zakuro/era/japan/gengou/parser.rb +95 -25
  27. data/lib/zakuro/era/japan/gengou/type.rb +148 -41
  28. data/lib/zakuro/era/japan/gengou/validator.rb +157 -52
  29. data/lib/zakuro/era/japan/gengou/yaml/set-001-until-south.yaml +1870 -0
  30. data/lib/zakuro/era/japan/gengou/yaml/set-002-from-north.yaml +810 -0
  31. data/lib/zakuro/era/japan/gengou/yaml/set-003-modern.yaml +50 -0
  32. data/lib/zakuro/era/japan/gengou.rb +5 -5
  33. data/lib/zakuro/era/japan/version.rb +151 -0
  34. data/lib/zakuro/era/{western.rb → western/calendar.rb} +0 -0
  35. data/lib/zakuro/merchant.rb +12 -3
  36. data/lib/zakuro/operation/month/parser.rb +1 -1
  37. data/lib/zakuro/operation/month/type.rb +1 -1
  38. data/lib/zakuro/operation/month/validator.rb +1 -1
  39. data/lib/zakuro/output/response.rb +5 -5
  40. data/lib/zakuro/tools/typeconv.rb +38 -0
  41. data/lib/zakuro/tools/typeof.rb +4 -1
  42. data/lib/zakuro/version/context.rb +24 -3
  43. data/lib/zakuro/version/daien/daien.rb +1 -26
  44. data/lib/zakuro/version/genka/genka.rb +1 -26
  45. data/lib/zakuro/version/gihou/gihou.rb +1 -30
  46. data/lib/zakuro/version/gregorio/gregorio.rb +1 -9
  47. data/lib/zakuro/version/houryaku/houryaku.rb +1 -9
  48. data/lib/zakuro/version/joukyou/joukyou.rb +1 -9
  49. data/lib/zakuro/version/kansei/kansei.rb +1 -9
  50. data/lib/zakuro/version/senmyou/senmyou.rb +1 -30
  51. data/lib/zakuro/version/tenpou/tenpou.rb +1 -9
  52. data/zakuro.gemspec +1 -3
  53. metadata +25 -17
  54. data/lib/zakuro/calculation/base/multi_gengou.rb +0 -101
  55. data/lib/zakuro/calculation/base/multi_gengou_roller.rb +0 -218
  56. data/lib/zakuro/calculation/range/transfer/western_date_allocation.rb +0 -82
  57. data/lib/zakuro/era/japan/reki.rb +0 -91
  58. data/lib/zakuro/era/japan/yaml/set-001-until-south.yaml +0 -1121
  59. data/lib/zakuro/era/japan/yaml/set-002-from-north.yaml +0 -485
  60. data/lib/zakuro/era/japan/yaml/set-003-modern.yaml +0 -28
  61. data/lib/zakuro/version/abstract_version.rb +0 -29
  62. data/lib/zakuro/version.rb +0 -7
  63. data/lib/zakuro/version_factory.rb +0 -59
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47e40b8ff5fd4b60fffed41193862d39a4d0569facec9c12f0aef65391bb4103
4
- data.tar.gz: a411cb2f43e952c52892ba4b5505ada7c96706effd2f4dae4019e0f39f409c01
3
+ metadata.gz: 567799c25255c97ec707d421b465358c1209afeef0d8c2f1c84a9b3bd5e65148
4
+ data.tar.gz: bff172488e9ea94a8da83d09fa1dc9bd62a28de871ad0cc41217d20ff5dc2b94
5
5
  SHA512:
6
- metadata.gz: 640e6519e03854a38da45e19dbbf589762a4f24b89c55b683383eb56e88471f4469ee41cde88fb3a1d3f91862091d323f820a9e46b9a9b3297826d62f40d5159
7
- data.tar.gz: 0c9d510a3ffac317b74286013f6fd0e476074ab1a8de2b7368d710d91e1a9f10f114340eb8a66f88b3e28795865bf5a483ce1a8aa49ae8382bee40104fb85a3b
6
+ metadata.gz: daf11f9f9319d23fbba463571cb8f76184aeb7b6133fcde661bd06773113f37bc9684e8680a848c4998a96ea287f371ca123f878a7bb5dbdfb2ed499c434f8a7
7
+ data.tar.gz: 3624066b34b4271961793f58bbad9509b046a3df967df4513de195352237386ccd5848b9b5f72c8838f6efb944fac1129b494001c570aa2cb5a93440d089e173
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.6.0
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../era/western/calendar'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Base
11
+ #
12
+ # Gengou 元号
13
+ #
14
+ class Gengou
15
+ # @return [Western::Calendar] 開始日
16
+ attr_reader :start_date
17
+ # @return [Western::Calendar] 終了日
18
+ attr_reader :end_date
19
+ # @return [Array<LinearGengou>] 1行目元号
20
+ attr_reader :first_line
21
+ # @return [Array<LinearGengou>] 2行目元号
22
+ attr_reader :second_line
23
+
24
+ #
25
+ # 初期化
26
+ #
27
+ # @param [Western::Calendar] start_date 開始日
28
+ # @param [Western::Calendar] end_date 終了日
29
+ # @param [Array<LinearGengou>] first_line 1行目元号
30
+ # @param [Array<LinearGengou>] second_line 2行目元号
31
+ #
32
+ def initialize(start_date: Western::Calendar.new, end_date: Western::Calendar.new,
33
+ first_line: [], second_line: [])
34
+ @start_date = start_date
35
+ @end_date = end_date
36
+ @first_line = first_line
37
+ @second_line = second_line
38
+ end
39
+
40
+ #
41
+ # 1行目元号を取得する
42
+ #
43
+ # @param [Western::Calendar] date 西暦日
44
+ #
45
+ # @return [LinearGengou] 1行目元号
46
+ #
47
+ def match_first_line(date: Western::Calendar)
48
+ @first_line.each do |line|
49
+ return line.clone if line.include?(date: date)
50
+ end
51
+
52
+ LinearGengou.new
53
+ end
54
+
55
+ #
56
+ # 2行目元号を取得する
57
+ #
58
+ # @param [Western::Calendar] date 西暦日
59
+ #
60
+ # @return [LinearGengou] 2行目元号
61
+ #
62
+ def match_second_line(date: Western::Calendar)
63
+ @second_line.each do |line|
64
+ return line.clone if line.include?(date: date)
65
+ end
66
+
67
+ LinearGengou.new
68
+ end
69
+
70
+ #
71
+ # 不正か
72
+ #
73
+ # @return [True] 不正
74
+ # @return [False] 不正なし
75
+ #
76
+ def invalid?
77
+ @first_line.size.zero? && @second_line.size.zero?
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../era/western/calendar'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Base
11
+ #
12
+ # LinearGengou 直列元号
13
+ #
14
+ class LinearGengou
15
+ INVALID_YEAR = -1
16
+
17
+ # @return [Western::Calendar] 開始日
18
+ attr_reader :start_date
19
+ # @return [Western::Calendar] 終了日
20
+ attr_reader :end_date
21
+ # @return [String] 元号名
22
+ attr_reader :name
23
+ # @return [Integer] 年
24
+ attr_reader :year
25
+
26
+ #
27
+ # 初期化
28
+ #
29
+ # @param [Western::Calendar] start_date 開始日
30
+ # @param [Western::Calendar] end_date 終了日
31
+ # @param [String] name 元号名
32
+ # @param [Integer] 元号年
33
+ #
34
+ def initialize(start_date: Western::Calendar.new, end_date: Western::Calendar.new,
35
+ name: '', year: INVALID_YEAR)
36
+ @start_date = start_date
37
+ @end_date = end_date
38
+ @name = name
39
+ @year = year
40
+ end
41
+
42
+ #
43
+ # 不正か
44
+ #
45
+ # @return [True] 不正
46
+ # @return [False] 不正なし
47
+ #
48
+ def invalid?
49
+ @name == '' || @year == INVALID_YEAR
50
+ end
51
+
52
+ #
53
+ # 範囲内か
54
+ #
55
+ # @param [Western::Calendar] date 西暦日
56
+ #
57
+ # @return [True] 範囲内
58
+ # @return [False] 範囲外
59
+ #
60
+ def include?(date: Western::Calendar.new)
61
+ return false if invalid?
62
+
63
+ return false if @start_date.invalid?
64
+
65
+ return false if @end_date.invalid?
66
+
67
+ return false if date < @start_date
68
+
69
+ return false if date > @end_date
70
+
71
+ true
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './year'
4
- require_relative './multi_gengou'
4
+
5
+ require_relative '../../version/context'
5
6
 
6
7
  # :nodoc:
7
8
  module Zakuro
@@ -16,23 +17,12 @@ module Zakuro
16
17
  #
17
18
  # 初期化
18
19
  #
19
- # @param [Gengou] multi_gengou 元号
20
+ # @param [Context] context 暦コンテキスト
20
21
  # @param [Array<OperatedMonth>] months 年内の全ての月
21
22
  # @param [Integer] total_days 年の日数
22
- # @param [Western::Calendar] new_year_date 元旦
23
23
  #
24
- def initialize(multi_gengou: MultiGengou.new, new_year_date: Western::Calendar.new,
25
- months: [], total_days: 0)
26
- super(multi_gengou: multi_gengou, new_year_date: new_year_date,
27
- months: months, total_days: total_days)
28
- end
29
-
30
- def commit
31
- super
32
-
33
- return if months.empty?
34
-
35
- @new_year_date = months[0].first_day.western_date
24
+ def initialize(context: Context.new, months: [], total_days: 0)
25
+ super(context: context, months: months, total_days: total_days)
36
26
  end
37
27
 
38
28
  #
@@ -2,6 +2,10 @@
2
2
 
3
3
  require_relative '../cycle/zodiac'
4
4
 
5
+ require_relative '../../era/western/calendar'
6
+
7
+ require_relative '../../version/context'
8
+
5
9
  # :nodoc:
6
10
  module Zakuro
7
11
  # :nodoc:
@@ -12,28 +16,23 @@ module Zakuro
12
16
  # Year 年
13
17
  #
14
18
  class Year
15
- # @return [Gengou] 元号
16
- attr_reader :multi_gengou
19
+ # @return [Context] 暦コンテキスト
20
+ attr_reader :context
17
21
  # @return [Array<Month>] 年内の全ての月
18
22
  attr_reader :months
19
23
  # @return [Integer] 年の日数
20
24
  attr_reader :total_days
21
- # @return [Western::Calendar] 元旦
22
- attr_reader :new_year_date
23
25
 
24
26
  #
25
27
  # 初期化
26
28
  #
27
- # @param [Gengou] multi_gengou 元号
29
+ # @param [Context] context 暦コンテキスト
28
30
  # @param [Array<Month>] months 年内の全ての月
29
31
  # @param [Integer] total_days 年の日数
30
- # @param [Western::Calendar] new_year_date 元旦
31
32
  #
32
- def initialize(multi_gengou: MultiGengou.new, new_year_date: Western::Calendar.new,
33
- months: [], total_days: 0)
34
- @multi_gengou = multi_gengou
33
+ def initialize(context: Context.new, months: [], total_days: 0)
34
+ @context = context
35
35
  @months = months
36
- @new_year_date = new_year_date
37
36
  @total_days = total_days
38
37
  end
39
38
 
@@ -58,21 +57,34 @@ module Zakuro
58
57
  # @return [MultiGengou] 自身
59
58
  #
60
59
  def next_year
61
- @multi_gengou.next_year
62
-
63
- @new_year_date += @total_days
64
60
  @total_days = 0
65
61
 
66
62
  self
67
63
  end
68
64
 
65
+ #
66
+ # 年初を取得する
67
+ #
68
+ # @return [Western::Calendar] 年初
69
+ #
70
+ def new_year_date
71
+ return Western::Calendar.new unless months
72
+
73
+ return Western::Calendar.new if months.size.zero?
74
+
75
+ @months[0].western_date
76
+ end
77
+
69
78
  #
70
79
  # 十干十二支を取得する
71
80
  #
72
81
  # @return [String] 十干十二支
73
82
  #
74
83
  def zodiac_name
75
- Cycle::Zodiac.year_name(western_year: @new_year_date.year)
84
+ date = new_year_date
85
+ return '' if date.invalid?
86
+
87
+ Cycle::Zodiac.year_name(western_year: date.year)
76
88
  end
77
89
 
78
90
  #
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../era/japan/gengou'
4
+ require_relative '../../../era/japan/calendar'
5
+ require_relative '../../../era/western/calendar'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Calculation
11
+ # :nodoc:
12
+ module Gengou
13
+ #
14
+ # Counter 加算元号
15
+ #
16
+ class Counter
17
+ # @return [Integer] 不正値
18
+ INVALID_YEAR = -1
19
+
20
+ # @return [Japan::Gengou] 元号
21
+ attr_reader :gengou
22
+ # @return [Integer] 元号年
23
+ attr_reader :japan_year
24
+ # @return [Integer] 西暦年
25
+ attr_reader :western_year
26
+
27
+ #
28
+ # 初期化
29
+ #
30
+ # @param [Japan::Gengou] gengou 元号
31
+ #
32
+ def initialize(gengou: Japan::Gengou.new)
33
+ @gengou = gengou
34
+ @japan_year = gengou.both_start_year.japan
35
+ @western_year = gengou.both_start_year.western
36
+ end
37
+
38
+ #
39
+ # 和暦開始日を取得する
40
+ #
41
+ # @return [Japan::Calendar] 和暦開始日
42
+ #
43
+ def japan_start_date
44
+ return Japan::Calendar.new if @gengou.invalid?
45
+
46
+ @gengou.both_start_date.japan
47
+ end
48
+
49
+ #
50
+ # 西暦開始日を取得する
51
+ #
52
+ # @return [Western::Calendar] 西暦開始日
53
+ #
54
+ def western_start_date
55
+ return Western::Calendar.new if @gengou.invalid?
56
+
57
+ @gengou.both_start_date.western
58
+ end
59
+
60
+ #
61
+ # 西暦終了日を取得する
62
+ #
63
+ # @return [Western::Calendar] 西暦終了日
64
+ #
65
+ def western_end_date
66
+ return Western::Calendar.new if @gengou.invalid?
67
+
68
+ @gengou.end_date
69
+ end
70
+
71
+ #
72
+ # 次年にする
73
+ #
74
+ # @return [MultiGengou] 自身
75
+ #
76
+ def next_year
77
+ @japan_year += 1
78
+ @western_year += 1
79
+
80
+ self
81
+ end
82
+
83
+ #
84
+ # 元号名を取得する
85
+ #
86
+ # @return [String] 元号名
87
+ #
88
+ def name
89
+ return '' unless @gengou
90
+
91
+ @gengou.name
92
+ end
93
+
94
+ #
95
+ # 不正か
96
+ #
97
+ # @return [True] 不正
98
+ # @return [False] 不正なし
99
+ #
100
+ def invalid?
101
+ @gengou.invalid? || @japan_year == INVALID_YEAR || @western_year == INVALID_YEAR
102
+ end
103
+
104
+ #
105
+ # 指定した日が元号に含まれるか
106
+ #
107
+ # @param [Western::Calendar] date 日
108
+ #
109
+ # @return [True] 含まれる
110
+ # @return [False] 含まれない
111
+ #
112
+ def include?
113
+ @gengou.include?
114
+ end
115
+
116
+ #
117
+ # ディープコピー
118
+ #
119
+ # @param [MultiGengou] obj 自身
120
+ #
121
+ def initialize_copy(obj)
122
+ @gengou = obj.gengou.clone
123
+ @japan_year = obj.japan_year
124
+ @western_year = obj.western_year
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../era/western/calendar'
4
+
5
+ require_relative './list'
6
+
7
+ # :nodoc:
8
+ module Zakuro
9
+ # :nodoc:
10
+ module Calculation
11
+ # :nodoc:
12
+ module Gengou
13
+ # :nodoc:
14
+ module Reserve
15
+ # Interval
16
+ #
17
+ # 予約済み計算範囲
18
+ #
19
+ class Interval
20
+ # @return [List] 1行目元号
21
+ attr_reader :first_gengou
22
+ # @return [List] 2行目元号
23
+ attr_reader :second_gengou
24
+
25
+ #
26
+ # 初期化
27
+ #
28
+ # @param [Western::Calendar] start_date 西暦開始日
29
+ # @param [Western::Calendar] end_date 西暦終了日
30
+ #
31
+ def initialize(start_date: Western::Calendar.new, end_date: Western::Calendar.new)
32
+ @first_gengou = List.new(first: true, start_date: start_date, end_date: end_date)
33
+ @second_gengou = List.new(first: false, start_date: start_date, end_date: end_date)
34
+ end
35
+
36
+ #
37
+ # 不正か
38
+ #
39
+ # @return [True] 不正
40
+ # @return [False] 不正なし
41
+ #
42
+ def invalid?
43
+ @first_gengou.invalid?
44
+ end
45
+
46
+ #
47
+ # 1行目元号を取得する
48
+ #
49
+ # @param [Western::Calendar] western_date 西暦日
50
+ #
51
+ # @return [Gengou::Counter] 加算元号
52
+ #
53
+ def match_first_gengou(western_date: Western::Calendar.new)
54
+ @first_gengou.get(western_date: western_date)
55
+ end
56
+
57
+ #
58
+ # 2行目元号を取得する
59
+ #
60
+ # @param [Western::Calendar] western_date 西暦日
61
+ #
62
+ # @return [Gengou::Counter] 加算元号
63
+ #
64
+ def match_second_gengou(western_date: Western::Calendar.new)
65
+ @second_gengou.get(western_date: western_date)
66
+ end
67
+
68
+ #
69
+ # 範囲内元号(1行目元号)を取得する
70
+ #
71
+ # @param [Western::Calendar] start_date 西暦開始日
72
+ # @param [Western::Calendar] end_date 西暦終了日
73
+ #
74
+ # @return [Array<Gengou::Counter>] 範囲内元号(1行目元号)
75
+ #
76
+ def collect_first_gengou(start_date: Western::Calendar.new,
77
+ end_date: Western::Calendar.new)
78
+ @first_gengou.collect(start_date: start_date, end_date: end_date)
79
+ end
80
+
81
+ #
82
+ # 範囲内元号(2行目元号)を取得する
83
+ #
84
+ # @param [Western::Calendar] start_date 西暦開始日
85
+ # @param [Western::Calendar] end_date 西暦終了日
86
+ #
87
+ # @return [Array<Gengou::Counter>] 範囲内元号(2行目元号)
88
+ #
89
+ def collect_second_gengou(start_date: Western::Calendar.new,
90
+ end_date: Western::Calendar.new)
91
+ @second_gengou.collect(start_date: start_date, end_date: end_date)
92
+ end
93
+
94
+ #
95
+ # 最古の元号から和暦開始日を取得する
96
+ #
97
+ # @return [Japan::Calendar] 和暦開始日
98
+ #
99
+ def japan_start_date
100
+ gengou = oldest_gengou
101
+
102
+ return Japan::Calendar.new if gengou.invalid?
103
+
104
+ gengou.japan_start_date
105
+ end
106
+
107
+ #
108
+ # 最古の元号から西暦開始日を取得する
109
+ #
110
+ # @return [Western::Calendar] 西暦開始日
111
+ #
112
+ def western_start_date
113
+ gengou = oldest_gengou
114
+
115
+ return Western::Calendar.new if gengou.invalid?
116
+
117
+ gengou.western_start_date
118
+ end
119
+
120
+ #
121
+ # 開始西暦年を取得する
122
+ #
123
+ # @return [Integer] 開始西暦年
124
+ #
125
+ def western_start_year
126
+ first_start_year = @first_gengou.western_start_year
127
+ second_start_year = @second_gengou.western_start_year
128
+
129
+ return first_start_year if @first_gengou.invalid?
130
+
131
+ return first_start_year if @second_gengou.invalid?
132
+
133
+ return first_start_year if first_start_year < second_start_year
134
+
135
+ second_start_year
136
+ end
137
+
138
+ #
139
+ # 終了西暦年を取得する
140
+ #
141
+ # @return [Integer] 終了西暦年
142
+ #
143
+ def western_end_year
144
+ first_end_year = @first_gengou.western_end_year
145
+ second_end_year = @second_gengou.western_end_year
146
+
147
+ return first_end_year if @first_gengou.invalid?
148
+
149
+ return first_end_year if @second_gengou.invalid?
150
+
151
+ return first_end_year if first_end_year > second_end_year
152
+
153
+ second_end_year
154
+ end
155
+
156
+ private
157
+
158
+ #
159
+ # 最古の元号を取得する
160
+ #
161
+ # @return [Japan::Gengou] 最古の元号
162
+ #
163
+ def oldest_gengou
164
+ return @first_gengou if @first_gengou.invalid?
165
+
166
+ return @first_gengou if @second_gengou.invalid?
167
+
168
+ first_western_date = @first_gengou.western_start_date
169
+ second_western_date = @second_gengou.western_start_date
170
+
171
+ return @first_gengou if first_western_date.invalid?
172
+
173
+ return @first_gengou if second_western_date.invalid?
174
+
175
+ return @first_gengou if first_western_date < second_western_date
176
+
177
+ @second_gengou
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end