zakuro 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,6 +25,8 @@ module Zakuro
25
25
  MONTH_HISTORIES
26
26
  end
27
27
 
28
+ # :reek:ControlParameter
29
+
28
30
  #
29
31
  # 変更履歴を特定する
30
32
  #
@@ -41,5 +43,24 @@ module Zakuro
41
43
 
42
44
  Operation::MonthHistory.new
43
45
  end
46
+
47
+ # :reek:ControlParameter
48
+
49
+ #
50
+ # 変更履歴を特定する
51
+ #
52
+ # @param [Western::Calendar] id 変更履歴ID
53
+ #
54
+ # @return [Operation::MonthHistory] 変更履歴
55
+ #
56
+ def self.specify_history_by_id(id:)
57
+ month_histroies = Operation.month_histories
58
+
59
+ month_histroies.each do |history|
60
+ return history if id == history.id
61
+ end
62
+
63
+ Operation::MonthHistory.new
64
+ end
44
65
  end
45
66
  end
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  - id: 156-1-a
3
3
  relation_id: "-"
4
- parent_id: "-"
4
+ parent_id: 156-1-1
5
5
  page: '156'
6
6
  number: "-"
7
7
  japan_date: 貞観 14年 12 大 丁酉 33-1832
@@ -17,7 +17,7 @@ module Zakuro
17
17
  # @return [True] 運用あり
18
18
  # @return [False] 運用なし
19
19
  attr_reader :operated
20
- # @return [Month] 月別履歴情報
20
+ # @return [Month::Bundle] 月別履歴情報
21
21
  attr_reader :month
22
22
  # @return [Data::SingleDay] 計算値
23
23
  attr_reader :original
@@ -26,7 +26,7 @@ module Zakuro
26
26
  # 初期化
27
27
  #
28
28
  # @param [True, False] operated 運用有無
29
- # @param [Month] month 月別履歴情報
29
+ # @param [Month::Bundle] month 月別履歴情報
30
30
  # @param [Data::SingleDay] original 計算値
31
31
  #
32
32
  def initialize(operated:, month:, original:)
@@ -37,48 +37,76 @@ module Zakuro
37
37
  end
38
38
 
39
39
  #
40
- # Month 月別履歴情報
40
+ # Month 運用情報(月)
41
41
  #
42
- class Month
43
- # @return [Integer] 原文頁数
44
- attr_reader :page
45
- # @return [Integer] 原文注釈番号
46
- attr_reader :number
47
- # @return [Array<Zakuro::Result::Operation::Annotation>] 注釈
48
- attr_reader :annotations
49
-
50
- #
51
- # 初期化
42
+ module Month
52
43
  #
53
- # @param [Integer] page 原文頁数
54
- # @param [Integer] number 原文注釈番号
55
- # @param [Array<Zakuro::Result::Operation::Annotation>] annotations 注釈
44
+ # Bundle 月履歴集約情報
56
45
  #
57
- def initialize(page: -1, number: -1, annotations: [])
58
- @page = page
59
- @number = number
60
- @annotations = annotations
61
- end
62
- end
46
+ class Bundle
47
+ # @return [History] 月別履歴情報(当月)
48
+ attr_reader :current
49
+ # @return [History] 月別履歴情報(影響を与えた月)
50
+ attr_reader :parent
63
51
 
64
- #
65
- # Annotation 注釈
66
- #
67
- class Annotation
68
- # @return [String] 注釈内容
69
- attr_reader :description
70
- # @return [String] 注釈補記
71
- attr_reader :note
52
+ def initialize(current: History.new, parent: History.new)
53
+ @current = current
54
+ @parent = parent
55
+ end
56
+ end
72
57
 
73
58
  #
74
- # 初期化
59
+ # History 月別履歴情報
75
60
  #
76
- # @param [String] description 注釈内容
77
- # @param [<Type>] note 注釈補記
61
+ class History
62
+ # @return [String] ID
63
+ attr_reader :id
64
+ # @return [String] 月初日の西暦日
65
+ attr_reader :western_date
66
+ # @return [Integer] 原文頁数
67
+ attr_reader :page
68
+ # @return [Integer] 原文注釈番号
69
+ attr_reader :number
70
+ # @return [Array<Zakuro::Result::Operation::Annotation>] 注釈
71
+ attr_reader :annotations
72
+
73
+ #
74
+ # 初期化
75
+ #
76
+ # @param [String] id ID
77
+ # @param [String] western_date 月初日の西暦日
78
+ # @param [Integer] page 原文頁数
79
+ # @param [Integer] number 原文注釈番号
80
+ # @param [Array<Annotation>] annotations 注釈
81
+ #
82
+ def initialize(id: '', western_date: '', page: -1, number: -1, annotations: [])
83
+ @id = id
84
+ @western_date = western_date
85
+ @page = page
86
+ @number = number
87
+ @annotations = annotations
88
+ end
89
+ end
90
+
91
+ #
92
+ # Annotation 注釈
78
93
  #
