Module: Treat::Entities::Entity::Debuggable

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

Overview

When Treat.debug is set to true, each call to #call_worker will result in a debug message being printed by the #print_debug function.

Instance Method Summary collapse

Instance Method Details

Explains what Treat is currently doing. Fixme: last call will never get shown.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
79
80
81
82
83
84
# File 'lib/treat/entities/entity/debuggable.rb', line 11

def print_debug(entity, task, worker, group, options)
  # Get a list of the worker's targets.
  targets = group.targets.map(&:to_s)
  
  # List the worker's targets as either
  # a single target or an and/or form
  # (since it would be too costly to
  # actually determine what target types
  # were processed at runtime for each call).
  t = targets.size == 1 ? targets[0] : targets[
  0..-2].join(', ') + ' and/or ' + targets[-1]
  
  # Add genitive for annotations (sing./plural)
  genitive = targets.size > 1 ? 'their' : 'its'
  
  # Set up an empty string and humanize task name.
  doing, human_task = '', task.to_s.gsub('_', ' ')

  # Base is "{task}-ed {a(n)|N} {target(s)}"
  if [:transformer, :computer].include?(group.type)
    tt = human_task
    tt = tt[0..-2] if tt[-1] == 'e'
    ed = tt[-1] == 'd' ? '' : 'ed'
    doing = "#{tt.capitalize}#{ed} #{t}"
  # Base is "Annotated {a(n)|N} {target(s)}"
  elsif group.type == :annotator
    if group.preset_option
      opt = options[group.preset_option]
      form = opt.to_s.gsub('_', ' ')
      human_task[-1] = ''
      human_task = form + ' ' + human_task
    end
    doing = "Annotated #{t} with " +
    "#{genitive} #{human_task}"
  end
  
  # Form is '{base} in format {worker}'.
  if group.to_s.index('Formatters')
    curr = doing + ' in format ' + worker.to_s
  # Form is '{base} using {worker}'.
  else
    curr = doing + ' using ' + worker.to_s.gsub('_', ' ')
  end
  
  # Remove any double pluralization that may happen.
  curr.gsub!('ss', 's') unless curr.index('class')
  
  # Accumulate repeated tasks.
  @@i += 1 if curr == @@prev
  
  # Change tasks, so output.
  if curr != @@prev && @@prev
    # Pluralize entity names if necessary.
    if @@i > 1
      Treat.core.entities.list.each do |e|
        @@prev.gsub!(e.to_s, e.to_s + 's')
      end
      @@prev.gsub!('its', 'their')
      @@prev = @@prev.split(' ').
      insert(1, @@i.to_s).join(' ')
    # Add determiner if singular.
    else
      @@prev = @@prev.split(' ').
      insert(1, 'a').join(' ')
    end
    # Reset counter.
    @@i = 0
    # Write to stdout.
    puts @@prev + '.'
  end
  
  @@prev = curr

end