Skip to content

Commit 8cb9c76

Browse files
committed
feat: YAMLキー順序をid優先に変更
- YAML出力のキー順序を id, url, title, published_at に統一 - データの識別性と可読性を向上 - 明示的なハッシュ構築による順序制御を実装 - 既存システムとの互換性は完全に維持 Before: - url: https://... title: ... published_at: '...' id: 147 After: - id: 147 url: https://... title: ... published_at: '...'
1 parent cec41d2 commit 8cb9c76

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/tasks/news.rake

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,17 @@ namespace :news do
9797
items_by_oldest = items.sort_by { |item| item['published_at'] }
9898
items_by_oldest.each.with_index(1) { |item, index| item['id'] = index }
9999

100-
# 4. 最新順にソートして YAML ファイルに書き出す
101-
File.open(NEWS_YAML_PATH, 'w') { it.write(items_by_oldest.reverse.to_yaml) }
100+
# 4. 最新順にソートして YAML ファイルに書き出す(キー順序: id, url, title, published_at)
101+
File.open(NEWS_YAML_PATH, 'w') do |file|
102+
file.write(items_by_oldest.reverse.map do |item|
103+
{
104+
'id' => item['id'],
105+
'url' => item['url'],
106+
'title' => item['title'],
107+
'published_at' => item['published_at']
108+
}
109+
end.to_yaml)
110+
end
102111

103112
TASK_LOGGER.info("✅ 合計 #{items_by_oldest.size} 件を news.yml に保存しました")
104113
TASK_LOGGER.info("📌 次は 'bundle exec rails news:upsert' でデータベースに反映してください")

0 commit comments

Comments
 (0)