Class: Fast::Shortcut
- Inherits:
-
Object
- Object
- Fast::Shortcut
- 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Instance Method Summary collapse
-
#initialize(*args, &block) ⇒ Shortcut
constructor
A new instance of Shortcut.
-
#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.
-
#run ⇒ Hash<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.
- #single_run_with_block? ⇒ Boolean
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
#args ⇒ Object (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
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 = all_args.select { |arg| arg.start_with? '-' } files = extra_args.select(&File.method(:exist?)) command = (@args - - files).first [command, *, *files] end |
#run ⇒ Hash<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.
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
58 59 60 |
# File 'lib/fast/shortcut.rb', line 58 def single_run_with_block? @block && @args.nil? end |