Sha256: fcb81bb3fb5598001d86ac25ab71f227eda3e7a1884558ebafe808eecbb4af15

Contents?: true

Size: 827 Bytes

Versions: 26

Compression:

Stored size: 827 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 { |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

26 entries across 26 versions & 1 rubygems

Version Path
ehbrs-tools-0.38.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.37.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.36.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.35.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.35.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.34.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.33.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.32.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.31.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.31.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.30.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.29.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.28.3 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.28.2 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.28.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.28.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.27.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.26.0 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.25.1 lib/ehbrs/observers/with_persistence.rb
ehbrs-tools-0.25.0 lib/ehbrs/observers/with_persistence.rb