Module: DataPaths::Finders

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.



201
202
203
# File 'lib/data_paths/finders.rb', line 201

def all_data_dirs(path)
  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.



162
163
164
# File 'lib/data_paths/finders.rb', line 162

def all_data_files(path)
  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.



106
107
108
# File 'lib/data_paths/finders.rb', line 106

def all_data_paths(path)
  each_data_path(path).to_a
end

#data_glob(pattern) ⇒ Object

Deprecated.

Will be removed in 1.0.0, please use #glob_data_paths instead.



236
237
238
239
240
# File 'lib/data_paths/finders.rb', line 236

def data_glob(pattern)
  STDERR.puts "DEPRECATED: please use glob_data_paths instead."

  glob_data_paths(pattern)
end

#each_data_dir(path) {|data_dir| ... } ⇒ Enumerator

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:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



182
183
184
185
186
187
188
# File 'lib/data_paths/finders.rb', line 182

def each_data_dir(path)
  return enum_for(:each_data_dir,path) unless block_given?

  each_data_path(path) do |full_path|
    yield(full_path) if File.directory?(full_path)
  end
end

#each_data_file(path) {|data_file| ... } ⇒ Enumerator

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:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



126
127
128
129
130
131
132
# File 'lib/data_paths/finders.rb', line 126

def each_data_file(path)
  return enum_for(:each_data_file,path) unless block_given?

  each_data_path(path) do |full_path|
    yield(full_path) if File.file?(full_path)
  end
end

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

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.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



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

def each_data_path(path)
  return enum_for(:each_data_path,path) unless block_given?

  DataPaths.paths.each do |dir|
    full_path = File.join(dir,path)

    yield(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.



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

def find_data_dir(path)
  each_data_path(path).find { |full_path| File.directory?(full_path) }
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.



60
61
62
# File 'lib/data_paths/finders.rb', line 60

def find_data_file(path)
  each_data_path(path).find { |full_path| File.file?(full_path) }
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.



45
46
47
# File 'lib/data_paths/finders.rb', line 45

def find_data_path(path)
  each_data_path(path).first
end

#glob_data_paths(pattern) {|path| ... } ⇒ Array<String>

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

Parameters:

  • pattern (String)

    The path glob pattern to search with.

Yields:

  • (path)

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

Yield Parameters:

  • path (String)

    The path of a matching file within a data directory.

Returns:

  • (Array<String>)

    If no block is given, the matching paths found within all data directories will be returned.

Since:

  • 0.3.0



224
225
226
227
228
229
230
# File 'lib/data_paths/finders.rb', line 224

def glob_data_paths(pattern,&block)
  return enum_for(:glob_data_paths,pattern).to_a unless block_given?

  DataPaths.paths.each do |path|
    Dir.glob(File.join(path,pattern),&block)
  end
end

#load_yaml_file(path) ⇒ Object

Loads the YAML file at the given path, within any data directory.

Parameters:

  • path (String)

    The file path to search for.

Returns:

  • (Object)

    The contents of the YAML file.

Since:

  • 0.3.0



75
76
77
78
79
# File 'lib/data_paths/finders.rb', line 75

def load_yaml_file(path)
  if (file = find_data_file(path))
    YAML.load_file(file)
  end
end

#load_yaml_files(path) ⇒ Array

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

Parameters:

  • path (String)

    The file path to search for.

Returns:

  • (Array)

    The loaded YAML files.

Since:

  • 0.3.0



146
147
148
# File 'lib/data_paths/finders.rb', line 146

def load_yaml_files(path)
  each_data_file(path).map { |file| YAML.load_file(file) }
end