79
- def initialize(description:, note:)
80
- @description = description
81
- @note = note
94
+ class Annotation
95
+ # @return [String] 注釈内容
96
+ attr_reader :description
97
+ # @return [String] 注釈補記
98
+ attr_reader :note
99
+
100
+ #
101
+ # 初期化
102
+ #
103
+ # @param [String] description 注釈内容
104
+ # @param [<Type>] note 注釈補記
105
+ #
106
+ def initialize(description:, note:)
107
+ @description = description
108
+ @note = note
109
+ end
82
110
  end
83
111
  end
84
112
  end
@@ -31,7 +31,7 @@ module Zakuro
31
31
  hash
32
32
  end
33
33
 
34
- # :reek:TooManyStatements { max_statements: 7 } and :reek:NilCheck
34
+ # :reek:TooManyStatements { max_statements: 7 }
35
35
 
36
36
  #
37
37
  # 対象インスタンスをハッシュ化する(再帰処理)
@@ -42,7 +42,7 @@ module Zakuro
42
42
  # @return [Hash<String, Objcet>] ハッシュ
43
43
  #
44
44
  def self.value_to_hash(obj:, class_prefix:, formatted:)
45
- return obj if obj.nil?
45
+ return obj unless obj
46
46
 
47
47
  # 日付をフォーマットする
48
48
  return obj.format if formatted && Tools::Typeof.time?(obj: obj)
@@ -3,5 +3,5 @@
3
3
  # :nodoc:
4
4
  module Zakuro
5
5
  # @return [String] library version
6
- VERSION = '0.1.0'
6
+ VERSION = '0.1.1'
7
7
  end
@@ -71,6 +71,16 @@ module Zakuro
71
71
  def empty?
72
72
  (@index == -1 && @remainder.invalid?)
73
73
  end
74
+
75
+ #
76
+ # ディープコピー
77
+ #
78
+ # @param [SolarTerm] obj 自身
79
+ #
80
+ def initialize_copy(obj)
81
+ @index = obj.index.clone
82
+ @remainder = obj.remainder.clone
83
+ end
74
84
  end
75
85
  end
76
86
  end
@@ -48,7 +48,9 @@ module Zakuro
48
48
  operated_years = []
49
49
 
50
50
  years.each do |year|
51
- operated_year = rewrite_year(year: year)
51
+ operated_year = OperatedRange.rewrite_year(
52
+ year: year, operated_solar_terms: @operated_solar_terms
53
+ )
52
54
  operated_years.push(operated_year)
53
55
  end
54
56
 
@@ -59,16 +61,16 @@ module Zakuro
59
61
  # 年を書き換える
60
62
  #
61
63
  # @param [Year] year 年
64
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
62
65
  #
63
66
  # @return [Year] 年
64
67
  #
65
- def rewrite_year(year:)
68
+ def self.rewrite_year(year:, operated_solar_terms:)
66
69
  result = Year.new(multi_gengou: year.multi_gengou, new_year_date: year.new_year_date)
67
70
  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)
71
+ result.push(month: resolve_month(
72
+ month: month, operated_solar_terms: operated_solar_terms
73
+ ))
72
74
  end
73
75
 
74
76
  result.commit
@@ -76,21 +78,40 @@ module Zakuro
76
78
  result
77
79
  end
78
80
 
81
+ #
82
+ # 履歴情報の有無に応じた月にする
83
+ #
84
+ # @param [Month] month 月
85
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
86
+ #
87
+ # @return [Month] 月
88
+ #
89
+ def self.resolve_month(month:, operated_solar_terms:)
90
+ history = Operation.specify_history(western_date: month.western_date)
91
+
92
+ return month if history.invalid?
93
+
94
+ OperatedRange.rewrite_month(
95
+ month: month, history: history, operated_solar_terms: operated_solar_terms
96
+ )
97
+ end
98
+
79
99
  #
80
100
  # 月を運用結果に書き換える
81
101
  #
82
102
  # @param [Month] month 月
83
103
  # @param [Operation::MonthHistory] history 変更履歴
104
+ # @param [OperatedSolarTerms] operated_solar_terms 運用時二十四節気
84
105
  #
85
106
  # @return [Month] 月(運用結果)
86
107
  #
87
- def rewrite_month(month:, history:)
108
+ def self.rewrite_month(month:, history:, operated_solar_terms:)
88
109
  return month unless month.western_date == history.western_date
89
110
 
90
111
  operated_month = OperatedMonth.new(
91
112
  month_label: month.month_label, first_day: month.first_day,
92
113
  solar_terms: month.solar_terms, history: history,
93
- operated_solar_terms: @operated_solar_terms
114
+ operated_solar_terms: operated_solar_terms
94
115
  )
