Class: Navigation::Registry
- Inherits:
-
Object
- Object
- Navigation::Registry
- Includes:
- Singleton
- Defined in:
- lib/navigation/registry.rb
Constant Summary collapse
- DEFAULT_PRIORITY =
500- DEFAULT_SECTION =
:main
Instance Method Summary collapse
-
#add_child(parent_key, child) ⇒ Object
Add a child item to a parent.
-
#all ⇒ Object
Get all items, sorted by priority.
-
#find(key) ⇒ Object
Get a specific item by key.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#items_for_section(section) ⇒ Object
Get all items for a specific section, sorted by priority.
-
#modify(key, **changes) ⇒ Object
Modify an existing item.
-
#register(item) ⇒ Object
Register a new navigation item or replace an existing one with the same key.
-
#reset! ⇒ Object
Clear all items (useful for testing or reloading).
-
#unregister(key) ⇒ Object
Remove an item by key.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
10 11 12 13 |
# File 'lib/navigation/registry.rb', line 10 def initialize @items = [] @mutex = Mutex.new end |
Instance Method Details
#add_child(parent_key, child) ⇒ Object
Add a child item to a parent
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/navigation/registry.rb', line 46 def add_child(parent_key, child) child = normalize_item(child) validate_item!(child) @mutex.synchronize do parent = @items.find { |i| i[:key] == parent_key.to_sym } raise ArgumentError, "Parent navigation item not found: #{parent_key}" unless parent parent[:children] ||= [] # Remove existing child with same key if present parent[:children].reject! { |c| c[:key] == child[:key] } parent[:children] << child parent[:children].sort_by! { |c| c[:priority] } end child end |
#all ⇒ Object
Get all items, sorted by priority
73 74 75 76 77 |
# File 'lib/navigation/registry.rb', line 73 def all @mutex.synchronize do @items.sort_by { |i| i[:priority] } end end |
#find(key) ⇒ Object
Get a specific item by key
80 81 82 83 84 |
# File 'lib/navigation/registry.rb', line 80 def find(key) @mutex.synchronize do @items.find { |i| i[:key] == key.to_sym } end end |
#items_for_section(section) ⇒ Object
Get all items for a specific section, sorted by priority
64 65 66 67 68 69 70 |
# File 'lib/navigation/registry.rb', line 64 def items_for_section(section) @mutex.synchronize do @items .select { |i| i[:section] == section.to_sym } .sort_by { |i| i[:priority] } end end |
#modify(key, **changes) ⇒ Object
Modify an existing item
36 37 38 39 40 41 42 43 |
# File 'lib/navigation/registry.rb', line 36 def modify(key, **changes) @mutex.synchronize do item = @items.find { |i| i[:key] == key.to_sym } raise ArgumentError, "Navigation item not found: #{key}" unless item item.merge!(changes) end end |
#register(item) ⇒ Object
Register a new navigation item or replace an existing one with the same key
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/navigation/registry.rb', line 16 def register(item) item = normalize_item(item) validate_item!(item) @mutex.synchronize do # Remove existing item with same key if present @items.reject! { |i| i[:key] == item[:key] } @items << item end item end |
#reset! ⇒ Object
Clear all items (useful for testing or reloading)
87 88 89 90 91 |
# File 'lib/navigation/registry.rb', line 87 def reset! @mutex.synchronize do @items = [] end end |
#unregister(key) ⇒ Object
Remove an item by key
29 30 31 32 33 |
# File 'lib/navigation/registry.rb', line 29 def unregister(key) @mutex.synchronize do @items.reject! { |i| i[:key] == key.to_sym } end end |