Module: Treat::Workers::Categorizable

Included in:
Treat::Workers
Defined in:
lib/treat/workers/categorizable.rb

Overview

This module creates all the worker categories and the groups within these categories and adds the relevant hooks on the appropriate entities.

Constant Summary collapse

@@lookup =

A lookup table for entity types.

{}

Instance Method Summary collapse

Instance Method Details

#bind_group_targets(group) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/treat/workers/categorizable.rb', line 75

def bind_group_targets(group)
  group.targets.each do |entity_type|
    entity = Treat::Entities.
    const_get(entity_type.cc)
    entity.class_eval do
      add_workers group
    end
  end
end

#categorize!Object



14
15
16
17
18
19
20
# File 'lib/treat/workers/categorizable.rb', line 14

def categorize!
  Treat.workers.members.each do |cat|
    name = cat.capitalize.intern
    conf = load_category_conf(cat)
    create_category(name, conf)
  end
end

#create_category(name, conf) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/treat/workers/categorizable.rb', line 32

def create_category(name, conf)
  category = Treat::Workers.
  const_set(name, Module.new)
  conf.each_pair do |group, worker|
    name = group.to_s.cc.intern
    category.module_eval do
      @@methods = []
      def methods; @@methods; end
      def groups; self.constants; end
    end
    create_group(name, worker, category)
  end
end

#create_group(name, conf, category) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/treat/workers/categorizable.rb', line 46

def create_group(name, conf, category)
  group = category.const_set(name, Module.new)
  self.set_group_options(group, conf)
  self.bind_group_targets(group)
  self.register_group_presets(group, conf)
  @@methods << group.method
  @@lookup[group.method] = group
end

#load_category_conf(name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/treat/workers/categorizable.rb', line 22

def load_category_conf(name)
  if !Treat.workers.respond_to?(name)
    raise Treat::Exception,
    "The configuration file " +
    "for #{cat_sym} is missing."
  else
    Treat.workers[name]
  end
end

#lookup(method) ⇒ Object

Find a worker group based on method.



12
# File 'lib/treat/workers/categorizable.rb', line 12

def lookup(method); @@lookup[method]; end

#register_group_presets(group, conf) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/treat/workers/categorizable.rb', line 85

def register_group_presets(group, conf)
  return unless conf.respond_to?(:presets)
  conf.presets.each do |method|
    @@methods << method
    @@lookup[method] = group
  end
end

#set_group_options(group, conf) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/treat/workers/categorizable.rb', line 55

def set_group_options(group, conf)
  group.module_eval do
    extend Treat::Workers::Groupable
    self.type = conf.type
    self.targets = conf.targets
    if conf.respond_to?(:default)
      self.default = conf.default
    end
    if conf.respond_to?(:preset_option)
      self.preset_option = conf.preset_option
    end
    if conf.respond_to?(:presets)
      self.presets = conf.presets
    end
    if conf.respond_to?(:recursive)
      self.recursive = conf.recursive
    end
  end
end