Class: Bumbleworks::Task

Inherits:
Object
  • 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

#nicknameObject (readonly)

Returns the value of attribute nickname.



12
13
14
# File 'lib/bumbleworks/task.rb', line 12

def nickname
  @nickname
end

Class Method Details

.allObject



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_participantObject



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

#claimantObject

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

true if task is claimed

Returns:

  • (Boolean)


159
160
161
# File 'lib/bumbleworks/task.rb', line 159

def claimed?
  !claimant.nil?
end

#claimed_atObject

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)

Raises:



132
133
134
135
136
137
138
139
140
# File 'lib/bumbleworks/task.rb', line 132

def complete( = {})
  raise NotCompletable unless completable?
  before_update()
  before_complete()
  proceed_workitem
  log(:complete, )
  after_complete()
  after_update()
end

#entityObject

Raises:



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_moduleObject



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

Returns:

  • (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

Returns:

  • (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,  = {})
  Bumbleworks.logger.info({
    :actor => params['claimant'],
    :action => action,
    :target_type => 'Task',
    :target_id => id,
    :metadata => .merge(:current_fields => fields)
  })
end

#on_dispatchObject



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

def on_dispatch
  log(:dispatch)
  after_dispatch
end

#releaseObject

release claim on task.



164
165
166
167
# File 'lib/bumbleworks/task.rb', line 164

def release
  log(:release)
  set_claimant(nil)
end

#roleObject



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

def role
  participant_name
end

#task_moduleObject



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

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



124
125
126
127
128
129
# File 'lib/bumbleworks/task.rb', line 124

def update( = {})
  before_update()
  update_workitem
  log(:update, )
  after_update()
end