Class: Fast::Shortcut

Inherits:
Object
  • Object
show all
Defined in:
lib/fast/shortcut.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Shortcut

Returns a new instance of Shortcut.



53
54
55
56
# File 'lib/fast/shortcut.rb', line 53

def initialize(*args, &block)
  @args = args if args.any?
  @block = block
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



52
53
54
# File 'lib/fast/shortcut.rb', line 52

def args
  @args
end

Instance Method Details

#merge_args(extra_args) ⇒ Object

Merge extra arguments from input returning a new arguments array keeping the options from previous alias and replacing the files with the

Parameters:

  • extra_args (Array)


65
66
67
68
69
70
71
72
# File 'lib/fast/shortcut.rb', line 65

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(:exist?))
  command = (@args - options - files).first

  [command, *options, *files]
end

#runHash<String, Array<Astrolabe::Node>] with file => search results.

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.

Returns:

  • (Hash<String, Array<Astrolabe::Node>] with file => search results.)

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



82
83
84
# File 'lib/fast/shortcut.rb', line 82

def run
  Fast.instance_exec(&@block) if single_run_with_block?
end

#single_run_with_block?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fast/shortcut.rb', line 58

def single_run_with_block?
  @block && @args.nil?
end