Sha256: b8c591a9addafbcb2542e68874a6df3e4f2324e2e784f098cfd6ce89a1f1d8a0

Contents?: true

Size: 965 Bytes

Versions: 5

Compression:

Stored size: 965 Bytes

Contents

module Eternity
  class CollectionTracker
    
    Changes = Restruct::NestedHash.new ObjectTracker

    extend Forwardable
    def_delegators :changes, :to_h, :to_primitive, :count, :[], :destroy

    def initialize(options)
      @changes = Changes.new options
    end

    def insert(id, data)
      changes[id].insert data
    end

    def update(id, data)
      changes[id].update data
    end

    def delete(id)
      changes[id].delete
    end

    def revert(id)
      changes[id].revert
    end

    def revert_all
      locker.lock! :revert_all do
        changes.destroy
      end
    end

    def flatten
      changes.each_with_object({}) do |(id, tracker), hash|
        change = tracker.flatten
        hash[id] = change if change
      end
    end

    private

    attr_reader :changes

    def locker
      Locky.new repository_name, Eternity.locker_storage
    end

    def repository_name
      changes.id.sections.reverse[2]
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
eternity-0.0.5 lib/eternity/collection_tracker.rb
eternity-0.0.4 lib/eternity/collection_tracker.rb
eternity-0.0.3 lib/eternity/collection_tracker.rb
eternity-0.0.2 lib/eternity/collection_tracker.rb
eternity-0.0.1 lib/eternity/collection_tracker.rb