Module: Treat::Entities::Entity::Delegatable

Included in:
Treat::Entities::Entity
Defined in:
lib/treat/entities/entity/delegatable.rb

Overview

Makes a class delegatable, allowing calls on it to be forwarded to a worker class able to perform the appropriate task.

Instance Method Summary collapse

Instance Method Details

#add_presets(group) ⇒ Object

Add preset methods to an entity class.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/treat/entities/entity/delegatable.rb', line 7

def add_presets(group)

  opt = group.preset_option
  return unless opt

  self.class_eval do
    group.presets.each do |preset|
      define_method(preset) do |worker=nil, options={}|
        return get(preset) if has?(preset)
        options = {opt => preset}.merge(options)
        m = group.method
        send(m, worker, options)
        f = unset(m)
        features[preset] = f if f
      end
    end
  end

end

#add_workers(group) ⇒ Object

Add the workers to perform a task on an entity class.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/treat/entities/entity/delegatable.rb', line 28

def add_workers(group)
  self.class_eval do
    task = group.method
    add_presets(group)
    define_method(task) do |worker=nil, options={}|
      if worker.is_a?(Hash)
        options, worker =
        worker, nil
      end
      if !@features[task].nil?
        @features[task]
      else
        self.class.call_worker(
          self, task, worker,
          group, options
        )
      end
    end
  end

end

#call_worker(entity, task, worker, group, options) ⇒ Object

Ask a worker found in the given group to perform a task on the entity with the supplied options.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/treat/entities/entity/delegatable.rb', line 52

def call_worker(entity, task, worker, group, options)

  if worker.nil? || worker == :default
    worker = find_worker(entity, group)
  end

  print_debug(entity, task, worker,
  group, options) if Treat.core.verbosity.debug
  if not group.list.include?(worker)
    raise Treat::Exception,
    worker_not_found(worker, group)
  end

  worker = group.const_get(worker.to_s.cc.intern)
  result = worker.send(group.method, entity, options)

  if group.type == :annotator && result
    entity.features[task] = result
  end

  if group.type == :transformer
    entity
  else
    result
  end

end

#find_worker(entity, group) ⇒ Object

Find which worker to use if none has been supplied.



81
82
83
84
85
86
# File 'lib/treat/entities/entity/delegatable.rb', line 81

def find_worker(entity, group)
  group.default.nil? ?
  self.find_worker_for_language(
  entity.language, group) :
  group.default
end

#find_worker_for_language(language, group) ⇒ Object

Get the default worker for that language inside the given group.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/treat/entities/entity/delegatable.rb', line 90

def find_worker_for_language(language, group)
  lang = Treat.languages[language]
  cat = group.to_s.split('::')[2].downcase.intern
  group = group.mn.ucc.intern
  if lang.nil?
    raise Treat::Exception,
    "No configuration file loaded for language #{language}."
  end
  workers = lang.workers
  if !workers.respond_to?(cat) ||
     !workers[cat].respond_to?(group)
      workers = Treat.languages.agnostic.workers
  end
  if !workers.respond_to?(cat) || 
     !workers[cat].respond_to?(group)
    raise Treat::Exception,
    "No #{group} is/are available for the " +
    "#{language.to_s.capitalize} language."
  end
  workers[cat][group].first
end

#worker_not_found(worker, group) ⇒ Object

Return an error message and suggest possible typos.



113
114
115
116
117
# File 'lib/treat/entities/entity/delegatable.rb', line 113

def worker_not_found(worker, group)
  "Worker with name '#{worker}' couldn't be "+
  "found in group #{group}." + Treat::Helpers::Help.
  did_you_mean?(group.list.map { |c| c.ucc }, worker)
end