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
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../base/multi_gengou_roller'
4
3
  require_relative '../base/year'
5
4
 
6
- require_relative '../../era/western'
5
+ require_relative '../../era/western/calendar'
7
6
  require_relative '../../output/response'
8
7
  require_relative '../../output/logger'
9
8
 
@@ -29,11 +28,7 @@ module Zakuro
29
28
  # @return [Result::Data::SingleDay] 和暦日
30
29
  #
31
30
  def self.get(years: [], date: Western::Calendar.new)
32
- year = specify_year(years: years, date: date)
33
-
34
- year = transfer(year: year, date: date)
35
-
36
- month = specify_month(year: year, date: date)
31
+ year, month = specify(years: years, date: date)
37
32
  first_date = month.western_date
38
33
 
39
34
  Output::Response::SingleDay.save_single_day(
@@ -50,35 +45,18 @@ module Zakuro
50
45
  # @param [Array<Year>] years 範囲
51
46
  # @param [Western::Calendar] date 西暦日
52
47
  #
53
- # @return [Year] 対象年
48
+ # @return [Base::Year] 対象年
49
+ # @return [Base::Month] 対象月
54
50
  #
55
- def self.specify_year(years:, date:)
56
- years.reverse_each do |year|
57
- return year if date >= year.new_year_date
51
+ def self.specify(years:, date:)
52
+ years.each do |year|
53
+ month = specify_month(year: year, date: date)
54
+ return year, month unless month.invalid?
58
55
  end
59
56
 
60
57
  raise ArgumentError, "invalid year range. date: #{date.format}"
61
58
  end
62
- private_class_method :specify_year
63
-
64
- #
65
- # 改元する
66
- #
67
- # @param [Year] year 年
68
- # @param [Western::Calendar] date 西暦日
69
- #
70
- # @return [Year] 改元後の年
71
- #
72
- def self.transfer(year:, date:)
73
- multi_gengou = Calculation::Base::MultiGengouRoller.transfer(
74
- multi_gengou: year.multi_gengou, date: date
75
- )
76
- Calculation::Base::Year.new(
77
- multi_gengou: multi_gengou, new_year_date: year.new_year_date,
78
- months: year.months, total_days: year.total_days
79
- )
80
- end
81
- private_class_method :transfer
59
+ private_class_method :specify
82
60
 
83
61
  # :reek:TooManyStatements { max_statements: 7 }
84
62
 
@@ -93,14 +71,14 @@ module Zakuro
93
71
  def self.specify_month(year:, date:)
94
72
  months = year.months
95
73
 
96
- current_month = months[0]
97
74
  months.each do |month|
98
- return current_month if month.western_date > date
75
+ western_date = month.western_date
76
+ next if western_date.invalid?
99
77
 
100
- current_month = month
78
+ return month if month.include?(date: date)
101
79
  end
102
80
 
103
- current_month
81
+ Monthly::Month.new
104
82
  end
105
83
  private_class_method :specify_month
106
84
  end
@@ -32,11 +32,14 @@ module Zakuro
32
32
  years: years, date: date
33
33
  )
34
34
 
35
- operated_range = Calculation::Range::OperatedRange.new(context: context, years: years)
35
+ operated_range = Calculation::Range::OperatedRange.new(
36
+ context: context, start_date: date, years: years
37
+ )
38
+ operated_years = operated_range.get
36
39
 
