zakuro 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +85 -43
  3. data/doc/operation.md +25 -0
  4. data/doc/operation/csv/month.csv +202 -0
  5. data/doc/operation/operation.xlsx +0 -0
  6. data/doc/operation/transfer.rb +73 -0
  7. data/lib/zakuro/era/japan/gengou.rb +106 -0
  8. data/lib/zakuro/era/japan/gengou/parser.rb +169 -0
  9. data/lib/zakuro/era/japan/gengou/type.rb +178 -0
  10. data/lib/zakuro/era/japan/gengou/validator.rb +234 -0
  11. data/lib/zakuro/era/japan/reki.rb +91 -0
  12. data/lib/zakuro/era/{gengou → japan/yaml}/set-001-until-south.yaml +0 -0
  13. data/lib/zakuro/era/{gengou → japan/yaml}/set-002-from-north.yaml +0 -0
  14. data/lib/zakuro/era/{gengou → japan/yaml}/set-003-modern.yaml +0 -0
  15. data/lib/zakuro/era/western.rb +1 -1
  16. data/lib/zakuro/operation/month/parser.rb +277 -0
  17. data/lib/zakuro/operation/month/type.rb +452 -0
  18. data/lib/zakuro/operation/month/validator.rb +498 -0
  19. data/lib/zakuro/operation/operation.rb +45 -0
  20. data/lib/zakuro/operation/yaml/month.yaml +6452 -0
  21. data/lib/zakuro/output/error.rb +2 -0
  22. data/lib/zakuro/output/logger.rb +2 -0
  23. data/lib/zakuro/output/response.rb +17 -15
  24. data/lib/zakuro/result/core.rb +52 -0
  25. data/lib/zakuro/result/data.rb +187 -0
  26. data/lib/zakuro/result/operation.rb +86 -0
  27. data/lib/zakuro/result/result.rb +37 -0
  28. data/lib/zakuro/{output → tools}/stringifier.rb +14 -7
  29. data/lib/zakuro/tools/typeof.rb +33 -0
  30. data/lib/zakuro/version.rb +1 -1
  31. data/lib/zakuro/version/senmyou/base/era.rb +1 -1
  32. data/lib/zakuro/version/senmyou/base/multi_gengou.rb +1 -1
  33. data/lib/zakuro/version/senmyou/base/multi_gengou_roller.rb +13 -1
  34. data/lib/zakuro/version/senmyou/base/solar_term.rb +10 -0
  35. data/lib/zakuro/version/senmyou/monthly/first_day.rb +44 -0
  36. data/lib/zakuro/version/senmyou/monthly/initialized_month.rb +48 -0
  37. data/lib/zakuro/version/senmyou/monthly/month.rb +127 -68
  38. data/lib/zakuro/version/senmyou/monthly/month_label.rb +87 -0
  39. data/lib/zakuro/version/senmyou/monthly/operated_month.rb +167 -0
  40. data/lib/zakuro/version/senmyou/{summary → range}/annual_range.rb +20 -23
  41. data/lib/zakuro/version/senmyou/{summary → range}/full_range.rb +102 -2
  42. data/lib/zakuro/version/senmyou/range/operated_range.rb +105 -0
  43. data/lib/zakuro/version/senmyou/range/operated_solar_terms.rb +163 -0
  44. data/lib/zakuro/version/senmyou/senmyou.rb +2 -2
  45. data/lib/zakuro/version/senmyou/{summary/specifier.rb → specifier/single_day_specifier.rb} +13 -14
  46. data/lib/zakuro/version/senmyou/stella/solar_average.rb +3 -7
  47. data/lib/zakuro/version/senmyou/summary/single.rb +71 -0
  48. data/lib/zakuro/version_factory.rb +1 -1
  49. metadata +35 -11
  50. data/lib/zakuro/era/japan.rb +0 -664
  51. data/lib/zakuro/output/result.rb +0 -225
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './full_range'
4
+ require_relative './operated_solar_terms'
5
+ require_relative '../../../operation/operation'
6
+ require_relative '../monthly/operated_month'
7
+
8
+ # :nodoc:
9
+ module Zakuro
10
+ # :nodoc:
11
+ module Senmyou
12
+ #
13
+ # OperatedRange 運用結果範囲
14
+ #
15
+ # 何らかの理由により、計算された暦とは異なる運用結果である場合、その結果に合わせて計算結果を上書きする
16
+ class OperatedRange
17
+ # @return [Array<Year>] 年データ(完全範囲)
18
+ attr_reader :years
19
+ # @return [OperatedSolarTerms] 運用時二十四節気
20
+ attr_reader :operated_solar_terms
21
+
22
+ #
23
+ # 初期化
24
+ #
25
+ # @param [Array<Year>] years 年データ(完全範囲)
26
+ #
27
+ def initialize(years: [])
28
+ @years = years
29
+ @operated_solar_terms = OperatedSolarTerms.new(years: @years)
30
+ @operated_solar_terms.create
31
+ end
32
+
33
+ #
34
+ # 運用結果範囲を取得する
35
+ #
36
+ # @return [Array<Year>] 運用結果範囲
37
+ #
38
+ def get
39
+ rewrite
40
+ end
41
+
42
+ #
43
+ # 運用結果に書き換える
44
+ #
45
+ # @return [Array<Year>] 運用結果範囲
46
+ #
47
+ def rewrite
48
+ operated_years = []
49
+
50
+ years.each do |year|
51
+ operated_year = rewrite_year(year: year)
52
+ operated_years.push(operated_year)
53
+ end
54
+
55
+ operated_years
56
+ end
57
+
58
+ #
59
+ # 年を書き換える
60
+ #
61
+ # @param [Year] year 年
62
+ #
63
+ # @return [Year] 年
64
+ #
65
+ def rewrite_year(year:)
66
+ result = Year.new(multi_gengou: year.multi_gengou, new_year_date: year.new_year_date)
67
+ year.months.each do |month|
68
+ operated_month = month
69
+ history = Operation.specify_history(western_date: month.western_date)
70
+ operated_month = rewrite_month(month: month, history: history) unless history.invalid?
71
+ result.push(month: operated_month)
72
+ end
73
+
74
+ result.commit
75
+
76
+ result
77
+ end
78
+
79
+ #
80
+ # 月を運用結果に書き換える
81
+ #
82
+ # @param [Month] month 月
83
+ # @param [Operation::MonthHistory] history 変更履歴
84
+ #
85
+ # @return [Month] 月(運用結果)
86
+ #
87
+ def rewrite_month(month:, history:)
88
+ return month unless month.western_date == history.western_date
89
+
90
+ operated_month = OperatedMonth.new(
91
+ month_label: month.month_label, first_day: month.first_day,
92
+ solar_terms: month.solar_terms, history: history,
93
+ operated_solar_terms: @operated_solar_terms
94
+ )
95
+
96
+ operated_month.rewrite
97
+
98
+ Month.new(
99
+ month_label: operated_month.month_label, first_day: operated_month.first_day,
100
+ solar_terms: operated_month.solar_terms
101
+ )
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './full_range'
4
+ require_relative '../../../operation/operation'
5
+ require_relative '../monthly/month'
6
+
7
+ # TODO: solar_termの中はsrc/destだが、yamlはcalc/actual。統一したい。
8
+
9
+ # :nodoc:
10
+ module Zakuro
11
+ # :nodoc:
12
+ module Senmyou
13
+ #
14
+ # OperatedSolarTerm 運用時二十四節気
15
+ #
16
+ class OperatedSolarTerms
17
+ # @return [Array<Year>] 完全範囲(年データ)
18
+ attr_reader :years
19
+ # @return [Array<MonthHistory>] 変更履歴
20
+ attr_reader :month_histroies
21
+ # @return [Hash<String, SolarTerm>] 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
22
+ #
23
+ # * 移動元の二十四節気:無効な大余小余あり(削除対象)
24
+ # * 移動先の二十四節気:移動元からの二十四節気(追加対象)
25
+ #
26
+ attr_reader :directions
27
+
28
+ #
29
+ # 初期化
30
+ #
31
+ # @param [Array<Year>] years 完全範囲(年データ)
32
+ # @param [Array<MonthHistory>] 変更履歴
33
+ #
34
+ def initialize(years: [], month_histories: Operation.month_histories)
35
+ @years = years
36
+ @month_histroies = month_histories
37
+ @directions = {}
38
+ end
39
+
40
+ #
41
+ # データ生成する
42
+ #
43
+ # @return [<Type>] <description>
44
+ #
45
+ def create
46
+ @directions = create_directions
47
+ end
48
+
49
+ #
50
+ # 二十四節気を取得する
51
+ #
52
+ # @param [Western::Calendar] western_date 月初日の西暦日
53
+ #
54
+ # @return [True] 対象あり
55
+ # @return [False] 対象なし
56
+ # @return [SolarTerm] 二十四節気
57
+ #
58
+ def get(western_date: Western::Calendar.new)
59
+ solar_term = @directions.fetch(western_date.format, SolarTerm.new)
60
+
61
+ # 合致しない場合
62
+ return false, SolarTerm.new if solar_term.empty?
63
+
64
+ # 合致した上で、二十四節気が移動元(削除対象)の場合
65
+ # 合致した上で、二十四節気が移動先(追加対象)の場合
66
+ [true, solar_term]
67
+ end
68
+
69
+ #
70
+ # 二十四節気の移動元/移動先を生成する
71
+ #
72
+ # @return [Hash<String, SolarTerm>] 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
73
+ #
74
+ def create_directions
75
+ directions = {}
76
+
77
+ years.each do |year|
78
+ create_directions_with_months(directions: directions, months: year.months)
79
+ end
80
+
81
+ directions
82
+ end
83
+
84
+ #
85
+ # 年内の全ての月の移動方向を作成する
86
+ #
87
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
88
+ # @param [Array<Month>] months 年内の全ての月
89
+ #
90
+ def create_directions_with_months(directions: {}, months: [])
91
+ months.each do |month|
92
+ @month_histroies.each do |history|
93
+ next unless month.western_date == history.western_date
94
+
95
+ direction = history.diffs.solar_term
96
+
97
+ next if direction.invalid?
98
+
99
+ OperatedSolarTerms.create_directions_each_month(
100
+ directions: directions, direction: direction, month: month
101
+ )
102
+ end
103
+ end
104
+ end
105
+
106
+ #
107
+ # 月毎の移動方向を作成する
108
+ #
109
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
110
+ # @param [Operation::SolarTerm::Diretion] 二十四節気(移動)
111
+ # @param [Month] 月
112
+ #
113
+ def self.create_directions_each_month(directions: {},
114
+ direction: Operation::SolarTerm::Diretion.new,
115
+ month: Month.new)
116
+
117
+ # TDOO: 移動だけではなく大余差分を反映する場合
118
+ month.solar_terms.each do |solar_term|
119
+ OperatedSolarTerms.push_source(directions: directions, direction: direction,
120
+ solar_term: solar_term)
121
+ end
122
+ OperatedSolarTerms.push_destination(directions: directions,
123
+ destination: direction.destination)
124
+ end
125
+
126
+ #
127
+ # 移動先に有効な二十四節気(差し替える二十四節気)を指定する
128
+ #
129
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
130
+ # @param [Operation::SolarTerm::Direction] source 二十四節気(移動)
131
+ # @param [SolarTerm] solar_term 二十四節気(計算値)
132
+ #
133
+ def self.push_source(directions: {}, direction: Operation::SolarTerm::Direction.new,
134
+ solar_term: SolarTerm.new)
135
+ source = direction.source
136
+
137
+ return if source.invalid?
138
+
139
+ return unless source.index == solar_term.index
140
+
141
+ unless direction.invalid_days?
142
+ # 二十四節気の大余をずらす
143
+ solar_term.remainder.add!(Remainder.new(day: direction.days, minute: 0, second: 0))
144
+ end
145
+
146
+ # 移動先に移動元の二十四節気を指定する
147
+ directions[source.to.format] = solar_term
148
+ end
149
+
150
+ #
151
+ # 移動元に無効な二十四節気(連番のみ指定)を指定する
152
+ #
153
+ # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
154
+ # @param [Operation::SolarTerm::Destination] destination 二十四節気(移動先)
155
+ #
156
+ def self.push_destination(directions: {}, destination: Operation::SolarTerm::Destination.new)
157
+ return if destination.invalid?
158
+
159
+ directions[destination.from.format] = SolarTerm.new(index: destination.index)
160
+ end
161
+ end
162
+ end
163
+ end
@@ -3,7 +3,7 @@
3
3
  require 'date'
