Class: Bumbleworks::Task

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Support::WrapperComparison, WorkitemEntityStorage
Defined in:
lib/bumbleworks/task.rb,
lib/bumbleworks/task/base.rb,
lib/bumbleworks/task/finder.rb

Defined Under Namespace

Modules: Base Classes: AlreadyClaimed, AvailabilityTimeout, CompletionFailed, Finder, MissingWorkitem, NotCompletable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::WrapperComparison

#==, #eql?, #hash, #identifier_for_comparison

Methods included from WorkitemEntityStorage

#entity_storage_workitem, #wrapped_workitem

Constructor Details

#initialize(workitem) ⇒ Task

Returns a new instance of Task.



61
62
63
64
65
66
67
68
# File 'lib/bumbleworks/task.rb', line 61

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

#nicknameObject (readonly)

Returns the value of attribute nickname.



18
19
20
# File 'lib/bumbleworks/task.rb', line 18

def nickname
  @nickname
end

#workitemObject (readonly)

Returns the value of attribute workitem.



18
19
20
# File 'lib/bumbleworks/task.rb', line 18

def workitem
  @workitem
end

Class Method Details

.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`.



33
34
35
36
37
38
39
# File 'lib/bumbleworks/task.rb', line 33

def autoload_all(options = {})
  if directory = options[:directory] || Bumbleworks.tasks_directory
    Bumbleworks::Support.all_files(directory, :camelize => true).each do |path, name|
      Object.autoload name.to_sym, path
    end
  end
end

.find_by_id(sid) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/bumbleworks/task.rb', line 48

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

.method_missing(method, *args, &block) ⇒ Object



41
42
43
44
45
46
# File 'lib/bumbleworks/task.rb', line 41

def method_missing(method, *args, &block)
  if Finder.new.respond_to?(method)
    return Finder.new(self).send(method, *args, &block)
  end
  super
end

.storage_participantObject



56
57
58
# File 'lib/bumbleworks/task.rb', line 56

def storage_participant
  Bumbleworks.dashboard.storage_participant
end

Instance Method Details

#[](key) ⇒ Object

alias for fields[] (fields delegated to workitem)



76
77
78
# File 'lib/bumbleworks/task.rb', line 76

def [](key)
  fields[key]
end

#[]=(key, value) ⇒ Object

alias for fields[]= (fields delegated to workitem)



81
82
83
# File 'lib/bumbleworks/task.rb', line 81

def []=(key, value)
  fields[key] = value
end

#call_after_hooks(action, *args) ⇒ Object



107
108
109
# File 'lib/bumbleworks/task.rb', line 107

def call_after_hooks(action, *args)
  call_hooks(:after, action, *args)
end

#call_before_hooks(action, *args) ⇒ Object



103
104
105
# File 'lib/bumbleworks/task.rb', line 103

def call_before_hooks(action, *args)
  call_hooks(:before, action, *args)
end

#claim(token, options = {}) ⇒ Object

Claim task and assign token to claimant



147
148
149
150
151
152
# File 'lib/bumbleworks/task.rb', line 147

def claim(token, options = {})
  with_hooks(:claim, token, options) do
    set_claimant(token)
    log(:claim)
  end
end

#claimantObject

Token used to claim task, nil if not claimed



137
138
139
# File 'lib/bumbleworks/task.rb', line 137

def claimant
  params['claimant']
end

#claimed?Boolean

true if task is claimed

Returns:

  • (Boolean)


155
156
157
# File 'lib/bumbleworks/task.rb', line 155

def claimed?
  !claimant.nil?
end

#claimed_atObject

Timestamp of last claim, nil if not currently claimed



142
143
144
# File 'lib/bumbleworks/task.rb', line 142

def claimed_at
  params['claimed_at']
end

#complete(metadata = {}, options = {}) ⇒ Object

proceed workitem (saving changes to fields)

Raises:



126
127
128
129
130
131
132
133
134
# File 'lib/bumbleworks/task.rb', line 126

def complete( = {}, options = {})
  raise NotCompletable.new(not_completable_error_message) unless completable?
  with_hooks(:update, , options) do
    with_hooks(:complete, , options) do
      proceed_workitem
      log(:complete, )
    end
  end
end

#extend_moduleObject



89
90
91
92
93
94
95
# File 'lib/bumbleworks/task.rb', line 89

def extend_module
  extend Bumbleworks::Task::Base
  begin
    extend task_module if nickname
  rescue NameError
  end
end

#humanize(options = {}) ⇒ Object



191
192
193
# File 'lib/bumbleworks/task.rb', line 191

def humanize(options = {})
  displayify(:humanize, options)
end

#log(action, metadata = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/bumbleworks/task.rb', line 173

def log(action,  = {})
  Bumbleworks.logger.info({
    :actor => params['claimant'],
    :action => action,
    :target_type => 'Task',
    :target_id => id,
    :metadata => .merge(:current_fields => fields)
  })
end

#on_dispatchObject



168
169
170
171
# File 'lib/bumbleworks/task.rb', line 168

def on_dispatch
  log(:dispatch)
  call_after_hooks(:dispatch)
end

#release(options = {}) ⇒ Object

release claim on task.



160
161
162
163
164
165
166
# File 'lib/bumbleworks/task.rb', line 160

def release(options = {})
  current_claimant = claimant
  with_hooks(:release, current_claimant, options) do
    log(:release)
    set_claimant(nil)
  end
end

#reloadObject



70
71
72
73
# File 'lib/bumbleworks/task.rb', line 70

def reload
  @workitem = storage_participant[sid]
  self
end

#roleObject



85
86
87
# File 'lib/bumbleworks/task.rb', line 85

def role
  participant_name
end

#task_moduleObject



97
98
99
100
101
# File 'lib/bumbleworks/task.rb', line 97

def task_module
  return nil unless nickname
  klass_name = Bumbleworks::Support.camelize(nickname)
  klass = Bumbleworks::Support.constantize("#{klass_name}Task")
end

#temporary_storageObject



21
22
23
# File 'lib/bumbleworks/task.rb', line 21

def temporary_storage
  @temporary_storage ||= {}
end

#titleize(options = {}) ⇒ Object



187
188
189
# File 'lib/bumbleworks/task.rb', line 187

def titleize(options = {})
  displayify(:titleize, options)
end

#to_s(options = {}) ⇒ Object



183
184
185
# File 'lib/bumbleworks/task.rb', line 183

def to_s(options = {})
  titleize(options)
end

#update(metadata = {}, options = {}) ⇒ Object

update workitem with changes to fields & params



118
119
120
121
122
123
# File 'lib/bumbleworks/task.rb', line 118

def update( = {}, options = {})
  with_hooks(:update, , options) do
    update_workitem
    log(:update, )
  end
end

#with_hooks(action, metadata, options = {}) ⇒ Object



111
112
113
114
115
# File 'lib/bumbleworks/task.rb', line 111

def with_hooks(action, , options = {})
  call_before_hooks(action, ) unless options[:skip_callbacks]
  yield
  call_after_hooks(action, ) unless options[:skip_callbacks]
end