37
40
  Result::Single.new(
38
41
  data: Calculation::Specifier::SingleDay.get(
39
- years: operated_range.get, date: date
42
+ years: operated_years, date: date
40
43
  ),
41
44
  operation: create_operation(calc_date: calc_date)
42
45
  )
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../era/japan/version'
4
+ require_relative './range'
5
+
6
+ # :nodoc:
7
+ module Zakuro
8
+ # :nodoc:
9
+ module Calculation
10
+ # :nodoc:
11
+ module Version
12
+ #
13
+ # 暦検索
14
+ #
15
+ module Crawler
16
+ #
17
+ # 暦の範囲を取得する
18
+ #
19
+ # @param [Integer] start_year 開始西暦年
20
+ # @param [Integer] end_year 終了西暦年
21
+ #
22
+ # @return [Array<Range>] 暦の範囲
23
+ #
24
+ def self.get(start_year:, end_year:)
25
+ ranges = Japan::Version.ranges_with_year(start_year: start_year, end_year: end_year)
26
+
27
+ result = []
28
+
29
+ ranges.each do |range|
30
+ next unless range.released
31
+
32
+ range_start_year = range.start_year
33
+ range_start_year = start_year if start_year > range.start_year
34
+
35
+ range_end_year = range.end_year
36
+ range_end_year = end_year if end_year < range.end_year
37
+
38
+ result.push(
39
+ Range.new(
40
+ name: range.name, start_date: range.start_date.clone,
41
+ start_year: range_start_year, end_year: range_end_year
42
+ )
43
+ )
44
+ end
45
+
46
+ result
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nodoc:
4
+ module Zakuro
5
+ # :nodoc:
6
+ module Calculation
7
+ # :nodoc:
8
+ module Version
9
+ #
10
+ # Range 暦(範囲)
11
+ #
12
+ class Range
13
+ # @return [String] 暦名
14
+ attr_reader :name
15
+ # @return [Western::Calendar] 暦の開始日
16
+ attr_reader :start_date
17
+ # @return [Integer] 開始西暦年
18
+ attr_reader :start_year
19
+ # @return [Integer] 終了西暦年
20
+ attr_reader :end_year
21
+
22
+ #
23
+ # 初期化
24
+ #
25
+ # @param [String] name 暦名
26
+ # @param [Western::Calendar] start_date 暦の開始日
27
+ # @param [Integer] start_year 開始西暦年
28
+ # @param [Integer] end_year 終了西暦年
29
+ #
30
+ def initialize(name:, start_date:, start_year:, end_year:)
31
+ @name = name
32
+ @start_date = start_date
33
+ @start_year = start_year
34
+ @end_year = end_year
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './internal/crawler'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ # :nodoc:
8
+ module Calculation
9
+ # :nodoc:
10
+ module Version
11
+ #
12
+ # 暦の範囲を取得する
13
+ #
14
+ # @param [Integer] start_year 開始西暦年
15
+ # @param [Integer] end_year 終了西暦年
16
+ #
17
+ # @return [Array<Range>] 暦の範囲
18
+ #
19
+ def self.get(start_year:, end_year:)
20
+ Crawler.get(start_year: start_year, end_year: end_year)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../tools/typeconv'
4
+
5
+ # :nodoc:
6
+ module Zakuro
7
+ #
8
+ # Japan 和暦
9
+ #
10
+ module Japan
11
+ #
12
+ # Calendar 年月日情報(和暦)
13
+ #
14
+ class Calendar
15
+ # @return [Integer] 無効値
16
+ INVALID = -1
17
+ # @return [String] 空文字列
18
+ EMPTY = ''
19
+ # @return [String] 閏を示す文字列
20
+ LEAPED_TEXT = '閏'
21
+ # @return [Regexp] 和暦日フォーマット
22
+ FORMAT = /^([一-龥]{2,4})([0-9]+)年(#{LEAPED_TEXT})?([0-9]+)月([0-9]+)日$/.freeze
23
+ # @return [String] 出力用デフォルトフォーマット
24
+ DEFAULT_OUTPUT_FORM = '%s%02d年%s%02d月%02d日'
25
+
26
+ # @return [String] 元号
27
+ attr_reader :gengou
28
+ # @return [Integer] 元号年
29
+ attr_reader :year
30
+ # @return [True] 閏あり
31
+ # @return [False] 閏なし
32
+ attr_reader :leaped
33
+ # @return [Integer] 月
34
+ attr_reader :month
35
+ # @return [Integer] 日
36
+ attr_reader :day
37
+
38
+ #
39
+ # 初期化
40
+ #
41
+ # @param [String] gengou 元号
42
+ # @param [Integer] year 元号年
43
+ # @param [True, False] leaped 閏
44
+ # @param [Integer] month 月
45
+ # @param [Integer] day 日
46
+ #
47
+ def initialize(gengou: EMPTY, year: INVALID, leaped: false, month: INVALID, day: INVALID)
48
+ @gengou = gengou
49
+ @year = year
50
+ @leaped = leaped
51
+ @month = month
52
+ @day = day
53
+ end
54
+
55
+ #
56
+ # 無効か
57
+ #
58
+ # @return [True] 無効
59
+ # @return [False] 有効
60
+ #
61
+ def invalid?
62
+ @gengou == EMPTY || @year == INVALID || @month == INVALID || @day == INVALID
63
+ end
64
+
65
+ #
66
+ # 文字列にする
67
+ #
68
+ # @return [String] 和暦日フォーマット文字列
69
+ #
70
+ def format(form: DEFAULT_OUTPUT_FORM)
71
+ leaped_text = @leaped ? LEAPED_TEXT : ''
72
+ super(form, @gengou, @year, leaped_text, @month, @day)
73
+ end
74
+
75
+ #
76
+ # 同月か
77
+ # 年と日は無視する
78
+ #
79
+ # @param [True, False] leaped 閏
80
+ # @param [Integer] month 月
81
+ #
82
+ # @return [True] 同月
83
+ # @return [True] 同月ではない
84
+ #
85
+ def same_month?(leaped:, month:)
86
+ return false unless @leaped == leaped
87
+
88
+ @month == month
89
+ end
90
+
91
+ #
92
+ # 年月日情報(和暦)を生成する
93
+ #
94
+ # @param [Regexp] regex 正規表現
95
+ # @param [String] text 和暦日文字列
96
+ #
97
+ # @return [Calendar] 年月日情報(和暦)
98
+ #
99
+ def self.parse(regex: FORMAT, text: '')
100
+ return Calendar.new unless valid_date_string(regex: regex, text: text)
101
+
102
+ matched = text.match(regex)
103
+
104
+ Calendar.new(
105
+ gengou: matched[1],
106
+ year: Tools::Typeconv.to_i(text: matched[2], default: INVALID),
107
+ leaped: matched[3] ? true : false,
108
+ month: Tools::Typeconv.to_i(text: matched[4], default: INVALID),
109
+ day: Tools::Typeconv.to_i(text: matched[5], default: INVALID)
110
+ )
111
+ end
112
+
113
+ #
114
+ # 日付文字列を検証する
115
+ #
116
+ # @param [Regexp] regex 正規表現
117
+ # @param [String] text 和暦日文字列
118
+ #
119
+ # @return [True] 正しい
120
+ # @return [True] 正しくない
121
+ #
122
+ def self.valid_date_string(regex: FORMAT, text: '')
123
+ return false unless text
124
+
125
+ matched = text.match(regex)
126
+
127
+ return false unless matched
128
+
129
+ matched.size == 6
130
+ end
131
+ end
132
+ end
133
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../western'
3
+ require_relative '../../western/calendar'
4
4
  require_relative './type'
5
5
  require_relative './validator'
6
6
  require 'yaml'
@@ -17,8 +17,6 @@ module Zakuro
17
17
  # Parser yaml解析
18
18
  #
19
19
  module Parser
20
- # :reek:TooManyInstanceVariables { max_instance_variables: 5 }
21
-
22
20
  #
23
21
  # GengouParser 元号情報の検証/展開を行う
24
22
  #
@@ -27,12 +25,10 @@ module Zakuro
27
25
  attr_reader :index
28
26
  # @return [String] 元号名
29
27
  attr_reader :name
30
- # @return [String] 開始日
31
- attr_reader :start_date
32
- # @return [String] 元旦
33
- attr_reader :new_year_date
34
- # @return [String] 開始年
35
- attr_reader :start_year
28
+ # @return [Hash<String, String>] 開始年
29
+ attr_reader :both_start_year
30
+ # @return [Hash<String, String>] 開始日
31
+ attr_reader :both_start_date
36
32
 
37
33
  #
38
34
  # 初期化
@@ -43,9 +39,8 @@ module Zakuro
43
39
  def initialize(hash:, index:)
44
40
  @index = index
45
41
  @name = hash['name']
46
- @start_date = hash['start_date']
47
- @new_year_date = hash['new_year_date']
48
- @start_year = hash['start_year']
42
+ @both_start_year = hash['start_year']
43
+ @both_start_date = hash['start_date']
49
44
  end
50
45
 
51
46
  #
@@ -54,12 +49,11 @@ module Zakuro
54
49
  # @return [Gengou] 元号情報
55
50
  #
56
51
  def create
57
- start_date = Western::Calendar.parse(str: @start_date)
58
- new_year_date = Western::Calendar.parse(str: @new_year_date)
59
- start_year = @start_year || 1
52
+ both_start_year = Both::YearParser.new(hash: @both_start_year).create
53
+ both_start_date = Both::DateParser.new(hash: @both_start_date).create
60
54
 
61
- Gengou.new(name: @name, start_date: start_date, new_year_date: new_year_date,
62
- year: start_year)
55
+ Gengou.new(name: @name, both_start_year: both_start_year,
56
+ both_start_date: both_start_date)
63
57
  end
64
58
  end
65
59
 
@@ -71,8 +65,10 @@ module Zakuro
71
65
  attr_reader :id
72
66
  # @return [String] 元号セット名
73
67
  attr_reader :name
74
- # @return [String] 終了日
75
- attr_reader :end_date
68
+ # @return [Hash<String, String>] 終了年
69
+ attr_reader :both_end_year
70
+ # @return [Hash<String, String>] 終了日
71
+ attr_reader :both_end_date
76
72
  # @return [Array<Hash<String, String>>] 元号情報
77
73
  attr_reader :list
78
74
 
@@ -84,7 +80,8 @@ module Zakuro
84
80
  def initialize(hash:)
85
81
  @id = hash['id']
86
82
  @name = hash['name']
87
- @end_date = hash['end_date']
83
+ @both_end_year = hash['end_year']
84
+ @both_end_date = hash['end_date']
88
85
  @list = hash['list']
89
86
  end
90
87
 
@@ -94,10 +91,10 @@ module Zakuro
94
91
  # @return [Set] 元号セット情報
95
92
  #
96
93
  def create
97
- end_date = Western::Calendar.parse(str: @end_date)
94
+ both_end_date = Both::DateParser.new(hash: @both_end_date).create
98
95
  list = create_list
99
96
  Set.new(
100
- id: @id, name: @name, end_date: end_date, list: list
97
+ id: @id, name: @name, both_end_date: both_end_date, list: list
101
98
  )
102
99
  end
103
100
 
@@ -133,18 +130,91 @@ module Zakuro
133
130
  #
134
131
  def calc_end_date_on_gengou_data(next_index:, gengou:)
135
132
  if next_index >= @list.size
136
- end_date = Western::Calendar.parse(str: @end_date)
133
+ gengou.write_end_year(end_year: @both_end_year['western'])
134
+ end_date = Western::Calendar.parse(str: @both_end_date['western'])
137
135
  gengou.write_end_date(end_date: end_date)
138
136
  return gengou
139
137
  end
140
- next_start_date = @list[next_index]['start_date']
138
+ next_item = @list[next_index]
139
+ gengou.convert_next_start_year_to_end_year(
140
+ next_start_year: next_item['start_year']['western']
141
+ )
141
142
  gengou.convert_next_start_date_to_end_date(
142
- next_start_date_string: next_start_date
143
+ next_start_date: next_item['start_date']['western']
143
144
  )
144
145
  gengou
145
146
  end
146
147
  end
147
148
 
149
+ #
150
+ # 和暦/西暦
151
+ #
152
+ module Both
153
+ #
154
+ # YearParser 年
155
+ #
156
+ class YearParser
157
+ # @return [Integer] 和暦元号年
158
+ attr_reader :japan
159
+ # @return [Integer] 西暦年
160
+ attr_reader :western
161
+
162
+ #
163
+ # 初期化
164
+ #
165
+ # @param [Hash<String, Object>] hash 年情報
166
+ #
167
+ def initialize(hash:)
168
+ @japan = hash['japan']
169
+ @western = hash['western']
170
+ end
171
+
172
+ #
173
+ # 年情報を生成する
174
+ #
175
+ # @return [Both::Year] 年情報
176
+ #
177
+ def create
178
+ japan = @japan.to_i
179
+ western = @western.to_i
180
+
181
+ Japan::Both::Year.new(japan: japan, western: western)
182
+ end
183
+ end
184
+
185
+ #
186
+ # DateParser 日
187
+ #
188
+ class DateParser
189
+ # @return [Japan::Calendar] 和暦日
190
+ attr_reader :japan
191
+ # @return [Western::Calendar] 西暦日
192
+ attr_reader :western
193
+
194
+ #
195
+ # 初期化
196
+ #
197
+ # @param [Hash<String, Object>] hash 日情報
198
+ #
199
+ def initialize(hash:)
200
+ @japan = hash['japan']
201
+ @western = hash['western']
202
+ end
203
+
204
+ #
205
+ # 日情報を生成する
206
+ #
207
+ # @return [Both::Date] 日情報
208
+ #
209
+ def create
210
+ japan = Japan::Calendar.parse(text: @japan)
211
+ western = Western::Calendar.parse(str: @western)
212
+
213
+ Japan::Both::Date.new(japan: japan, western: western)
214
+ end
215
+ end
216
+ end
217
+
148
218
  #
149
219
  # 解析/展開する
150
220
  #