Class: Bumbleworks::Task::Finder

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

Instance Method Summary collapse

Constructor Details

#initialize(queries = [], task_class = Bumbleworks::Task) ⇒ Finder

Returns a new instance of Finder.



6
7
8
9
# File 'lib/bumbleworks/task/finder.rb', line 6

def initialize(queries = [], task_class = Bumbleworks::Task)
  @queries = queries
  @task_class = task_class
end

Instance Method Details

#allObject



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

def all
  workitems = Bumbleworks.dashboard.storage_participant.send(:do_select, {}) { |wi|
    @queries.all? { |q| q.call(wi) }
  }
  from_workitems(workitems)
end

#by_nickname(nickname) ⇒ Object



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

def by_nickname(nickname)
  @queries << proc { |wi| wi['fields']['params']['task'] == nickname }
  self
end

#claimedObject



31
32
33
# File 'lib/bumbleworks/task/finder.rb', line 31

def claimed
  unclaimed(false)
end

#each(&block) ⇒ Object



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

def each(&block)
  all.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/bumbleworks/task/finder.rb', line 60

def empty?
  all.empty?
end

#for_claimant(token) ⇒ Object



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

def for_claimant(token)
  @queries << proc { |wi| wi['fields']['params']['claimant'] == token }
  self
end

#for_entity(entity) ⇒ Object



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

def for_entity(entity)
  entity_id = entity.respond_to?(:identifier) ? entity.identifier : entity.id
  @queries << proc { |wi|
    (wi['fields'][:entity_type] || wi['fields']['entity_type']) == entity.class.name &&
      (wi['fields'][:entity_id] || wi['fields']['entity_id']) == entity_id
  }
  self
end

#for_role(identifier) ⇒ Object



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

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

#for_roles(identifiers) ⇒ Object



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

def for_roles(identifiers)
  identifiers ||= []
  @queries << proc { |wi| identifiers.include?(wi['participant_name']) }
  self
end

#next_available(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bumbleworks/task/finder.rb', line 64

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

  start_time = Time.now
  while 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
end

#unclaimed(check = true) ⇒ Object



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

def unclaimed(check = true)
  @queries << proc { |wi| wi['fields']['params']['claimant'].nil? == check }
  self
end