Class: Tap::Tasks::Insert
- Inherits:
-
Tap::Task
- Object
- Tap::Task
- Tap::Tasks::Insert
- Defined in:
- lib/tap/tasks/insert.rb
Overview
:startdoc::task insert into an array
Insert supports a common workflow pattern of inserting variable arguments into an otherwise static array.
% tap load moon -: insert goodnight %0 -: inspect
["goodnight", "moon"]
The percent serves as a placeholder identifying the index of the argument that will be inserted. Unlike most tasks, command-line arguments provided to insert define the insertion template rather than inputs (ie they cannot be enqued or executed with – or -!) so typically inserts are used with joins or signals.
% tap insert goodnight %0 -: inspect -/enq 0 moon
["goodnight", "moon"]
Arguments can be used more than once, as indicated. The default value will be used if the argument value at the given index is nil.
% tap load a -: insert %0 %0 %1 -: inspect
["a", "a", nil]
Instance Attribute Summary collapse
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
- .build(spec = {}, app = Tap::App.current) ⇒ Object
- .convert_to_spec(parser, args) ⇒ Object
- .parse(argv = ARGV, app = Tap::App.current) ⇒ Object
- .parse!(argv = ARGV, app = Tap::App.current) ⇒ Object
Instance Method Summary collapse
-
#initialize(template, config = {}, app = Tap::App.current) ⇒ Insert
constructor
A new instance of Insert.
- #process(*args) ⇒ Object
- #to_spec ⇒ Object
Constructor Details
#initialize(template, config = {}, app = Tap::App.current) ⇒ Insert
Returns a new instance of Insert.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tap/tasks/insert.rb', line 58 def initialize(template, config={}, app=Tap::App.current) super(config, app) @template = template @map = {} plength = placeholder.length template.each_with_index do |arg, index| if arg.index(placeholder) == 0 @map[index] = arg[plength, arg.length - plength].to_i end end end |
Instance Attribute Details
#template ⇒ Object (readonly)
Returns the value of attribute template.
56 57 58 |
# File 'lib/tap/tasks/insert.rb', line 56 def template @template end |
Class Method Details
.build(spec = {}, app = Tap::App.current) ⇒ Object
38 39 40 |
# File 'lib/tap/tasks/insert.rb', line 38 def build(spec={}, app=Tap::App.current) new(spec['template'], spec['config'], app) end |
.convert_to_spec(parser, args) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/tap/tasks/insert.rb', line 42 def convert_to_spec(parser, args) template = args.dup args.clear { 'config' => parser.nested_config, 'template' => template } end |
.parse(argv = ARGV, app = Tap::App.current) ⇒ Object
30 31 32 |
# File 'lib/tap/tasks/insert.rb', line 30 def parse(argv=ARGV, app=Tap::App.current) super(argv, app, &nil) end |
.parse!(argv = ARGV, app = Tap::App.current) ⇒ Object
34 35 36 |
# File 'lib/tap/tasks/insert.rb', line 34 def parse!(argv=ARGV, app=Tap::App.current) super(argv, app, &nil) end |
Instance Method Details
#process(*args) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tap/tasks/insert.rb', line 71 def process(*args) result = template.dup @map.each_pair do |insert_idx, arg_idx| value = args[arg_idx] result[insert_idx] = value.nil? ? default : value end result end |
#to_spec ⇒ Object
82 83 84 85 86 |
# File 'lib/tap/tasks/insert.rb', line 82 def to_spec spec = super spec['template'] = template spec end |