Sha256: dfc6313071a0f724dd6170684b564c8f0aaf7151838e3fab14093a3619d0dab8

Contents?: true

Size: 883 Bytes

Versions: 10

Compression:

Stored size: 883 Bytes

Contents

# frozen_string_literal: true
# encoding: utf-8

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

10 entries across 10 versions & 1 rubygems

Version Path
mongo-2.17.4 spec/runners/unified/entity_map.rb
mongo-2.18.3 spec/runners/unified/entity_map.rb
mongo-2.18.2 spec/runners/unified/entity_map.rb
mongo-2.17.3 spec/runners/unified/entity_map.rb
mongo-2.18.1 spec/runners/unified/entity_map.rb
mongo-2.18.0 spec/runners/unified/entity_map.rb
mongo-2.17.2 spec/runners/unified/entity_map.rb
mongo-2.18.0.beta1 spec/runners/unified/entity_map.rb
mongo-2.17.1 spec/runners/unified/entity_map.rb
mongo-2.17.0 spec/runners/unified/entity_map.rb