class Bumbleworks::Task::Finder

Constants

WhereKeyToMethodMap

Public Class Methods

new(task_class = Bumbleworks::Task) click to toggle source
# 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

Public Instance Methods

add_filter(&block) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 150
def add_filter(&block)
  @queries << TaskQuery.new(&block)
  self
end
add_query(&block) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 145
def add_query(&block)
  @queries << WorkitemQuery.new(&block)
  self
end
add_subfinder(finder) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 140
def add_subfinder(finder)
  @queries << finder
  self
end
all() click to toggle source
# File lib/bumbleworks/task/finder.rb, line 183
def all
  to_a
end
available(check = true) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 195
def check_queries(workitem, task)
  grouped_queries(@join).call(workitem, task)
end
claimed(check = true) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 89
def claimed(check = true)
  unclaimed(!check)
end
completable(true_or_false = true) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 136
def completable(true_or_false = true)
  add_filter { |task| task.completable? == true_or_false }
end
each() { |task| ... } click to toggle source
# 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?() click to toggle source
# File lib/bumbleworks/task/finder.rb, line 191
def empty?
  !any?
end
for_claimant(token) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 97
def for_claimant(token)
  add_query { |wi| wi['fields']['params']['claimant'] == token }
end
for_entity(entity) click to toggle source
# 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) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 116
def for_process(process)
  for_processes([process])
end
for_processes(processes) click to toggle source
# 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) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 81
def for_role(identifier)
  for_roles([identifier])
end
for_roles(identifiers) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 128
def order_by_fields(fields)
  add_orderer(fields)
end
order_by_param(param, direction = :asc) click to toggle source
# 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) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 132
def order_by_params(params)
  add_orderer(params, 'params')
end
size() click to toggle source
# File lib/bumbleworks/task/finder.rb, line 187
def size
  all.size
end
unavailable(check = true) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 67
def unavailable(check = true)
  available(!check)
end
unclaimed(check = true) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

add_orderer(fields, field_type = 'fields') click to toggle source
# File lib/bumbleworks/task/finder.rb, line 201
def add_orderer(fields, field_type = 'fields')
  @orderers << Proc.new { |wi_x, wi_y|
    relevant_direction, result = :asc, 0
    fields.each do |field, direction|
      sets = [wi_x['fields'], wi_y['fields']]
      sets.map! { |s| s['params'] } if field_type.to_s == 'params'
      wi_x_field, wi_y_field = sets[0][field.to_s], sets[1][field.to_s]
      result = wi_x_field <=> wi_y_field
      if result.nil?
        result = wi_x_field.nil? ? 1 : -1
      end
      relevant_direction = direction
      break if !result.zero?
    end
    relevant_direction == :desc ? -result : result
  }
  self
end
filtered_task_from_raw_workitem(workitem, only_workitem_queries = false) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 220
def filtered_task_from_raw_workitem(workitem, only_workitem_queries = false)
  if only_workitem_queries
    if check_queries(workitem, nil)
      task = from_workitem(::Ruote::Workitem.new(workitem))
    end
  else
    task = from_workitem(::Ruote::Workitem.new(workitem))
    task if check_queries(workitem, task)
  end
end
from_workitem(workitem) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 248
def from_workitem(workitem)
  task = @task_class.new(workitem)
end
grouped_queries(group_type) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 231
def grouped_queries(group_type)
  Proc.new { |wi, task|
    @queries.send(:"#{group_type}?") { |q|
      case q
      when WorkitemQuery
        q.call(wi)
      when TaskQuery
        q.call(task)
      when self.class
        q.check_queries(wi, task)
      else
        raise "Unrecognized query type"
      end
    }
  }
end
join=(new_join) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 258
def join=(new_join)
  @join = new_join if [:all, :any].include?(new_join)
end
raw_workitems(wfids) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 252
def raw_workitems(wfids)
  Bumbleworks.dashboard.context.storage.get_many('workitems', wfids).select { |wi|
    wi['fields']['params']['task']
  }
end
set_join_for_query_group(query_group, type) click to toggle source
# File lib/bumbleworks/task/finder.rb, line 262
def set_join_for_query_group(query_group, type)
  if query_group.empty?
    self.join = type
    self
  else
    where(query_group, type)
  end
end