Class: Lich::Common::PathNavigator

Inherits:
Object
  • Object
show all
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.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

Navigates to a specified path within the database structure.

Parameters:

  • script_name (String)

    the name of the script to navigate

  • create_missing (Boolean) (defaults to: true)

    whether to create missing path elements (default: true)

  • scope (String) (defaults to: ":")

    the scope for the navigation (default: ":")

  • path (Array<String>, nil) (defaults to: nil)

    the path to navigate to (default: current path)

Returns:

  • (Array<Object>)

    the target object at the end of the path and the root object



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_pathvoid

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.

Parameters:

  • value (Object)

    the value to return after resetting the path

Returns:

  • (Object)

    the provided 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.

Parameters:

Returns:

  • (Array<String>)

    the updated path



20
21
22
# File 'documented/common/settings/path_navigator.rb', line 20

def set_path(new_path)
  @path = Array(new_path).dup
end