95
116
 
96
117
  operated_month.rewrite
@@ -16,8 +16,6 @@ module Zakuro
16
16
  class OperatedSolarTerms
17
17
  # @return [Array<Year>] 完全範囲(年データ)
18
18
  attr_reader :years
19
- # @return [Array<MonthHistory>] 変更履歴
20
- attr_reader :month_histroies
21
19
  # @return [Hash<String, SolarTerm>] 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
22
20
  #
23
21
  # * 移動元の二十四節気:無効な大余小余あり(削除対象)
@@ -29,11 +27,9 @@ module Zakuro
29
27
  # 初期化
30
28
  #
31
29
  # @param [Array<Year>] years 完全範囲(年データ)
32
- # @param [Array<MonthHistory>] 変更履歴
33
30
  #
34
- def initialize(years: [], month_histories: Operation.month_histories)
31
+ def initialize(years: [])
35
32
  @years = years
36
- @month_histroies = month_histories
37
33
  @directions = {}
38
34
  end
39
35
 
@@ -75,31 +71,35 @@ module Zakuro
75
71
  directions = {}
76
72
 
77
73
  years.each do |year|
78
- create_directions_with_months(directions: directions, months: year.months)
74
+ OperatedSolarTerms.create_directions_with_months(
75
+ directions: directions, months: year.months
76
+ )
79
77
  end
80
78
 
81
79
  directions
82
80
  end
83
81
 
82
+ # :reek:TooManyStatements { max_statements: 6 }
83
+
84
84
  #
85
85
  # 年内の全ての月の移動方向を作成する
86
86
  #
87
87
  # @param [Hash<String, SolarTerm>] directions 二十四節気の移動元/移動先(西暦日 -> 対応する二十四節気)
88
88
  # @param [Array<Month>] months 年内の全ての月
89
89
  #
90
- def create_directions_with_months(directions: {}, months: [])
90
+ def self.create_directions_with_months(directions: {}, months: [])
91
91
  months.each do |month|
92
- @month_histroies.each do |history|
93
- next unless month.western_date == history.western_date
92
+ history = Operation.specify_history(western_date: month.western_date)
93
+
94
+ next if history.invalid?
94
95
 
95
- direction = history.diffs.solar_term
96
+ direction = history.diffs.solar_term
96
97
 
97
- next if direction.invalid?
98
+ next if direction.invalid?
98
99
 
99
- OperatedSolarTerms.create_directions_each_month(
100
- directions: directions, direction: direction, month: month
101
- )
102
- end
100
+ OperatedSolarTerms.create_directions_each_month(
101
+ directions: directions, direction: direction, month: month
102
+ )
103
103
  end
104
104
  end
105
105
 
@@ -114,7 +114,6 @@ module Zakuro
114
114
  direction: Operation::SolarTerm::Diretion.new,
115
115
  month: Month.new)
116
116
 
117
- # TDOO: 移動だけではなく大余差分を反映する場合
118
117
  month.solar_terms.each do |solar_term|
119
118
  OperatedSolarTerms.push_source(directions: directions, direction: direction,
120
119
  solar_term: solar_term)
@@ -138,13 +137,32 @@ module Zakuro
138
137
 
139
138
  return unless source.index == solar_term.index
140
139
 
140
+ # 移動先に移動元の二十四節気を指定する
141
+ directions[source.to.format] = OperatedSolarTerms.created_source(
142
+ direction: direction, solar_term: solar_term
143
+ )
144
+ end
145
+
146
+ #
147
+ # 移動先に有効な二十四節気(差し替える二十四節気)を生成する
148
+ #
149
+ # @param [Operation::SolarTerm::Direction] source 二十四節気(移動)
150
+ # @param [SolarTerm] solar_term 二十四節気(計算値)
151
+ #
152
+ # @return [SolarTerm] 二十四節気(運用値)
153
+ #
154
+ def self.created_source(direction: Operation::SolarTerm::Direction.new,
155
+ solar_term: SolarTerm.new)
156
+ operated_solar_term = solar_term.clone
157
+
141
158
  unless direction.invalid_days?
142
159
  # 二十四節気の大余をずらす
143
- solar_term.remainder.add!(Remainder.new(day: direction.days, minute: 0, second: 0))
160
+ operated_solar_term.remainder.add!(
161
+ Remainder.new(day: direction.days, minute: 0, second: 0)
162
+ )
144
163
  end
145
164
 
146
- # 移動先に移動元の二十四節気を指定する
147
- directions[source.to.format] = solar_term
165
+ operated_solar_term
148
166
  end
149
167
 
150
168
  #
@@ -57,6 +57,7 @@ module Zakuro
57
57
 
58
58
  raise ArgumentError, "invalid year range. date: #{date.format}"
59
59
  end