4
4
  require_relative '../abstract_version'
5
5
  require_relative '../../era/western'
6
- require_relative 'summary/specifier'
6
+ require_relative 'summary/single'
7
7
 
8
8
  # :nodoc:
9
9
  module Zakuro
@@ -27,7 +27,7 @@ module Zakuro
27
27
  #
28
28
  def self.to_japan_date(western_date:)
29
29
  date = Western::Calendar.create(date: western_date)
30
- SingleDaySpecifier.get(date: date)
30
+ Single.get(date: date)
31
31
  end
32
32
  end
33
33
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../../era/western'
4
- require_relative './full_range'
4
+ require_relative '../range/full_range'
5
5
  require_relative '../base/multi_gengou_roller'
6
6
  require_relative '../base/year'
7
7
  require_relative '../../../output/response'
@@ -21,18 +21,18 @@ module Zakuro
21
21
  #
22
22
  # 取得する
23
23
  #
24
+ # @param [Array<Year>] yeas 範囲
24
25
  # @param [Western::Calendar] date 西暦日
25
26
  #
26
- # @return [Response::SingleDay] 和暦日
27
+ # @return [Result::Data::SingleDay] 和暦日
27
28
  #
28
- def self.get(date:)
29
- years = FullRange.new(start_date: date).get
30
-
29
+ def self.get(years: [], date: Western::Calendar.new)
31
30
  year = specify_year(years: years, date: date)
