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



48
49
50
51
# File 'lib/fast/shortcut.rb', line 48

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



47
48
49
# File 'lib/fast/shortcut.rb', line 47

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



60
61
62
63
64
65
66
67
# 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

#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.



77
78
79
# File 'lib/fast/shortcut.rb', line 77

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

#single_run_with_block?Boolean



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

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