Class: Bumbleworks::Task
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/bumbleworks/task.rb
Defined Under Namespace
Classes: AlreadyClaimed, EntityNotFound, MissingWorkitem, NotCompletable
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(workitem) ⇒ Task
Returns a new instance of Task.
69
70
71
72
73
74
75
76
|
# File 'lib/bumbleworks/task.rb', line 69
def initialize(workitem)
@workitem = workitem
unless workitem && workitem.is_a?(::Ruote::Workitem)
raise ArgumentError, "Not a valid workitem"
end
@nickname = params['task']
extend_module
end
|
Instance Attribute Details
#nickname ⇒ Object
Returns the value of attribute nickname.
12
13
14
|
# File 'lib/bumbleworks/task.rb', line 12
def nickname
@nickname
end
|
Class Method Details
.all ⇒ Object
46
47
48
|
# File 'lib/bumbleworks/task.rb', line 46
def all
from_workitems(storage_participant.all)
end
|
.autoload_all(options = {}) ⇒ Object
Autoload all task modules defined in files in the tasks_directory. The symbol for autoload comes from the camelized version of the filename, so this method is dependent on following that convention. For example, file ‘chew_cud_task.rb` should define `ChewCudTask`.
23
24
25
26
27
28
|
# File 'lib/bumbleworks/task.rb', line 23
def autoload_all(options = {})
options[:directory] ||= Bumbleworks.tasks_directory
Bumbleworks::Support.all_files(options[:directory], :camelize => true).each do |path, name|
Object.autoload name.to_sym, path
end
end
|
.find_by_id(sid) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/bumbleworks/task.rb', line 50
def find_by_id(sid)
workitem = storage_participant[sid] if sid
raise MissingWorkitem unless workitem
new(workitem)
rescue ArgumentError => e
raise MissingWorkitem, e.message
end
|
.for_claimant(token) ⇒ Object
42
43
44
|
# File 'lib/bumbleworks/task.rb', line 42
def for_claimant(token)
all.select { |t| t.claimant == token }
end
|
.for_role(identifier) ⇒ Object
30
31
32
|
# File 'lib/bumbleworks/task.rb', line 30
def for_role(identifier)
for_roles([identifier])
end
|
.for_roles(identifiers) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/bumbleworks/task.rb', line 34
def for_roles(identifiers)
return [] unless identifiers.is_a?(Array)
workitems = identifiers.collect { |identifier|
storage_participant.by_participant(identifier)
}.flatten.uniq
from_workitems(workitems)
end
|
.from_workitems(workitems) ⇒ Object
62
63
64
65
66
|
# File 'lib/bumbleworks/task.rb', line 62
def from_workitems(workitems)
workitems.map { |wi|
new(wi) if wi.params['task']
}.compact
end
|
.storage_participant ⇒ Object
58
59
60
|
# File 'lib/bumbleworks/task.rb', line 58
def storage_participant
Bumbleworks.dashboard.storage_participant
end
|
Instance Method Details
#[](key) ⇒ Object
alias for fields[] (fields delegated to workitem)
98
99
100
|
# File 'lib/bumbleworks/task.rb', line 98
def [](key)
fields[key]
end
|
#[]=(key, value) ⇒ Object
alias for fields[]= (fields delegated to workitem)
103
104
105
|
# File 'lib/bumbleworks/task.rb', line 103
def []=(key, value)
fields[key] = value
end
|
#claim(token) ⇒ Object
Claim task and assign token to claimant
153
154
155
156
|
# File 'lib/bumbleworks/task.rb', line 153
def claim(token)
set_claimant(token)
log(:claim)
end
|
#claimant ⇒ Object
Token used to claim task, nil if not claimed
143
144
145
|
# File 'lib/bumbleworks/task.rb', line 143
def claimant
params['claimant']
end
|
#claimed? ⇒ Boolean
159
160
161
|
# File 'lib/bumbleworks/task.rb', line 159
def claimed?
!claimant.nil?
end
|
#claimed_at ⇒ Object
Timestamp of last claim, nil if not currently claimed
148
149
150
|
# File 'lib/bumbleworks/task.rb', line 148
def claimed_at
params['claimed_at']
end
|
#complete(metadata = {}) ⇒ Object
proceed workitem (saving changes to fields)
132
133
134
135
136
137
138
139
140
|
# File 'lib/bumbleworks/task.rb', line 132
def complete(metadata = {})
raise NotCompletable unless completable?
before_update(metadata)
before_complete(metadata)
proceed_workitem
log(:complete, metadata)
after_complete(metadata)
after_update(metadata)
end
|
#entity ⇒ Object
78
79
80
81
82
83
84
85
|
# File 'lib/bumbleworks/task.rb', line 78
def entity
if has_entity_fields?
klass = Bumbleworks::Support.constantize(fields['entity_type'])
entity = klass.first_by_identifier(fields['entity_id'])
end
raise EntityNotFound unless entity
entity
end
|
#extend_module ⇒ Object
111
112
113
114
115
|
# File 'lib/bumbleworks/task.rb', line 111
def extend_module
extend Bumbleworks::Tasks::Base
extend task_module if nickname
rescue NameError
end
|
#has_entity? ⇒ Boolean
87
88
89
90
91
|
# File 'lib/bumbleworks/task.rb', line 87
def has_entity?
!entity.nil?
rescue EntityNotFound
false
end
|
#has_entity_fields? ⇒ Boolean
93
94
95
|
# File 'lib/bumbleworks/task.rb', line 93
def has_entity_fields?
fields['entity_id'] && fields['entity_type']
end
|
#log(action, metadata = {}) ⇒ Object
174
175
176
177
178
179
180
181
182
|
# File 'lib/bumbleworks/task.rb', line 174
def log(action, metadata = {})
Bumbleworks.logger.info({
:actor => params['claimant'],
:action => action,
:target_type => 'Task',
:target_id => id,
:metadata => metadata.merge(:current_fields => fields)
})
end
|
#on_dispatch ⇒ Object
169
170
171
172
|
# File 'lib/bumbleworks/task.rb', line 169
def on_dispatch
log(:dispatch)
after_dispatch
end
|
#release ⇒ Object
164
165
166
167
|
# File 'lib/bumbleworks/task.rb', line 164
def release
log(:release)
set_claimant(nil)
end
|
#role ⇒ Object
107
108
109
|
# File 'lib/bumbleworks/task.rb', line 107
def role
participant_name
end
|
#update(metadata = {}) ⇒ Object
update workitem with changes to fields & params
124
125
126
127
128
129
|
# File 'lib/bumbleworks/task.rb', line 124
def update(metadata = {})
before_update(metadata)
update_workitem
log(:update, metadata)
after_update(metadata)
end
|