Class: Toys::Context

Inherits:
Object
  • Object
show all
Defined in:
core-docs/toys/context.rb

Overview

Defined in the toys-core gem

This is the base class for tool execution. It represents self when your tool's methods (such as run) are called, and it defines the methods that can be called by your tool (such as #logger and #exit.)

This class also manages the "data" available to your tool when it runs. This data is a hash of key-value pairs. It consists of values set by flags and arguments defined by the tool, plus some "well-known" values such as the logger and verbosity level.

You can obtain a value from the data using the #get method. Additionally, convenience methods are provided for many of the well-known keys. For instance, you can call #verbosity to obtain the value for the key Key::VERBOSITY. Finally, flags and positional arguments that store their data here will also typically generate convenience methods. For example, an argument with key :abc will add a method called abc that you can call to get the value.

By convention, flags and arguments defined by your tool should use strings or symbols as keys. Keys that are not strings or symbols should either be well-known keys such as Key::VERBOSITY, or should be used for internal private information needed by middleware and mixins. The module Key defines a number of well-known keys as constants.

Direct Known Subclasses

Tool

Defined Under Namespace

Modules: Key

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit(code = 0) ⇒ void

This method returns an undefined value.

Exit immediately with the given status code

Parameters:

  • code (Integer) (defaults to: 0)

    The status code, which should be 0 for no error, or nonzero for an error condition. Default is 0.



317
318
319
# File 'core-docs/toys/context.rb', line 317

def self.exit(code = 0)
  # Source available in the toys-core gem
end

Instance Method Details

#[](key) ⇒ Object Also known as: get, __get

Fetch an option or other piece of data by key.

Parameters:

  • key (Symbol)

Returns:

  • (Object)


237
238
239
# File 'core-docs/toys/context.rb', line 237

def [](key)
  # Source available in the toys-core gem
end

#[]=(key, value) ⇒ Object

Set an option or other piece of context data by key.

Parameters:

  • key (Symbol)
  • value (Object)


249
250
251
# File 'core-docs/toys/context.rb', line 249

def []=(key, value)
  # Source available in the toys-core gem
end

#argsArray<String>

The raw arguments passed to the tool, as an array of strings. This does not include the tool name itself.

This is a convenience getter for Toys::Context::Key::ARGS.

Returns:

  • (Array<String>)


146
147
148
# File 'core-docs/toys/context.rb', line 146

def args
  # Source available in the toys-core gem
end

#cliToys::CLI

The currently running CLI.

This is a convenience getter for Toys::Context::Key::CLI.

Returns:



157
158
159
# File 'core-docs/toys/context.rb', line 157

def cli
  # Source available in the toys-core gem
end

#context_directoryString?

Return the context directory for this tool. Generally, this defaults to the directory containing the toys config directory structure being read, but it may be changed by setting a different context directory for the tool.

This is a convenience getter for Toys::Context::Key::CONTEXT_DIRECTORY.

Returns:

  • (String)

    Context directory path

  • (nil)

    if there is no context.



172
173
174
# File 'core-docs/toys/context.rb', line 172

def context_directory
  # Source available in the toys-core gem
end

#exit(code = 0) ⇒ void

This method returns an undefined value.

Exit immediately with the given status code

Parameters:

  • code (Integer) (defaults to: 0)

    The status code, which should be 0 for no error, or nonzero for an error condition. Default is 0.



306
307
308
# File 'core-docs/toys/context.rb', line 306

def exit(code = 0)
  # Source available in the toys-core gem
end

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

Find the given data file or directory in this tool's search path.

Parameters:

  • path (String)

    The path to find

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

    Type of file system object to find, or nil to return any type.

Returns:

  • (String)

    Absolute path of the result

  • (nil)

    if the data was not found.



295
296
297
# File 'core-docs/toys/context.rb', line 295

def find_data(path, type: nil)
  # Source available in the toys-core gem
end

#loggerLogger

The logger for this execution.

This is a convenience getter for Toys::Context::Key::LOGGER.

Returns:

  • (Logger)


183
184
185
# File 'core-docs/toys/context.rb', line 183

def logger
  # Source available in the toys-core gem
end

#optionsHash

The subset of the context that uses string or symbol keys. By convention, this includes keys that are set by tool flags and arguments, but does not include well-known context values such as verbosity or private context values used by middleware or mixins.

Returns:

  • (Hash)


281
282
283
# File 'core-docs/toys/context.rb', line 281

def options
  # Source available in the toys-core gem
end

#set(key, value) ⇒ self #set(hash) ⇒ self

Set one or more options or other context data by key.

Overloads:

  • #set(key, value) ⇒ self

    Set an option or other piece of context data by key.

    Parameters:

    • key (Symbol)
    • value (Object)

    Returns:

    • (self)
  • #set(hash) ⇒ self

    Set multiple content data keys and values

    Parameters:

    • hash (Hash)

      The keys and values to set

    Returns:

    • (self)

Returns:

  • (self)


269
270
271
# File 'core-docs/toys/context.rb', line 269

def set(key, value = nil)
  # Source available in the toys-core gem
end

#tool_nameArray<String>

The full name of the tool being executed, as an array of strings.

This is a convenience getter for Toys::Context::Key::TOOL_NAME.

Returns:

  • (Array<String>)


194
195
196
# File 'core-docs/toys/context.rb', line 194

def tool_name
  # Source available in the toys-core gem
end

#tool_sourceToys::SourceInfo

The source of the tool being executed.

This is a convenience getter for Toys::Context::Key::TOOL_SOURCE.

Returns:



205
206
207
# File 'core-docs/toys/context.rb', line 205

def tool_source
  # Source available in the toys-core gem
end

#usage_errorsArray<Toys::ArgParser::UsageError>

The (possibly empty) array of errors detected during argument parsing.

This is a convenience getter for Toys::Context::Key::USAGE_ERRORS.

Returns:



216
217
218
# File 'core-docs/toys/context.rb', line 216

def usage_errors
  # Source available in the toys-core gem
end

#verbosityInteger

The current verbosity setting as an integer.

This is a convenience getter for Toys::Context::Key::VERBOSITY.

Returns:

  • (Integer)


227
228
229
# File 'core-docs/toys/context.rb', line 227

def verbosity
  # Source available in the toys-core gem
end