Class: Lich::Common::PathNavigator
- Inherits:
-
Object
- Object
- Lich::Common::PathNavigator
- Defined in:
- documented/common/settings/path_navigator.rb
Overview
Path navigator to encapsulate path navigation logic
This class provides methods to manage and navigate through a path structure. It interacts with a database adapter to retrieve and manipulate settings.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(db_adapter) ⇒ PathNavigator
constructor
A new instance of PathNavigator.
-
#navigate_to_path(script_name, create_missing = true, scope = ":", path = nil) ⇒ Array(Object, Hash|Array)
Navigate to the node at “path” (or @path if nil).
-
#reset_path ⇒ void
Resets the current path to an empty state.
-
#reset_path_and_return(value) ⇒ Object
Resets the path and returns a specified value.
-
#set_path(new_path) ⇒ void
Allow external callers to drive the effective path.
Constructor Details
#initialize(db_adapter) ⇒ PathNavigator
Returns a new instance of PathNavigator.
12 13 14 15 |
# File 'documented/common/settings/path_navigator.rb', line 12 def initialize(db_adapter) @db_adapter = db_adapter @path = [] end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'documented/common/settings/path_navigator.rb', line 17 def path @path end |
Instance Method Details
#navigate_to_path(script_name, create_missing = true, scope = ":", path = nil) ⇒ Array(Object, Hash|Array)
Navigate to the node at “path” (or @path if nil). Returns [target, root]
This method navigates through the path segments to find the target node. It can create missing containers if specified.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'documented/common/settings/path_navigator.rb', line 65 def navigate_to_path(script_name, create_missing = true, scope = ":", path = nil) work_path = path ? Array(path) : @path root = @db_adapter.get_settings(script_name, scope) return [root, root] if work_path.empty? target = root work_path.each_with_index do |key, idx| next_key = work_path[idx + 1] if target.is_a?(Hash) if target.key?(key) target = target[key] elsif create_missing target[key] = next_key.is_a?(Integer) ? [] : {} target = target[key] else return [nil, root] end elsif target.is_a?(Array) unless key.is_a?(Integer) && key >= 0 return [nil, root] unless create_missing raise ArgumentError, "Array index must be a non-negative Integer (got: #{key.inspect})" end if key >= target.length (target.length..key).each { target << nil } end if target[key].nil? && create_missing target[key] = next_key.is_a?(Integer) ? [] : {} end return [nil, root] if target[key].nil? && !create_missing target = target[key] else # Non-container encountered mid-path; only replace if allowed. return [nil, root] unless create_missing replacement = next_key.is_a?(Integer) ? [] : {} target = replacement end end [target, root] end |
#reset_path ⇒ void
This method returns an undefined value.
Resets the current path to an empty state.
This method clears the existing path.
36 37 38 |
# File 'documented/common/settings/path_navigator.rb', line 36 def reset_path @path = [] end |
#reset_path_and_return(value) ⇒ Object
Resets the path and returns a specified value.
This method clears the current path and returns the provided value.
47 48 49 50 |
# File 'documented/common/settings/path_navigator.rb', line 47 def reset_path_and_return(value) reset_path value end |
#set_path(new_path) ⇒ void
This method returns an undefined value.
Allow external callers to drive the effective path
This method sets the current path to the provided new path.
26 27 28 |
# File 'documented/common/settings/path_navigator.rb', line 26 def set_path(new_path) @path = Array(new_path).dup end |