Class: Toys::SourceInfo

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

Overview

Information about the source of a tool, such as the file, git repository, or block that defined it.

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)



64
65
66
# File 'lib/toys/source_info.rb', line 64

def context_directory
  @context_directory
end

#git_commitString? (readonly)

The git commit.

Returns:

  • (String)

    The git commit.

  • (nil)

    if this source is not fron git.



119
120
121
# File 'lib/toys/source_info.rb', line 119

def git_commit
  @git_commit
end

#git_pathString? (readonly)

The git path.

Returns:

  • (String)

    The git path. This could be the empty string.

  • (nil)

    if this source is not fron git.



111
112
113
# File 'lib/toys/source_info.rb', line 111

def git_path
  @git_path
end

#git_remoteString? (readonly)

The git remote.

Returns:

  • (String)

    The git remote

  • (nil)

    if this source is not fron git.



103
104
105
# File 'lib/toys/source_info.rb', line 103

def git_remote
  @git_remote
end

#parentToys::SourceInfo? (readonly)

The parent of this SourceInfo.

Returns:



39
40
41
# File 'lib/toys/source_info.rb', line 39

def parent
  @parent
end

#priorityInteger (readonly)

The priority of tools defined by this source. Higher values indicate a higher priority. Lower priority values could be negative.

Returns:

  • (Integer)

    The priority.



54
55
56
# File 'lib/toys/source_info.rb', line 54

def priority
  @priority
end

#rootToys::SourceInfo (readonly)

The root ancestor of this SourceInfo.

Returns:



46
47
48
# File 'lib/toys/source_info.rb', line 46

def root
  @root
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.



72
73
74
# File 'lib/toys/source_info.rb', line 72

def source
  @source
end

#source_nameString (readonly) Also known as: to_s

The user-visible name of this source.

Returns:

  • (String)


126
127
128
# File 'lib/toys/source_info.rb', line 126

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.



87
88
89
# File 'lib/toys/source_info.rb', line 87

def source_path
  @source_path
end

#source_procProc? (readonly)

The source proc.

Returns:

  • (Proc)

    The source proc

  • (nil)

    if this source has no proc.



95
96
97
# File 'lib/toys/source_info.rb', line 95

def source_proc
  @source_proc
end

#source_type:file, ... (readonly)

Return the type of source.

Returns:

  • (:file, :directory, :proc)


79
80
81
# File 'lib/toys/source_info.rb', line 79

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)


158
159
160
161
162
# File 'lib/toys/source_info.rb', line 158

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.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/toys/source_info.rb', line 138

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