class Fast::Shortcut

Wraps shortcuts for repeated command line actions or build custom scripts with shorcut blocks This is an utility that can be used preloading several shortcuts The shortcut structure will be consumed by [Fast::Cli] and feed with the command line arguments in realtime.

Attributes

args[R]

Public Class Methods

new(*args, &block) click to toggle source
# File lib/fast/shortcut.rb, line 48
def initialize(*args, &block)
  @args = args if args.any?
  @block = block
end

Public Instance Methods

merge_args(extra_args) click to toggle source

Merge extra arguments from input returning a new arguments array keeping the options from previous alias and replacing the files with the @param [Array] extra_args

# File lib/fast/shortcut.rb, line 60
def merge_args(extra_args)
  all_args = (@args + extra_args).uniq
  options = all_args.select { |arg| arg.start_with? '-' }
  files = extra_args.select(&File.method(:exists?))
  command = (@args - options - files).first

  [command, *options, *files]
end
run() click to toggle source

If the shortcut was defined with a single block and no extra arguments, it only runs the block and return the result of the yielded block. The block is also executed in the [Fast] module level. Making it easy to implement smalls scripts using several Fast methods. Use ARGV to catch regular arguments from command line if the block is given.

@return [Hash<String, Array<Astrolabe::Node>] with file => search results.

# File lib/fast/shortcut.rb, line 77
def run
  Fast.instance_exec(&@block) if single_run_with_block?
end
single_run_with_block?() click to toggle source
# File lib/fast/shortcut.rb, line 53
def single_run_with_block?
  @block && @args.nil?
end