Class: Bumbleworks::Task::Finder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bumbleworks/task/finder.rb

Defined Under Namespace

Classes: TaskQuery, WorkitemQuery

Constant Summary collapse

WhereKeyToMethodMap =
{
  :available => :available,
  :unavailable => :unavailable,
  :nickname => :by_nickname,
  :roles => :for_roles,
  :role => :for_role,
  :unclaimed => :unclaimed,
  :claimed => :claimed,
  :fields => :with_fields,
  :claimant => :for_claimant,
  :entity => :for_entity,
  :processes => :for_processes,
  :process => :for_process,
  :completable => :completable
}

Instance Method Summary collapse

Constructor Details

#initialize(task_class = Bumbleworks::Task) ⇒ Finder

Returns a new instance of Finder.



25
26
27
28
29
30
31
# File 'lib/bumbleworks/task/finder.rb', line 25

def initialize(task_class = Bumbleworks::Task)
  @task_class = task_class
  @queries = []
  @orderers = []
  @wfids = nil
  @join = :all
end

Instance Method Details

#add_filter(&block) ⇒ Object



150
151
152
153
# File 'lib/bumbleworks/task/finder.rb', line 150

def add_filter(&block)
  @queries << TaskQuery.new(&block)
  self
end

#add_query(&block) ⇒ Object



145
146
147
148
# File 'lib/bumbleworks/task/finder.rb', line 145

def add_query(&block)
  @queries << WorkitemQuery.new(&block)
  self
end

#add_subfinder(finder) ⇒ Object



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

def add_subfinder(finder)
  @queries << finder
  self
end

#allObject



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

def all
  to_a
end

#available(check = true) ⇒ Object



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

def available(check = true)
  if check
    where_all(:unclaimed => true, :completable => true)
  else
    where_any(:claimed => true, :completable => false)
  end
end

#by_nickname(nickname) ⇒ Object



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

def by_nickname(nickname)
  add_query { |wi| wi['fields']['params']['task'] == nickname }
end

#check_queries(workitem, task) ⇒ Object



195
196
197
# File 'lib/bumbleworks/task/finder.rb', line 195

def check_queries(workitem, task)
  grouped_queries(@join).call(workitem, task)
end

#claimed(check = true) ⇒ Object



89
90
91
# File 'lib/bumbleworks/task/finder.rb', line 89

def claimed(check = true)
  unclaimed(!check)
end

#completable(true_or_false = true) ⇒ Object



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

def completable(true_or_false = true)
  add_filter { |task| task.completable? == true_or_false }
end

#eachObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bumbleworks/task/finder.rb', line 168

def each
  return to_enum(:each) unless block_given?
  return if @wfids == []
  only_workitem_queries = @queries.all? { |q| q.is_a? WorkitemQuery }
  workitems = raw_workitems(@wfids)
  @orderers.each do |order_proc|
    workitems.sort! &order_proc
  end
  workitems.each { |wi|
    if task = filtered_task_from_raw_workitem(wi, only_workitem_queries)
      yield task
    end
  }
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  !any?
end

#for_claimant(token) ⇒ Object



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

def for_claimant(token)
  add_query { |wi| wi['fields']['params']['claimant'] == token }
end

#for_entity(entity) ⇒ Object



101
102
103
104
105
106
# File 'lib/bumbleworks/task/finder.rb', line 101

def for_entity(entity)
  with_fields({
    :entity_type => entity.class.name,
    :entity_id => entity.identifier
  })
end

#for_process(process) ⇒ Object



116
117
118
# File 'lib/bumbleworks/task/finder.rb', line 116

def for_process(process)
  for_processes([process])
end

#for_processes(processes) ⇒ Object



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

def for_processes(processes)
  process_ids = (processes || []).map { |p|
    p.is_a?(Bumbleworks::Process) ? p.wfid : p
  }
  @wfids = process_ids
  self
end

#for_role(identifier) ⇒ Object



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

def for_role(identifier)
  for_roles([identifier])
end

#for_roles(identifiers) ⇒ Object



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

def for_roles(identifiers)
  identifiers ||= []
  identifiers.map! { |i| i.to_s }
  add_query { |wi| identifiers.include?(wi['participant_name']) }
end

#next_available(options = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/bumbleworks/task/finder.rb', line 155

def next_available(options = {})
  options[:timeout] ||= Bumbleworks.timeout

  start_time = Time.now
  while (first_task = first).nil?
    if (Time.now - start_time) > options[:timeout]
      raise @task_class::AvailabilityTimeout, "No tasks found matching criteria in time"
    end
    sleep 0.1
  end
  first_task
end

#order_by_field(field, direction = :asc) ⇒ Object



120
121
122
# File 'lib/bumbleworks/task/finder.rb', line 120

def order_by_field(field, direction = :asc)
  order_by_fields(field => direction)
end

#order_by_fields(fields) ⇒ Object



128
129
130
# File 'lib/bumbleworks/task/finder.rb', line 128

def order_by_fields(fields)
  add_orderer(fields)
end

#order_by_param(param, direction = :asc) ⇒ Object



124
125
126
# File 'lib/bumbleworks/task/finder.rb', line 124

def order_by_param(param, direction = :asc)
  order_by_params(param => direction)
end

#order_by_params(params) ⇒ Object



132
133
134
# File 'lib/bumbleworks/task/finder.rb', line 132

def order_by_params(params)
  add_orderer(params, 'params')
end

#sizeObject



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

def size
  all.size
end

#unavailable(check = true) ⇒ Object



67
68
69
# File 'lib/bumbleworks/task/finder.rb', line 67

def unavailable(check = true)
  available(!check)
end

#unclaimed(check = true) ⇒ Object



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

def unclaimed(check = true)
  add_query { |wi| wi['fields']['params']['claimant'].nil? == check }
end

#where(filters, group_type = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bumbleworks/task/finder.rb', line 41

def where(filters, group_type = nil)
  group_type = :all unless group_type == :any
  if group_type != @join
    finder = self.class.new(@task_class)
    finder.send(:"where_#{group_type}")
  else
    finder = self
  end
  finder = filters.inject(finder) { |query_target, (key, args)|
    if method = WhereKeyToMethodMap[key]
      query_target.send(method, args)
    else
      query_target.with_fields(key => args)
    end
  }
  finder == self ? self : add_subfinder(finder)
end

#where_all(query_group = {}) ⇒ Object



37
38
39
# File 'lib/bumbleworks/task/finder.rb', line 37

def where_all(query_group = {})
  set_join_for_query_group(query_group, :all)
end

#where_any(query_group = {}) ⇒ Object



33
34
35
# File 'lib/bumbleworks/task/finder.rb', line 33

def where_any(query_group = {})
  set_join_for_query_group(query_group, :any)
end

#with_fields(field_hash) ⇒ Object



93
94
95
# File 'lib/bumbleworks/task/finder.rb', line 93

def with_fields(field_hash)
  add_query { |wi| field_hash.all? { |k, v| wi['fields'][k.to_s] == v } }
end