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<Fast::Node>>
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.
65 66 67 68 |
# File 'lib/fast/shortcut.rb', line 65 def initialize(*args, &block) @args = args if args.any? @block = block end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
64 65 66 |
# File 'lib/fast/shortcut.rb', line 64 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
77 78 79 80 81 82 83 84 |
# File 'lib/fast/shortcut.rb', line 77 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<Fast::Node>>
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.
94 95 96 |
# File 'lib/fast/shortcut.rb', line 94 def run Fast.instance_exec(&@block) if single_run_with_block? end |
#single_run_with_block? ⇒ Boolean
70 71 72 |
# File 'lib/fast/shortcut.rb', line 70 def single_run_with_block? @block && @args.nil? end |