Class: RakeFactory::Task

Inherits:
Rake::TaskLib
  • Object
show all
Extended by:
Actions, Defaults, Parameters
Defined in:
lib/rake_factory/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parameters

parameter, parameter_set

Methods included from Actions

action, actions

Methods included from Defaults

default_argument_names, default_description, default_name, default_order_only_prerequisites, default_prerequisites, inherited

Constructor Details

#initialize(*args, &configuration_block) ⇒ Task

Returns a new instance of Task.



25
26
27
28
29
# File 'lib/rake_factory/task.rb', line 25

def initialize(*args, &configuration_block)
  setup_parameter_defaults
  process_arguments(args)
  process_configuration_block(configuration_block)
end

Instance Attribute Details

#configuration_blockObject

Returns the value of attribute configuration_block.



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

def configuration_block
  @configuration_block
end

Class Method Details

.inherited(inheritor) ⇒ Object



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

def self.inherited(inheritor)
  super(inheritor)
  inheritor.singleton_class.class_eval do
    define_method :define do |*args, &block|
      inheritor.new(*args, &block).define_on(Rake.application)
    end
  end
end

Instance Method Details

#check_parameter_requirementsObject



52
53
54
# File 'lib/rake_factory/task.rb', line 52

def check_parameter_requirements
  self.class.parameter_set.enforce_requirements_on(self)
end

#define_on(application) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rake_factory/task.rb', line 62

def define_on(application)
  task = application.define_task(
      Rake::Task,
      name,
      argument_names => prerequisites,
      order_only: order_only_prerequisites
  ) do |_, args|
    invoke_configuration_block(args)
    check_parameter_requirements
    invoke_actions(args)
  end
  task.add_description(description)
  self
end

#invoke_actions(args) ⇒ Object



56
57
58
59
60
# File 'lib/rake_factory/task.rb', line 56

def invoke_actions(args)
  self.class.actions.each do |action|
    action.call(*[self, args].slice(0, action.arity))
  end
end

#invoke_configuration_block(args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rake_factory/task.rb', line 44

def invoke_configuration_block(args)
  if configuration_block
    view = ParameterView.new(self)
    configuration_block.call(
        *[view, args].slice(0, configuration_block.arity))
  end
end

#process_arguments(args) ⇒ Object



35
36
37
38
# File 'lib/rake_factory/task.rb', line 35

def process_arguments(args)
  opts = args.first || {}
  opts.each { |key, value| set_if_value_present(key, value) }
end

#process_configuration_block(configuration_block) ⇒ Object



40
41
42
# File 'lib/rake_factory/task.rb', line 40

def process_configuration_block(configuration_block)
  set_if_value_present(:configuration_block, configuration_block)
end

#setup_parameter_defaultsObject



31
32
33
# File 'lib/rake_factory/task.rb', line 31

def setup_parameter_defaults
  self.class.parameter_set.apply_defaults_to(self)
end