Module: Bkblz::Task::ClassMethods

Included in:
BaseTask
Defined in:
lib/bkblz/task/task.rb

Constant Summary collapse

PARAM_SPEC =
{
  :name => nil,
  :required => false,
  :default => nil,
  :one_of => nil
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_one_of(name, value, spec) ⇒ Object



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

def check_one_of(name, value, spec)
  return unless spec[:one_of]
  unless spec[:one_of].include? value
    raise TaskParamError, "[#{name}:one_of] invalid values: #{value}"
  end
end

#check_params(params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bkblz/task/task.rb', line 30

def check_params(params)
  task_params.each do |spec|
    name spec[:name]
    value = params[name]

    if spec[:default] && value.nil?
      params[name] = spec[:default]
    end

    check_required name, value, spec
    check_one_of name, value, spec
  end
end

#check_required(name, value, spec) ⇒ Object

Raises:



44
45
46
# File 'lib/bkblz/task/task.rb', line 44

def check_required(name, value, spec)
  raise TaskParamError, "[#{name}:required]" if spec[:required] && value.nil?
end

#run(config, **params) ⇒ Object



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

def run(config, **params)
  task = self.new config
  task.run **params
end

#task_param(name, **param_spec) ⇒ Object



19
20
21
22
23
24
# File 'lib/bkblz/task/task.rb', line 19

def task_param(name, **param_spec)
  if param_spec[:one_of] && !param_spec[:one_of].is_a?(Enumerable)
    raise TaskParamError, "#{name}: :one_of must be enumerable"
  end
  task_params << PARAM_SPEC.merge(param_spec.merge :name => name)
end

#task_paramsObject



26
27
28
# File 'lib/bkblz/task/task.rb', line 26

def task_params
  @task_params ||= []
end