Class: Abid::Task

Inherits:
Rake::Task
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/abid/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name, app) ⇒ Task

Returns a new instance of Task.



10
11
12
13
# File 'lib/abid/task.rb', line 10

def initialize(task_name, app)
  super(task_name, app)
  @siblings = {}
end

Instance Attribute Details

#extendsObject

Returns the value of attribute extends.



5
6
7
# File 'lib/abid/task.rb', line 5

def extends
  @extends
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/abid/task.rb', line 6

def params
  @params
end

#playObject (readonly)

Returns the value of attribute play.



6
7
8
# File 'lib/abid/task.rb', line 6

def play
  @play
end

#play_class_definitionObject

Returns the value of attribute play_class_definition.



5
6
7
# File 'lib/abid/task.rb', line 5

def play_class_definition
  @play_class_definition
end

Class Method Details

.define_play(*args, &block) ⇒ Object

:nodoc:



118
119
120
# File 'lib/abid/task.rb', line 118

def define_play(*args, &block) # :nodoc:
  Rake.application.define_play(self, *args, &block)
end

Instance Method Details

#bind(**params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abid/task.rb', line 28

def bind(**params)
  fail 'already bound' if bound?

  parsed_params = ParamsParser.parse(params, play_class.params_spec)
  return @siblings[parsed_params] if @siblings.include?(parsed_params)

  sorted_params = parsed_params.sort.to_h
  sorted_params.freeze

  @siblings[sorted_params] = dup.tap do |t|
    t.instance_eval do
      @prerequisites = []
      @params = sorted_params
      @play = play_class.new(t)
    end
    play_class.hooks[:setup].each { |blk| t.play.instance_eval(&blk) }
  end
end

#bound?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/abid/task.rb', line 24

def bound?
  !@play.nil?
end

#execute(_args = nil) ⇒ Object

Execute the play associated with this task.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/abid/task.rb', line 89

def execute(_args = nil)
  fail 'no play is bound yet' unless bound?

  if application.options.dryrun
    application.trace "** Execute (dry run) #{name_with_params}"
    return
  end
  if application.options.trace
    application.trace "** Execute #{name_with_params}"
  end

  play_class.hooks[:before].each { |blk| play.instance_eval(&blk) }

  call_around_hooks(play_class.hooks[:around]) { play.run }

  play_class.hooks[:after].each { |blk| play.instance_eval(&blk) }
end

#name_with_argsObject

Name of task with argument list description.



56
57
58
59
60
61
62
# File 'lib/abid/task.rb', line 56

def name_with_args # :nodoc:
  if params_description
    "#{super} #{params_description}"
  else
    super
  end
end

#name_with_paramsObject

Name of task with params



65
66
67
68
69
70
71
# File 'lib/abid/task.rb', line 65

def name_with_params # :nodoc:
  if params_description
    "#{name} #{params_description}"
  else
    super
  end
end

#params_descriptionObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/abid/task.rb', line 73

def params_description
  sig_params = play_class.params_spec.select do |_, spec|
    spec[:significant]
  end
  return if sig_params.empty?

  if bound? # unbound
    p = sig_params.map { |name, _| "#{name}=#{params[name]}" }
  else
    p = sig_params.map { |name, spec| "#{name}:#{spec[:type]}" }
  end

  p.join(' ')
end

#play_classObject



15
16
17
18
19
20
21
22
# File 'lib/abid/task.rb', line 15

def play_class
  return @play_class if @play_class

  klass = application.lookup_play_class(extends, scope)
  @play_class = Class.new(klass, &play_class_definition).tap do |c|
    c.task = self
  end
end

#prerequisite_tasksObject



47
48
49
50
51
52
53
# File 'lib/abid/task.rb', line 47

def prerequisite_tasks
  fail 'no play is bound yet' unless bound?

  prerequisites.map do |pre, params|
    application[pre, @scope, **self.params.merge(params)]
  end
end