Sha256: 1d1c8e5359b6facfc9f5e4fdce377cce5d512a5f0d93696f9bf95e81e7e21ef6

Contents?: true

Size: 1.94 KB

Versions: 8

Compression:

Stored size: 1.94 KB

Contents

module Imap::Backup
  class Mirror; end

  class Mirror::Map
    attr_reader :pathname
    attr_reader :destination

    def initialize(pathname:, destination:)
      @pathname = pathname
      @destination = destination
      @store = nil
      @destination_store = nil
      @source_uid_validity = nil
      @destination_uid_validity = nil
      @map = nil
    end

    def check_uid_validities(source:, destination:)
      store
      return false if source != source_uid_validity
      return false if destination != destination_uid_validity

      true
    end

    def reset(source_uid_validity:, destination_uid_validity:)
      destination_store["source_uid_validity"] = source_uid_validity
      @source_uid_validity = nil
      destination_store["destination_uid_validity"] = destination_uid_validity
      @destination_uid_validity = nil
      destination_store["map"] = {}
      @map = nil
    end

    def source_uid(destination_uid)
      map.key(destination_uid)
    end

    def destination_uid(source_uid)
      map[source_uid]
    end

    def map_uids(source:, destination:)
      map[source] = destination
    end

    def save
      File.write(pathname, store.to_json)
    end

    private

    def store
      @store ||=
        if File.exist?(pathname)
          content = File.read(pathname)
          store = JSON.parse(content)
          if store[destination]
            destination_store = store[destination]
            map = destination_store["map"]
            map.transform_keys!(&:to_i)
          end
          store
        else
          {}
        end
    end

    def destination_store
      @destination_store ||= store[destination] ||= {}
    end

    def source_uid_validity
      @source_uid_validity ||= destination_store["source_uid_validity"]
    end

    def destination_uid_validity
      @destination_uid_validity ||= destination_store["destination_uid_validity"]
    end

    def map
      @map ||= destination_store["map"]
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
imap-backup-9.0.1.rc1 lib/imap/backup/mirror/map.rb
imap-backup-9.0.0 lib/imap/backup/mirror/map.rb
imap-backup-9.0.0.rc1 lib/imap/backup/mirror/map.rb
imap-backup-8.0.2 lib/imap/backup/mirror/map.rb
imap-backup-8.0.1 lib/imap/backup/mirror/map.rb
imap-backup-8.0.0 lib/imap/backup/mirror/map.rb
imap-backup-8.0.0.rc1 lib/imap/backup/mirror/map.rb
imap-backup-7.0.2 lib/imap/backup/mirror/map.rb