Class: Bumbleworks::Task
- Inherits:
-
Object
- Object
- Bumbleworks::Task
- Extended by:
- Forwardable
- Includes:
- WorkitemEntityStorage
- Defined in:
- lib/bumbleworks/task.rb
Defined Under Namespace
Classes: AlreadyClaimed, MissingWorkitem, NotCompletable
Instance Attribute Summary collapse
-
#nickname ⇒ Object
readonly
Returns the value of attribute nickname.
Class Method Summary collapse
- .all ⇒ Object
-
.autoload_all(options = {}) ⇒ Object
Autoload all task modules defined in files in the tasks_directory.
- .find_by_id(sid) ⇒ Object
- .for_claimant(token) ⇒ Object
- .for_role(identifier) ⇒ Object
- .for_roles(identifiers) ⇒ Object
- .from_workitems(workitems) ⇒ Object
- .storage_participant ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
alias for fields[] (fields delegated to workitem).
-
#[]=(key, value) ⇒ Object
alias for fields[]= (fields delegated to workitem).
-
#claim(token) ⇒ Object
Claim task and assign token to claimant.
-
#claimant ⇒ Object
Token used to claim task, nil if not claimed.
-
#claimed? ⇒ Boolean
true if task is claimed.
-
#claimed_at ⇒ Object
Timestamp of last claim, nil if not currently claimed.
-
#complete(metadata = {}) ⇒ Object
proceed workitem (saving changes to fields).
- #extend_module ⇒ Object
-
#initialize(workitem) ⇒ Task
constructor
A new instance of Task.
- #log(action, metadata = {}) ⇒ Object
- #on_dispatch ⇒ Object
-
#release ⇒ Object
release claim on task.
- #reload ⇒ Object
- #role ⇒ Object
- #task_module ⇒ Object
-
#update(metadata = {}) ⇒ Object
update workitem with changes to fields & params.
Methods included from WorkitemEntityStorage
#entity, #has_entity?, #has_entity_fields?
Constructor Details
#initialize(workitem) ⇒ Task
Returns a new instance of Task.
71 72 73 74 75 76 77 78 |
# File 'lib/bumbleworks/task.rb', line 71 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 (readonly)
Returns the value of attribute nickname.
14 15 16 |
# File 'lib/bumbleworks/task.rb', line 14 def nickname @nickname end |
Class Method Details
.all ⇒ Object
48 49 50 |
# File 'lib/bumbleworks/task.rb', line 48 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`.
25 26 27 28 29 30 |
# File 'lib/bumbleworks/task.rb', line 25 def autoload_all( = {}) [:directory] ||= Bumbleworks.tasks_directory Bumbleworks::Support.all_files([:directory], :camelize => true).each do |path, name| Object.autoload name.to_sym, path end end |
.find_by_id(sid) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/bumbleworks/task.rb', line 52 def find_by_id(sid) workitem = storage_participant[sid] if sid raise MissingWorkitem unless workitem new(workitem) rescue ArgumentError => e raise MissingWorkitem, e. end |
.for_claimant(token) ⇒ Object
44 45 46 |
# File 'lib/bumbleworks/task.rb', line 44 def for_claimant(token) all.select { |t| t.claimant == token } end |
.for_role(identifier) ⇒ Object
32 33 34 |
# File 'lib/bumbleworks/task.rb', line 32 def for_role(identifier) for_roles([identifier]) end |
.for_roles(identifiers) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/bumbleworks/task.rb', line 36 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
64 65 66 67 68 |
# File 'lib/bumbleworks/task.rb', line 64 def from_workitems(workitems) workitems.map { |wi| new(wi) if wi.params['task'] }.compact end |
.storage_participant ⇒ Object
60 61 62 |
# File 'lib/bumbleworks/task.rb', line 60 def storage_participant Bumbleworks.dashboard.storage_participant end |
Instance Method Details
#[](key) ⇒ Object
alias for fields[] (fields delegated to workitem)
86 87 88 |
# File 'lib/bumbleworks/task.rb', line 86 def [](key) fields[key] end |
#[]=(key, value) ⇒ Object
alias for fields[]= (fields delegated to workitem)
91 92 93 |
# File 'lib/bumbleworks/task.rb', line 91 def []=(key, value) fields[key] = value end |
#claim(token) ⇒ Object
Claim task and assign token to claimant
141 142 143 144 |
# File 'lib/bumbleworks/task.rb', line 141 def claim(token) set_claimant(token) log(:claim) end |
#claimant ⇒ Object
Token used to claim task, nil if not claimed
131 132 133 |
# File 'lib/bumbleworks/task.rb', line 131 def claimant params['claimant'] end |
#claimed? ⇒ Boolean
true if task is claimed
147 148 149 |
# File 'lib/bumbleworks/task.rb', line 147 def claimed? !claimant.nil? end |
#claimed_at ⇒ Object
Timestamp of last claim, nil if not currently claimed
136 137 138 |
# File 'lib/bumbleworks/task.rb', line 136 def claimed_at params['claimed_at'] end |
#complete(metadata = {}) ⇒ Object
proceed workitem (saving changes to fields)
120 121 122 123 124 125 126 127 128 |
# File 'lib/bumbleworks/task.rb', line 120 def complete( = {}) raise NotCompletable.new() unless completable? before_update() before_complete() proceed_workitem log(:complete, ) after_complete() after_update() end |
#extend_module ⇒ Object
99 100 101 102 103 |
# File 'lib/bumbleworks/task.rb', line 99 def extend_module extend Bumbleworks::Tasks::Base extend task_module if nickname rescue NameError end |
#log(action, metadata = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/bumbleworks/task.rb', line 162 def log(action, = {}) Bumbleworks.logger.info({ :actor => params['claimant'], :action => action, :target_type => 'Task', :target_id => id, :metadata => .merge(:current_fields => fields) }) end |
#on_dispatch ⇒ Object
157 158 159 160 |
# File 'lib/bumbleworks/task.rb', line 157 def on_dispatch log(:dispatch) after_dispatch end |
#release ⇒ Object
release claim on task.
152 153 154 155 |
# File 'lib/bumbleworks/task.rb', line 152 def release log(:release) set_claimant(nil) end |
#reload ⇒ Object
80 81 82 83 |
# File 'lib/bumbleworks/task.rb', line 80 def reload @workitem = storage_participant[sid] self end |
#role ⇒ Object
95 96 97 |
# File 'lib/bumbleworks/task.rb', line 95 def role participant_name end |
#task_module ⇒ Object
105 106 107 108 109 |
# File 'lib/bumbleworks/task.rb', line 105 def task_module return nil unless nickname klass_name = Bumbleworks::Support.camelize(nickname) klass = Bumbleworks::Support.constantize("#{klass_name}Task") end |
#update(metadata = {}) ⇒ Object
update workitem with changes to fields & params
112 113 114 115 116 117 |
# File 'lib/bumbleworks/task.rb', line 112 def update( = {}) before_update() update_workitem log(:update, ) after_update() end |