Class: KeeperSecretsManager::FolderManager

Inherits:
Object
  • Object
show all
Defined in:
lib/keeper_secrets_manager/folder_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(folders) ⇒ FolderManager

Returns a new instance of FolderManager.



3
4
5
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 3

def initialize(folders)
  @folders = folders
end

Instance Method Details

#build_folder_treeObject

Build a hierarchical tree structure from flat folder list



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 8

def build_folder_tree
  # Create a hash for quick lookup
  folder_map = {}
  @folders.each { |f| folder_map[f.uid] = f }

  # Find root folders (no parent) and build tree
  root_folders = []
  @folders.each do |folder|
    root_folders << build_node(folder, folder_map) if folder.parent_uid.nil? || folder.parent_uid.empty?
  end

  root_folders
end

#find_folder_by_name(name, parent_uid: nil) ⇒ Object

Find folder by name (optionally within a parent)



71
72
73
74
75
76
77
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 71

def find_folder_by_name(name, parent_uid: nil)
  if parent_uid
    @folders.find { |f| f.name == name && f.parent_uid == parent_uid }
  else
    @folders.find { |f| f.name == name }
  end
end

#get_ancestors(folder_uid) ⇒ Object

Get all ancestors of a folder (parent, grandparent, etc.)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 40

def get_ancestors(folder_uid)
  ancestors = []
  folder = @folders.find { |f| f.uid == folder_uid }
  return ancestors unless folder

  current_parent_uid = folder.parent_uid
  while current_parent_uid && !current_parent_uid.empty?
    parent = @folders.find { |f| f.uid == current_parent_uid }
    break unless parent

    ancestors << parent
    current_parent_uid = parent.parent_uid
  end

  ancestors
end

#get_descendants(folder_uid) ⇒ Object

Get all descendants of a folder (children, grandchildren, etc.)



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 58

def get_descendants(folder_uid)
  descendants = []
  children = @folders.select { |f| f.parent_uid == folder_uid }

  children.each do |child|
    descendants << child
    descendants.concat(get_descendants(child.uid))
  end

  descendants
end

#get_folder_path(folder_uid) ⇒ Object

Get folder path from root to given folder



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 23

def get_folder_path(folder_uid)
  folder = @folders.find { |f| f.uid == folder_uid }
  return nil unless folder

  path = []
  current = folder

  # Walk up the tree
  while current
    path.unshift(current.name)
    current = @folders.find { |f| f.uid == current.parent_uid }
  end

  path.join('/')
end

Print folder tree to console



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/keeper_secrets_manager/folder_manager.rb', line 80

def print_tree(folders = nil, indent = 0)
  folders ||= build_folder_tree

  folders.each do |node|
    puts "#{' ' * indent}├── #{node[:folder].name} (#{node[:folder].uid})"
    if node[:folder].records && !node[:folder].records.empty?
      node[:folder].records.each do |record|
        puts "#{' ' * (indent + 4)}└─ #{record.title} (#{record.uid})"
      end
    end
    print_tree(node[:children], indent + 4) if node[:children]
  end
end