Class: Toys::SourceInfo

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

Overview

Information about source toys directories and files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#context_directoryString? (readonly)

The context directory path (normally the directory containing the toplevel toys file or directory).

Returns:

  • (String)

    The context directory path.

  • (nil)

    if there is no context directory (perhaps because the tool is being defined from a block)



43
44
45
# File 'lib/toys/source_info.rb', line 43

def context_directory
  @context_directory
end

#parentToys::SourceInfo? (readonly)

The parent of this SourceInfo.

Returns:



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

def parent
  @parent
end

#sourceString, Proc (readonly)

The source, which may be a path or a proc.

Returns:

  • (String)

    Path to the source file or directory.

  • (Proc)

    The block serving as the source.



51
52
53
# File 'lib/toys/source_info.rb', line 51

def source
  @source
end

#source_nameString (readonly) Also known as: to_s

The user-visible name of this source.

Returns:

  • (String)


81
82
83
# File 'lib/toys/source_info.rb', line 81

def source_name
  @source_name
end

#source_pathString? (readonly)

The path of the current source file or directory.

Returns:

  • (String)

    The source path

  • (nil)

    if this source has no file system path.



66
67
68
# File 'lib/toys/source_info.rb', line 66

def source_path
  @source_path
end

#source_procProc? (readonly)

The source proc.

Returns:

  • (Proc)

    The source proc

  • (nil)

    if this source has no proc.



74
75
76
# File 'lib/toys/source_info.rb', line 74

def source_proc
  @source_proc
end

#source_type:file, ... (readonly)

Return the type of source.

Returns:

  • (:file, :directory, :proc)


58
59
60
# File 'lib/toys/source_info.rb', line 58

def source_type
  @source_type
end

Instance Method Details

#apply_lib_pathsself

Apply all lib paths in order from high to low priority

Returns:

  • (self)


113
114
115
116
117
# File 'lib/toys/source_info.rb', line 113

def apply_lib_paths
  parent&.apply_lib_paths
  $LOAD_PATH.unshift(@lib_dir) if @lib_dir && !$LOAD_PATH.include?(@lib_dir)
  self
end

#find_data(path, type: nil) ⇒ String?

Locate the given data file or directory and return an absolute path.

Parameters:

  • path (String)

    The relative path to find

  • type (nil, :file, :directory) (defaults to: nil)

    Type of file system object to find, or nil (the default) to return any type.

Returns:

  • (String)

    Absolute path of the resulting data.

  • (nil)

    if the data was not found.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/toys/source_info.rb', line 93

def find_data(path, type: nil)
  if @data_dir
    full_path = ::File.join(@data_dir, path)
    case type
    when :file
      return full_path if ::File.file?(full_path)
    when :directory
      return full_path if ::File.directory?(full_path)
    else
      return full_path if ::File.readable?(full_path)
    end
  end
  parent&.find_data(path, type: type)
end