Class: Heathen::Task

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

Overview

A Heathen task is a block of ruby called (by [Converter] in the context of a

Processor

object. This allows us to call the Processor methods in a relatively

concise (and hopefully understandable) way.

For example:

Heathen::Task.register 'ocr','image/*' do
  convert_image to: :tiff
  tesseract format: 'pdf'
end

In this case, the task will be selected if a request is made to OCR an image. A Processor object will be created and the block executed in its context (see source in [Converter#convert]). So, #convert_image and #tesseract are mixin methods of the Processor object, but of course you can put whatever Ruby you like into the task block.

Class Method Summary collapse

Class Method Details

.clear(action, mime_type = nil) ⇒ Object

Deletes any tasks that are keyed for the given action (and optional mime_type pattern, as specified in register) Used for testing purposes


41
42
43
44
45
# File 'lib/heathen/task.rb', line 41

def clear action, mime_type=nil
  tasks.keys.each do |key|
    tasks.delete key if key =~ %r[#{task_key(action,mime_type)}]
  end
end

.find(action, mime_type) ⇒ Object

Finds a registered task suitable for the given action and mime type (note, the first suitable one will be selected).


30
31
32
33
34
35
36
37
# File 'lib/heathen/task.rb', line 30

def find action, mime_type
  tasks.each do |k,task|
    if task[:action] == action && mime_type =~ %r[#{task[:mime_type_pattern]}]
      return task
    end
  end
  raise TaskNotFound.new action, mime_type
end

.register(action, mime_type_pattern, &block) ⇒ Object

Registers a code block to be run for the given action and mime type.


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

def register action, mime_type_pattern, &block
  tasks[task_key(action,mime_type_pattern)] = { action: action, mime_type_pattern: mime_type_pattern, proc: block }
end

.tasksObject


19
20
21
# File 'lib/heathen/task.rb', line 19

def tasks
  @tasks ||= {}
end