Sha256: a4564d95bf9cd9f7209c0646855328018d10ea601b1aade6388c380a47eb3919

Contents?: true

Size: 884 Bytes

Versions: 9

Compression:

Stored size: 884 Bytes

Contents

# frozen_string_literal: true
# rubocop:todo all

module Unified
  class EntityMap
    extend Forwardable

    def initialize
      @map = {}
    end

    def set(type, id, value)
      @map[type] ||= {}
      if @map[type][id]
        raise Error::EntityMapOverwriteAttempt,
          "Cannot set #{type} #{id} because it is already defined"
      end
      @map[type][id] = value
    end

    def get(type, id)
      unless @map[type]
        raise Error::EntityMissing, "There are no #{type} entities known"
      end
      unless v = @map[type][id]
        raise Error::EntityMissing, "There is no #{type} #{id} known"
      end
      v
    end

    def get_any(id)
      @map.each do |type, sub|
        if sub[id]
          return sub[id]
        end
      end
      raise Error::EntityMissing, "There is no #{id} known"
    end

    def_delegators :@map, :[], :fetch
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
mongo-2.21.0 spec/runners/unified/entity_map.rb
mongo-2.20.1 spec/runners/unified/entity_map.rb
mogno-2.20.0 spec/runners/unified/entity_map.rb
mogno-1.0.0 spec/runners/unified/entity_map.rb
mongo-2.20.0 spec/runners/unified/entity_map.rb
mongo-2.19.3 spec/runners/unified/entity_map.rb
mongo-2.19.2 spec/runners/unified/entity_map.rb
mongo-2.19.1 spec/runners/unified/entity_map.rb
mongo-2.19.0 spec/runners/unified/entity_map.rb