Class: Bumbleworks::Task

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
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 WorkitemEntityStorage

#entity_storage_workitem, #wrapped_workitem

Constructor Details

#initialize(workitem) ⇒ Task

Returns a new instance of Task.



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

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.



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

def nickname
  @nickname
end

#workitemObject (readonly)

Returns the value of attribute workitem.



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

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



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

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



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

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



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

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



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

def storage_participant
  Bumbleworks.dashboard.storage_participant
end

Instance Method Details

#[](key) ⇒ Object

alias for fields[] (fields delegated to workitem)



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

def [](key)
  fields[key]
end

#[]=(key, value) ⇒ Object

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



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

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

#call_after_hooks(action, *args) ⇒ Object



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

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

#call_before_hooks(action, *args) ⇒ Object



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

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

#claim(token) ⇒ Object

Claim task and assign token to claimant



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

def claim(token)
  with_hooks(:claim, token) do
    set_claimant(token)
    log(:claim)
  end
end

#claimantObject

Token used to claim task, nil if not claimed



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

def claimant
  params['claimant']
end

#claimed?Boolean

true if task is claimed

Returns:

  • (Boolean)


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

def claimed?
  !claimant.nil?
end

#claimed_atObject

Timestamp of last claim, nil if not currently claimed



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

def claimed_at
  params['claimed_at']
end

#complete(metadata = {}) ⇒ Object

proceed workitem (saving changes to fields)

Raises:



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

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

#extend_moduleObject



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

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

#humanize(options = {}) ⇒ Object



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

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

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



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

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



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

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

#releaseObject

release claim on task.



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

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

#reloadObject



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

def reload
  @workitem = storage_participant[sid]
  self
end

#roleObject



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

def role
  participant_name
end

#task_moduleObject



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

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

#temporary_storageObject



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

def temporary_storage
  @temporary_storage ||= {}
end

#titleize(options = {}) ⇒ Object



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

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

#to_s(options = {}) ⇒ Object



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

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

#update(metadata = {}) ⇒ Object

update workitem with changes to fields & params



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

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

#with_hooks(action, *args, &block) ⇒ Object



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

def with_hooks(action, *args, &block)
  call_before_hooks(action, *args)
  yield
  call_after_hooks(action, *args)
end