Module: DataPaths::Finders

Includes:
Enumerable
Defined in:
lib/data_paths/finders.rb

Instance Method Summary collapse

Instance Method Details

#all_data_dirs(path) ⇒ Array<String>

Finds all occurrences of a given directory path, within all data directories.

Parameters:

  • path (String)

    The directory path to search for.

Returns:

  • (Array<String>)

    The occurrences of the given directory path within all data directories.



171
172
173
# File 'lib/data_paths/finders.rb', line 171

def all_data_dirs(path)
  enum_for(:each_data_dir,path).to_a
end

#all_data_files(path) ⇒ Array<String>

Finds all occurrences of a given file path, within all data directories.

Parameters:

  • path (String)

    The file path to search for.

Returns:

  • (Array<String>)

    The occurrences of the given file path within all data directories.



133
134
135
# File 'lib/data_paths/finders.rb', line 133

def all_data_files(path)
  enum_for(:each_data_file,path).to_a
end

#all_data_paths(path) ⇒ Array<String>

Finds all occurrences of a given path, within all data directories.

Parameters:

  • path (String)

    The path to search for.

Returns:

  • (Array<String>)

    The occurrences of the given path within all data directories.



94
95
96
# File 'lib/data_paths/finders.rb', line 94

def all_data_paths(path)
  enum_for(:each_data_path,path).to_a
end

#data_glob(pattern) ⇒ Array<String>

Finds all paths that match a given pattern, within all data directories.

Parameters:

  • pattern (String)

    The path glob pattern to search with.

Returns:

  • (Array<String>)

    The matching paths found within all data directories.



185
186
187
188
189
190
191
192
193
# File 'lib/data_paths/finders.rb', line 185

def data_glob(pattern)
  paths = []

  DataPaths.paths.each do |path|
    paths += Dir[File.join(path,pattern)]
  end

  return paths
end

#each_data_dir(path) {|data_dir| ... } ⇒ Array<String>

Finds all occurrences of a given directory path, within all data directories.

Parameters:

  • path (String)

    The directory path to search for.

Yields:

  • (data_dir)

    If a block is given, it will be passed every found path.

Yield Parameters:

  • data_dir (String)

    The path of a directory within a data directory.

Returns:

  • (Array<String>)

    The occurrences of the given directory path within all data directories.



154
155
156
157
158
# File 'lib/data_paths/finders.rb', line 154

def each_data_dir(path,&block)
  each_data_path(path) do |full_path|
    block.call(full_path) if File.directory?(full_path)
  end
end

#each_data_file(path) {|data_file| ... } ⇒ Array<String>

Finds all occurrences of a given file path, within all data directories.

Parameters:

  • path (String)

    The file path to search for.

Yields:

  • (data_file)

    If a block is given, it will be passed every found path.

Yield Parameters:

  • data_file (String)

    The path of a file within a data directory.

Returns:

  • (Array<String>)

    The occurrences of the given file path within all data directories.



115
116
117
118
119
# File 'lib/data_paths/finders.rb', line 115

def each_data_file(path,&block)
  each_data_path(path) do |full_path|
    block.call(full_path) if File.file?(full_path)
  end
end

#each_data_path(path) {|potential_path| ... } ⇒ Object

Passes all existing data paths for the specified path, within the data directories, to the given block.

Parameters:

  • path (String)

    The path to search for in all data directories.

Yields:

  • (potential_path)

    The given block will be passed every existing combination of the given path and the data directories.

Yield Parameters:

  • potential_path (String)

    An existing data path.



23
24
25
26
27
28
29
# File 'lib/data_paths/finders.rb', line 23

def each_data_path(path,&block)
  DataPaths.paths.each do |dir|
    full_path = File.join(dir,path)

    block.call(full_path) if File.exists?(full_path)
  end
end

#find_data_dir(path) ⇒ String?

Searches for a directory at the given path, within any data directory.

Parameters:

  • path (String)

    The directory path to search for.

Returns:

  • (String, nil)

    Returns the first valid directory at the given path within a data directory. Returns nil if the given path could not be found in any data directory.



77
78
79
80
81
82
83
# File 'lib/data_paths/finders.rb', line 77

def find_data_dir(path)
  each_data_path(path) do |full_path|
    return full_path if File.directory?(full_path)
  end

  return nil
end

#find_data_file(path) ⇒ String?

Searches for a file at the given path, within any data directory.

Parameters:

  • path (String)

    The file path to search for.

Returns:

  • (String, nil)

    Returns the first valid file at the given path within a data directory. Returns nil if the given path could not be found in any data directory.



57
58
59
60
61
62
63
# File 'lib/data_paths/finders.rb', line 57

def find_data_file(path)
  each_data_path(path) do |full_path|
    return full_path if File.file?(full_path)
  end

  return nil
end

#find_data_path(path) ⇒ String?

Searches for the given path within any data directory.

Parameters:

  • path (String)

    The path to search for.

Returns:

  • (String, nil)

    Returns the first valid match for the given path within a data directory. Returns nil if the given path could not be found in any data directory.



42
43
44
# File 'lib/data_paths/finders.rb', line 42

def find_data_path(path)
  enum_for(:each_data_path,path).first
end