class Index
Represents an index of contexts by their identity keys.
Definitions
def initialize
Initialize a new Index.
Implementation
def initialize
@contexts = {}
end
attr :contexts
Signature
-
attribute
Hash A hash mapping identity keys to contexts.
def add(parent)
Add all children from a parent context to the index.
Signature
-
parameter
parentObject The parent context.
Implementation
def add(parent)
parent.children&.each do |identity, child|
insert(identity, child)
add(child)
end
end
def insert(identity, context)
Insert a context into the index.
Signature
-
parameter
identityIdentity The identity of the context.
-
parameter
contextObject The context to index.
-
raises
KeyError If a context with the same key already exists.
Implementation
def insert(identity, context)
key = identity.key
if existing_context = @contexts[key]
raise KeyError, "Assigning context to existing key: #{key.inspect}!"
else
@contexts[key] = context
end
end
def [](key)
Look up a context by its key.
Signature
-
parameter
keyString The identity key.
-
returns
Object, nil The context if found.
Implementation
def [] key
@contexts[key]
end