32
31
 
33
32
  year = transfer(year: year, date: date)
34
33
 
35
- month, first_date = specify_month(year: year, date: date)
34
+ month = specify_month(year: year, date: date)
35
+ first_date = month.western_date
36
36
 
37
37
  Response::SingleDay.save_single_day(
38
38
  param: Response::SingleDay::Param.new(
@@ -81,19 +81,18 @@ module Zakuro
81
81
  # @param [Western::Calendar] date 西暦日
82
82
  #
83
83
  # @return [Month] 対象月
84
- # @return [Western::Calendar] 月初日
85
84
  #
86
85
  def self.specify_month(year:, date:)
87
- current_month_date = year.new_year_date.clone
88
- next_month_date = current_month_date.clone
89
- year.months.each do |month|
90
- next_month_date += month.days
91
- return month, current_month_date if next_month_date > date
86
+ months = year.months
87
+
88
+ current_month = months[0]
89
+ months.each do |month|
90
+ return current_month if month.western_date > date
92
91
 
93
- current_month_date = next_month_date.clone
92
+ current_month = month
94
93
  end
95
94
 
96
- raise ArgumentError, "invalid month range. date: #{date.format}"
95
+ current_month
97
96
  end
98
97
  end
99
98
  end
@@ -202,13 +202,9 @@ module Zakuro
202
202
  #
203
203
  def self.set_solar_term(month:, solar_term:, solar_term_index:)
204
204
  term = SolarTerm.new(remainder: solar_term, index: solar_term_index)
205
- if solar_term_index.even?
206
- # 中気
207
- month.even_term = term
208
- else
209
- # 節気
210
- month.odd_term = term
211
- end
205
+
206
+ # 設定する
207
+ month.add_term(term: term)
212
208
 
213
209
  nil
214
210
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('../../../../../' \
4
+ 'lib/zakuro/version/senmyou/specifier/single_day_specifier',
5
+ __dir__)
6
+
7
+ require File.expand_path('../../../../../' \
8
+ 'lib/zakuro/version/senmyou/range/full_range',
9
+ __dir__)
10
+
11
+ require File.expand_path('../../../../../' \
12
+ 'lib/zakuro/version/senmyou/range/operated_range',
13
+ __dir__)
14
+
15
+ # :nodoc:
16
+ module Zakuro
17
+ # :nodoc:
18
+ module Senmyou
19
+ #
20
+ # Single 一日
21
+ #
22
+ module Single
23
+ #
24
+ # 生成する
25
+ #
26
+ # @param [Western::Calendar] date 西暦日
27
+ #
28
+ # @return [Result::Single] 一日検索結果(和暦日)
29
+ #
30
+ def self.get(date: Western::Calendar.new)
31
+ full_range = FullRange.new(start_date: date)
32
+ years = full_range.get
33
+
34
+ calc_date = SingleDaySpecifier.get(years: years, date: date)
35
+
36
+ operated_range = OperatedRange.new(years: years)
37
+
38
+ actual_date = SingleDaySpecifier.get(years: operated_range.get, date: date)
39
+
40
+ first_day = calc_date.month.first_day.western_date
41
+ operation_history = Operation.specify_history(western_date: first_day)
42
+
43
+ operation_month = Single.create_operation_month(operation_history: operation_history)
44
+
45
+ Result::Single.new(data: actual_date, operation: Result::Operation::Bundle.new(
46
+ operated: !operation_history.invalid?, month: operation_month, original: calc_date
47
+ ))
48
+ end
49
+
50
+ def self.create_operation_month(operation_history: Operation::MonthHistory.new)
51
+ # TODO: MonthHistory が持つ親IDとの紐付け
52
+ return Result::Operation::Month.new if operation_history.invalid?
53
+
54
+ annotations = []
55
+ operation_history.annotations.each do |annotation|
56
+ annotations.push(
57
+ Result::Operation::Annotation.new(
58
+ description: annotation.description,
59
+ note: annotation.note
60
+ )
61
+ )
62
+ end
63
+
64
+ reference = operation_history.reference
65
+ Result::Operation::Month.new(
66
+ page: reference.page, number: reference.number, annotations: annotations
67
+ )
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './era/japan'
3
+ require_relative './era/japan/reki'
4
4
  require_relative './era/western'
5
5
  require_relative './version/genka/genka'
6
6
  require_relative './version/gihou/gihou'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zakuro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pldb
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-30 00:00:00.000000000 Z
11
+ date: 2021-04-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: mainly lunar solar calendar
14
14
  email:
@@ -29,6 +29,10 @@ files:
29
29
  - bin/console
30
30
  - bin/setup
31
31
  - doc/gengou.md
32
+ - doc/operation.md
33
+ - doc/operation/csv/month.csv
34
+ - doc/operation/operation.xlsx
35
+ - doc/operation/transfer.rb
32
36
  - images/12月以降の入定気.png
33
37
  - images/12月以降の月の進退.png
34
38
  - images/source/description.numbers
@@ -39,17 +43,30 @@ files:
39
43
  - lib/zakuro/condition.rb
40
44
  - lib/zakuro/cycle/abstract_remainder.rb
41
45
  - lib/zakuro/cycle/zodiac.rb
42
- - lib/zakuro/era/gengou/set-001-until-south.yaml
43
- - lib/zakuro/era/gengou/set-002-from-north.yaml
44
- - lib/zakuro/era/gengou/set-003-modern.yaml
45
- - lib/zakuro/era/japan.rb
46
+ - lib/zakuro/era/japan/gengou.rb
47
+ - lib/zakuro/era/japan/gengou/parser.rb
48
+ - lib/zakuro/era/japan/gengou/type.rb
49
+ - lib/zakuro/era/japan/gengou/validator.rb
50
+ - lib/zakuro/era/japan/reki.rb
51
+ - lib/zakuro/era/japan/yaml/set-001-until-south.yaml
52
+ - lib/zakuro/era/japan/yaml/set-002-from-north.yaml
53
+ - lib/zakuro/era/japan/yaml/set-003-modern.yaml
46
54
  - lib/zakuro/era/western.rb
47
55
  - lib/zakuro/merchant.rb
56
+ - lib/zakuro/operation/month/parser.rb
57
+ - lib/zakuro/operation/month/type.rb
58
+ - lib/zakuro/operation/month/validator.rb
59
+ - lib/zakuro/operation/operation.rb
60
+ - lib/zakuro/operation/yaml/month.yaml
48
61
  - lib/zakuro/output/error.rb
49
62
  - lib/zakuro/output/logger.rb
50
63
  - lib/zakuro/output/response.rb
51
- - lib/zakuro/output/result.rb
52
- - lib/zakuro/output/stringifier.rb
64
+ - lib/zakuro/result/core.rb
65
+ - lib/zakuro/result/data.rb
66
+ - lib/zakuro/result/operation.rb
67
+ - lib/zakuro/result/result.rb
68
+ - lib/zakuro/tools/stringifier.rb
69
+ - lib/zakuro/tools/typeof.rb
53
70
  - lib/zakuro/version.rb
54
71
  - lib/zakuro/version/abstract_version.rb
55
72
  - lib/zakuro/version/genka/genka.rb
@@ -65,16 +82,23 @@ files:
65
82
  - lib/zakuro/version/senmyou/base/remainder.rb
66
83
  - lib/zakuro/version/senmyou/base/solar_term.rb
67
84
  - lib/zakuro/version/senmyou/base/year.rb
85
+ - lib/zakuro/version/senmyou/monthly/first_day.rb
86
+ - lib/zakuro/version/senmyou/monthly/initialized_month.rb
68
87
  - lib/zakuro/version/senmyou/monthly/lunar_phase.rb
69
88
  - lib/zakuro/version/senmyou/monthly/month.rb
89
+ - lib/zakuro/version/senmyou/monthly/month_label.rb
90
+ - lib/zakuro/version/senmyou/monthly/operated_month.rb
91
+ - lib/zakuro/version/senmyou/range/annual_range.rb
92
+ - lib/zakuro/version/senmyou/range/full_range.rb
93
+ - lib/zakuro/version/senmyou/range/operated_range.rb
94
+ - lib/zakuro/version/senmyou/range/operated_solar_terms.rb
70
95
  - lib/zakuro/version/senmyou/senmyou.rb
96
+ - lib/zakuro/version/senmyou/specifier/single_day_specifier.rb
71
97
  - lib/zakuro/version/senmyou/stella/lunar_orbit.rb
72
98
  - lib/zakuro/version/senmyou/stella/solar_average.rb
73
99
  - lib/zakuro/version/senmyou/stella/solar_orbit.rb
74
100
  - lib/zakuro/version/senmyou/stella/winter_solstice.rb
75
- - lib/zakuro/version/senmyou/summary/annual_range.rb
76
- - lib/zakuro/version/senmyou/summary/full_range.rb
77
- - lib/zakuro/version/senmyou/summary/specifier.rb
101
+ - lib/zakuro/version/senmyou/summary/single.rb
78
102
  - lib/zakuro/version/taien/taien.rb
79
103
  - lib/zakuro/version/tenpou/tenpou.rb
80
104
  - lib/zakuro/version_factory.rb