Sha256: def1835d95a9c06c15249430c372e634b0c2678cc4e7772283db250277333695

Contents?: true

Size: 812 Bytes

Versions: 2

Compression:

Stored size: 812 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(value, date)
      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

2 entries across 2 versions & 1 rubygems

Version Path
ehbrs-tools-0.39.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.39.0 lib/ehbrs/observers/with_persistence.rb