Class: WorkspaceStorageFinder

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

Defined Under Namespace

Classes: Error, NotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ WorkspaceStorageFinder

Returns a new instance of WorkspaceStorageFinder.



8
9
10
# File 'lib/workspace_storage_finder.rb', line 8

def initialize(path:)
  @root = Pathname.new(path)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



33
34
35
# File 'lib/workspace_storage_finder.rb', line 33

def root
  @root
end

Instance Method Details

#find_path(needle:) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/workspace_storage_finder.rb', line 12

def find_path(needle:)
  needle = Pathname.new(needle).realpath.to_s
  worspace_storage_paths.each do |path|
    next unless path.exist?

    workspace_json_path = path.join('workspace.json')
    next unless workspace_json_path.exist?

    workspace_data = JSON.parse(workspace_json_path.read)
    folder_value = workspace_data['folder']
    return path if folder_value && folder_value == "file://#{needle}"
  end

  raise(NotFoundError, "Workspace storage not found for #{needle}. Searched in #{worspace_storage_paths.count} " \
    "folders within \"#{root}\".")
end

#worspace_storage_pathsObject



29
30
31
# File 'lib/workspace_storage_finder.rb', line 29

def worspace_storage_paths
  @worspace_storage_paths ||= root.children.select(&:directory?)
end