Class: Sus::Filter::Index
- Inherits:
-
Object
- Object
- Sus::Filter::Index
- Defined in:
- lib/sus/filter.rb
Overview
Represents an index of contexts by their identity keys.
Instance Attribute Summary collapse
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Look up a context by its key.
- #A hash mapping identity keys to contexts.=(hashmappingidentitykeystocontexts. = (value)) ⇒ Object
-
#add(parent) ⇒ Object
Add all children from a parent context to the index.
-
#initialize ⇒ Index
constructor
Initialize a new Index.
-
#insert(identity, context) ⇒ Object
Insert a context into the index.
Constructor Details
#initialize ⇒ Index
Initialize a new Index.
18 19 20 |
# File 'lib/sus/filter.rb', line 18 def initialize @contexts = {} end |
Instance Attribute Details
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
23 24 25 |
# File 'lib/sus/filter.rb', line 23 def contexts @contexts end |
Instance Method Details
#[](key) ⇒ Object
Look up a context by its key.
51 52 53 |
# File 'lib/sus/filter.rb', line 51 def [] key @contexts[key] end |
#A hash mapping identity keys to contexts.=(hashmappingidentitykeystocontexts. = (value)) ⇒ Object
23 |
# File 'lib/sus/filter.rb', line 23 attr :contexts |
#add(parent) ⇒ Object
Add all children from a parent context to the index.
27 28 29 30 31 32 |
# File 'lib/sus/filter.rb', line 27 def add(parent) parent.children&.each do |identity, child| insert(identity, child) add(child) end end |
#insert(identity, context) ⇒ Object
Insert a context into the index.
38 39 40 41 42 43 44 45 46 |
# File 'lib/sus/filter.rb', line 38 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 |