Class: FeduxOrgStdlib::RecursiveFileFinder

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

Overview

Look for file recursively

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name:, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new, raise_error: true) ⇒ RecursiveFileFinder

Returns a new instance of RecursiveFileFinder.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 27

def initialize(
  file_name:,
  working_directory: Dir.getwd,
  logger: FeduxOrgStdlib::Logging::Logger.new,
  raise_error: true
)
  @logger            = logger
  @working_directory = Pathname.new(working_directory).expand_path
  @file_name         = Pathname.new(file_name)
  @raise_error       = raise_error
end

Instance Attribute Details

#file_nameRecursiveFileFinder (readonly)

Create a new instance of finder

It tries to find a file.

Parameters:

  • file_name (String)

    The name of the file to be looked for

  • working_directory (String)

    The directory where to start search

Returns:

  • (RecursiveFileFinder)

    The config instance. If the resulting data structure created by the config_engine does not respond to ‘:[]` an empty config object will be created.



25
26
27
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 25

def file_name
  @file_name
end

#loggerRecursiveFileFinder (readonly)

Create a new instance of finder

It tries to find a file.

Parameters:

  • file_name (String)

    The name of the file to be looked for

  • working_directory (String)

    The directory where to start search

Returns:

  • (RecursiveFileFinder)

    The config instance. If the resulting data structure created by the config_engine does not respond to ‘:[]` an empty config object will be created.



25
26
27
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 25

def logger
  @logger
end

#raise_errorRecursiveFileFinder (readonly)

Create a new instance of finder

It tries to find a file.

Parameters:

  • file_name (String)

    The name of the file to be looked for

  • working_directory (String)

    The directory where to start search

Returns:

  • (RecursiveFileFinder)

    The config instance. If the resulting data structure created by the config_engine does not respond to ‘:[]` an empty config object will be created.



25
26
27
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 25

def raise_error
  @raise_error
end

#working_directoryRecursiveFileFinder (readonly)

Create a new instance of finder

It tries to find a file.

Parameters:

  • file_name (String)

    The name of the file to be looked for

  • working_directory (String)

    The directory where to start search

Returns:

  • (RecursiveFileFinder)

    The config instance. If the resulting data structure created by the config_engine does not respond to ‘:[]` an empty config object will be created.



25
26
27
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 25

def working_directory
  @working_directory
end

Instance Method Details

#directoryPathname

The directory where file was found

Returns:

  • (Pathname)

    The path to directory

Raises:

  • (Errno::ENOENT)

    If file cannot be found



69
70
71
72
73
74
75
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 69

def directory
  return file.dirname if file

  fail Errno::ENOENT, file_name.to_s  if raise_error

  nil
end

#filePathname

Return path to file

Returns:

  • (Pathname)

    The path

Raises:

  • (Errno::ENOENT)

    If file cannot be found



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fedux_org_stdlib/recursive_file_finder.rb', line 46

def file
  return @found_path if @found_path

  @found_path = nil

  working_directory.ascend do |p|
    path = p + file_name
    @found_path = path if path.exist?
  end

  return @found_path if @found_path
  fail Errno::ENOENT, file_name.to_s  if raise_error

  nil
end