Sha256: fbbb3c0f58dda4e0a45de2bf5165e1a45e2df8bdde314d74dacd7964a70ad3e7

Contents?: true

Size: 799 Bytes

Versions: 3

Compression:

Stored size: 799 Bytes

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'
require 'ehbrs/observers/base'

module Ehbrs
  module Observers
    class WithPersistence < ::Ehbrs::Observers::Base
      attr_reader :path

      def initialize(path, options = {})
        super(options)
        @path = path.to_pathname
        load
      end

      def check(value, date = ::Time.required_zone.now)
        save if super
      end

      def load
        save unless path.exist?
        data = ::YAML.load_file(path.to_path)
        @records = data.fetch(:records).map(&:to_struct)
        @last_check_time = data.fetch(:last_check_time)
      end

      def save
        path.parent.mkpath
        path.write({ records: records.map(&:to_h), last_check_time: last_check_time }.to_yaml)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ehbrs-tools-0.43.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.43.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.42.0 lib/ehbrs/observers/with_persistence.rb