60
+ private_class_method :specify_year
60
61
 
61
62
  #
62
63
  # 改元する
@@ -71,6 +72,7 @@ module Zakuro
71
72
  Year.new(multi_gengou: multi_gengou, new_year_date: year.new_year_date,
72
73
  months: year.months, total_days: year.total_days)
73
74
  end
75
+ private_class_method :transfer
74
76
 
75
77
  # :reek:TooManyStatements { max_statements: 7 }
76
78
 
@@ -94,6 +96,7 @@ module Zakuro
94
96
 
95
97
  current_month
96
98
  end
99
+ private_class_method :specify_month
97
100
  end
98
101
  end
99
102
  end
@@ -35,37 +35,91 @@ module Zakuro
35
35
 
36
36
  operated_range = OperatedRange.new(years: years)
37
37
 
38
- actual_date = SingleDaySpecifier.get(years: operated_range.get, date: date)
38
+ Result::Single.new(
39
+ data: SingleDaySpecifier.get(years: operated_range.get, date: date),
40
+ operation: create_operation(calc_date: calc_date)
41
+ )
42
+ end
39
43
 
44
+ #
45
+ # 運用情報を生成する
46
+ #
47
+ # @param [Result::Data::SingleDay] calc_date 和暦日(計算値)
48
+ #
49
+ # @return [Result::Operation::Bundle] 運用情報
50
+ #
51
+ def self.create_operation(calc_date: Result::Data::SingleDay.new)
40
52
  first_day = calc_date.month.first_day.western_date
41
53
  operation_history = Operation.specify_history(western_date: first_day)
42
54
 
43
- operation_month = Single.create_operation_month(operation_history: operation_history)
55
+ operation_month = create_operation_month(operation_history: operation_history)
44
56
 
45
- Result::Single.new(data: actual_date, operation: Result::Operation::Bundle.new(
57
+ Result::Operation::Bundle.new(
46
58
  operated: !operation_history.invalid?, month: operation_month, original: calc_date
47
- ))
59
+ )
48
60
  end
61
+ private_class_method :create_operation
49
62
 
63
+ #
64
+ # 月履歴集約情報を生成する
65
+ #
66
+ # @param [Operation::MonthHistory] operation_history 変更履歴(月)
67
+ #
68
+ # @return [Result::Operation::Month::Bundle] 月履歴集約情報
69
+ #
50
70
  def self.create_operation_month(operation_history: Operation::MonthHistory.new)
51
- # TODO: MonthHistory が持つ親IDとの紐付け
52
- return Result::Operation::Month.new if operation_history.invalid?
71
+ return Result::Operation::Month::Bundle.new if operation_history.invalid?
72
+
73
+ parent_operation_history = Operation.specify_history_by_id(id: operation_history.parent_id)
74
+
75
+ Result::Operation::Month::Bundle.new(
76
+ current: create_operation_month_history(operation_history: operation_history),
77
+ parent: create_operation_month_history(operation_history: parent_operation_history)
78
+ )
79
+ end
80
+ private_class_method :create_operation_month
53
81
 
82
+ #
83
+ # 月別履歴情報を生成する
84
+ #
85
+ # @param [Operation::MonthHistory] operation_history 変更履歴(月)
86
+ #
87
+ # @return [Result::Operation::Month::History] 月別履歴情報
88
+ #
89
+ def self.create_operation_month_history(operation_history: Operation::MonthHistory.new)
90
+ return Result::Operation::Month::History.new if operation_history.invalid?
91
+
92
+ annotations = create_annnotations(operation_history: operation_history)
93
+
94
+ reference = operation_history.reference
95
+ Result::Operation::Month::History.new(
96
+ id: operation_history.id, western_date: operation_history.western_date.format,
97
+ page: reference.page, number: reference.number, annotations: annotations
98
+ )
99
+ end
100
+ private_class_method :create_operation_month_history
101
+
102
+ #
103
+ # 注釈情報を生成する
104
+ #
105
+ # @param [Operation::MonthHistory] operation_history 変更履歴(月)
106
+ #
107
+ # @return [Array<Result::Operation::Month::Annotation>] 注釈
108
+ #
109
+ def self.create_annnotations(operation_history: Operation::MonthHistory.new)
54
110
  annotations = []
55
111
  operation_history.annotations.each do |annotation|
56
112
  annotations.push(
57
- Result::Operation::Annotation.new(
113
+ Result::Operation::Month::Annotation.new(
58
114
  description: annotation.description,
59
115
  note: annotation.note
60
116
  )
61
117
  )
62
118
  end
63
119
 
64
- reference = operation_history.reference
65
- Result::Operation::Month.new(
66
- page: reference.page, number: reference.number, annotations: annotations
67
- )
120
+ annotations
68
121
  end
122
+ private_class_method :create_annnotations
69
123
  end
70
124
  end
71
125
  end