Sha256: f1fa2053f1c450b4dbe223579e85e98c5e071d65b6ab2a1e75bc2728cfbb0fc4

Contents?: true

Size: 818 Bytes

Versions: 19

Compression:

Stored size: 818 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.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 { |h| ::OpenStruct.new(h) }
        @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

19 entries across 19 versions & 1 rubygems

Version Path
ehbrs-tools-0.19.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.18.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.17.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.16.5 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.16.4 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.16.3 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.16.2 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.16.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.16.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.15.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.14.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.14.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.13.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.13.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.12.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.11.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.10.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.9.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.7.0 lib/ehbrs/observers/with_persistence.rb