Class: Lich::Common::PathNavigator
- Inherits:
-
Object
- Object
- Lich::Common::PathNavigator
- Defined in:
- documented/common/settings/path_navigator.rb
Overview
Manages navigation through a path structure in a database.
This class provides methods to set, reset, and navigate through paths.
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>
Navigates to a specified path within the database structure.
-
#reset_path ⇒ void
Resets the current path to an empty array.
-
#reset_path_and_return(value) ⇒ Object
Resets the current path and returns the specified value.
-
#set_path(new_path) ⇒ Array<String>
Sets the current path to the specified value.
Constructor Details
#initialize(db_adapter) ⇒ PathNavigator
Returns a new instance of PathNavigator.
10 11 12 13 |
# File 'documented/common/settings/path_navigator.rb', line 10 def initialize(db_adapter) @db_adapter = db_adapter @path = [] end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
15 16 17 |
# File 'documented/common/settings/path_navigator.rb', line 15 def path @path end |
Instance Method Details
#navigate_to_path(script_name, create_missing = true, scope = ":", path = nil) ⇒ Array<Object>
Navigates to a specified path within the database structure.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'documented/common/settings/path_navigator.rb', line 44 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 array.
26 27 28 |
# File 'documented/common/settings/path_navigator.rb', line 26 def reset_path @path = [] end |
#reset_path_and_return(value) ⇒ Object
Resets the current path and returns the specified value.
33 34 35 36 |
# File 'documented/common/settings/path_navigator.rb', line 33 def reset_path_and_return(value) reset_path value end |
#set_path(new_path) ⇒ Array<String>
Sets the current path to the specified value.
20 21 22 |
# File 'documented/common/settings/path_navigator.rb', line 20 def set_path(new_path) @path = Array(new_path).dup end |