Class: Houdini::Task

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, blueprint, options = {}) ⇒ Task

Returns a new instance of Task.



6
7
8
9
10
11
12
13
14
# File 'lib/houdini/task.rb', line 6

def initialize(klass, blueprint, options={})
  @class_name         = klass.name
  @blueprint          = blueprint.to_s
  @input              = options[:input]
  @after_submit       = options[:after_submit]
  @on_task_completion = options[:on_task_completion]
  @finder             = options[:finder] || :find
  @id_method          = options[:id_method] || :id
end

Class Method Details

.callable(object, callable, *args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/houdini/task.rb', line 39

def self.callable(object, callable, *args)
  if callable.is_a? Symbol
    object.send callable, *args
  elsif callable.respond_to? :call
    object.instance_exec *args, &callable
  elsif block_given?
    yield callable, *args
  else
    raise "#{callable.inspect} is not a Symbol and does not respond to #call"
  end
end

.get_input(object, input) ⇒ Object



33
34
35
36
37
# File 'lib/houdini/task.rb', line 33

def self.get_input(object, input)
  input.inject({}) do |hash, (info_name, model_attribute)|
    hash.merge info_name => callable(object, model_attribute){ model_attribute }
  end
end

Instance Method Details

#process(object_id, results, verbose_results) ⇒ Object



16
17
18
19
20
# File 'lib/houdini/task.rb', line 16

def process(object_id, results, verbose_results)
  # we have to re-constantize the class name because Rails reloads the classes in development
  obj = self.class.callable @class_name.constantize, @finder, object_id
  self.class.callable obj, @on_task_completion, results, verbose_results  if @on_task_completion
end

#submit!(object, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/houdini/task.rb', line 22

def submit!(object, options={})
  get_input   = options[:input_transformer] || self.class.method(:get_input)
  submit_task = options[:submit_task]       || Houdini.method(:submit!)

  id = self.class.callable object, @id_method

  submit_task.call @blueprint, object.class.name, id, get_input.call(object, @input)

  self.class.callable(object, @after_submit) if @after_submit
end