Module: Treat::Entities::Entity::Applicable

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

Overview

Implement support for the functions #do and #do_task.

Instance Method Summary collapse

Instance Method Details

#apply(*tasks) ⇒ Object Also known as: do

Perform the supplied tasks on the entity.



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

def apply(*tasks)
  tasks.each do |task|

    if task.is_a?(Hash)

      task.each do |k,v|
        t, w = k, v
        w, o = *w if w.is_a?(Array)
        o ||= {}
        do_task(t, w, o)
      end
    else

      t = task.is_a?(Array) ? task[0] : task
      w = task.is_a?(Array) ? task[1] : nil
      w, o = *w if w.is_a?(Array)
      o ||= {}
      do_task(t, w, o)
    end

  end
  self
end

#do_task(task, worker, options, group = nil) ⇒ Object

Perform an individual task on an entity given a worker and options to pass to it.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/treat/entities/entity/applicable.rb', line 33

def do_task(task, worker, options, group = nil)
  group ||= get_group(task)
  entity_types = group.targets
  f = nil
  entity_types.each do |t|
    f = true if is_a?(Treat::Entities.const_get(t.cc))
  end
  if f || entity_types.include?(:entity)
    send(task, worker, options)
    if group.recursive
      each do |entity|
        entity.do_task(task, worker, options, group)
      end
    end
  else
    each do |entity|
      entity.do_task(task, worker, options, group)
    end
    unless entity_types.include?(type)
      features.delete(task)
    end
    nil
  end
end

#get_group(task) ⇒ Object

Get the group of a task.



59
60
61
62
63
64
65
66
# File 'lib/treat/entities/entity/applicable.rb', line 59

def get_group(task)
  g = Treat::Workers.lookup(task)
  unless g
    raise Treat::Exception,
    "Task #{task} does not exist."
  end
  g
end