Class: Shellout::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/shellout/task.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Task

For executing sub tasks in correct order, we depend on the fact that hash iteration is in insert order

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
# File 'lib/shellout/task.rb', line 7

def initialize
  @sub_tasks    = {}
  @on_call_done = ->{}
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



13
14
15
16
# File 'lib/shellout/task.rb', line 13

def method_missing(name, *args)
  super unless name =~ /=$/
  add_sub_task(name.to_s.chop, args.first)
end

Instance Method Details

#add_sub_task(name, sub) ⇒ Object



18
19
20
# File 'lib/shellout/task.rb', line 18

def add_sub_task(name, sub)
  @sub_tasks[name.to_sym] = sub
end

#callObject



32
33
34
35
36
37
38
# File 'lib/shellout/task.rb', line 32

def call
  @results = {}
  @sub_tasks.each do |k, sub|
    @results[k] = sub.call
  end
  @on_call_done.call
end

#on_call_done(&callback) ⇒ Object



28
29
30
# File 'lib/shellout/task.rb', line 28

def on_call_done(&callback)
  @on_call_done = callback
end

#printf(format) ⇒ Object



22
23
24
25
26
# File 'lib/shellout/task.rb', line 22

def printf(format)
  @sub_tasks['printf'] = -> do
    super(format, @results)
  end
end