Sha256: 33e85276e9a659a13cf299b57406ccf2db5a9ec3f100f208c32712d5de6208cb

Contents?: true

Size: 863 Bytes

Versions: 8

Compression:

Stored size: 863 Bytes

Contents

# frozen_string_literal: true
# encoding: utf-8

module Unified
  class EntityMap
    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 [](type)
      @map[type]
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongo-2.16.4 spec/runners/unified/entity_map.rb
mongo-2.16.3 spec/runners/unified/entity_map.rb
mongo-2.16.2 spec/runners/unified/entity_map.rb
mongo-2.16.1 spec/runners/unified/entity_map.rb
mongo-2.16.0 spec/runners/unified/entity_map.rb
mongo-2.15.1 spec/runners/unified/entity_map.rb
mongo-2.16.0.alpha1 spec/runners/unified/entity_map.rb
mongo-2.15.0 spec/runners/unified/entity_map.rb