Class: Lich::Common::PathNavigator
- Inherits:
-
Object
- Object
- Lich::Common::PathNavigator
- Defined in:
- lib/common/settings/path_navigator.rb
Overview
Path navigator to encapsulate path navigation logic
Instance Attribute Summary collapse
-
#path ⇒ Array
readonly
Returns the current path.
Instance Method Summary collapse
-
#initialize(db_adapter) ⇒ PathNavigator
constructor
Initializes a new PathNavigator instance.
-
#navigate_to_path(script_name, create_missing = true, scope = ":") ⇒ Array
Navigates to a specified path based on the script name and scope.
-
#reset_path ⇒ void
Resets the path to an empty array.
-
#reset_path_and_return(value) ⇒ Object
Resets the path and returns the provided value.
Constructor Details
#initialize(db_adapter) ⇒ PathNavigator
Initializes a new PathNavigator instance.
8 9 10 11 |
# File 'lib/common/settings/path_navigator.rb', line 8 def initialize(db_adapter) @db_adapter = db_adapter @path = [] end |
Instance Attribute Details
#path ⇒ Array (readonly)
Returns the current path.
16 17 18 |
# File 'lib/common/settings/path_navigator.rb', line 16 def path @path end |
Instance Method Details
#navigate_to_path(script_name, create_missing = true, scope = ":") ⇒ Array
Navigates to a specified path based on the script name and scope.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/common/settings/path_navigator.rb', line 44 def navigate_to_path(script_name, create_missing = true, scope = ":") root = @db_adapter.get_settings(script_name, scope) return [root, root] if @path.empty? target = root @path.each do |key| if target.is_a?(Hash) && target.key?(key) target = target[key] elsif target.is_a?(Array) && key.is_a?(Integer) && key < target.length target = target[key] elsif create_missing # Path doesn't exist yet, create it target[key] = key.is_a?(Integer) ? [] : {} target = target[key] else # Path doesn't exist and we're not creating it return [nil, root] end end [target, root] end |
#reset_path ⇒ void
This method returns an undefined value.
Resets the path to an empty array.
21 22 23 |
# File 'lib/common/settings/path_navigator.rb', line 21 def reset_path @path = [] end |
#reset_path_and_return(value) ⇒ Object
Resets the path and returns the provided value.
29 30 31 32 |
# File 'lib/common/settings/path_navigator.rb', line 29 def reset_path_and_return(value) reset_path value end |