# frozen_string_literal: true module Crys class PagesDbManager PAGES_KEY = :pages PAGE = Struct.new(:filename, :file_path, :last_update, :in_rss, keyword_init: true) def db_path; end def add_page(processor) @processor = processor hash = Digest::MD5.hexdigest(processor.file_path) known_page = known_page(hash) upsert_db(hash, known_page) save_db end def pages db.fetch(:pages).map do |_, data| PAGE.new(data) end end private attr_reader :hash, :processor def db @db ||= YAML.load_file( db_path, permitted_classes: [Time, Symbol] ) end def upsert_db(hash, known_page) record = PAGE.new(known_page || { filename: processor.filename, file_path: processor.file_path, in_rss: true }) record.last_update = Time.now @db[PAGES_KEY][hash] = record.to_h end def save_db File.write(db_path, db.to_yaml) end def known_page(hash) db[PAGES_KEY][hash] end end end