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
10
11
12
# File 'lib/bumbleworks/task/finder.rb', line 6

def initialize(queries = [], task_class = Bumbleworks::Task)
  @queries = queries
  @queries << proc { |wi| wi['fields']['params']['task'] }
  @task_class = task_class
  @task_filters = []
  @wfids = nil
end

Instance Method Details

#allObject



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

def all
  return [] if @wfids == []
  workitems = Bumbleworks.dashboard.context.storage.get_many('workitems', @wfids).select { |wi|
    @queries.all? { |q| q.call(wi) }
  }.collect { |wi|
    ::Ruote::Workitem.new(wi)
  }
  from_workitems(workitems)
end

#availableObject



40
41
42
# File 'lib/bumbleworks/task/finder.rb', line 40

def available
  unclaimed.completable
end

#by_nickname(nickname) ⇒ Object



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

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

#claimedObject



64
65
66
# File 'lib/bumbleworks/task/finder.rb', line 64

def claimed
  unclaimed(false)
end

#completable(true_or_false = true) ⇒ Object



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

def completable(true_or_false = true)
  @task_filters << proc { |task| task.completable? == true_or_false }
  self
end

#each(&block) ⇒ Object



113
114
115
# File 'lib/bumbleworks/task/finder.rb', line 113

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  all.empty?
end

#for_claimant(token) ⇒ Object



73
74
75
76
# File 'lib/bumbleworks/task/finder.rb', line 73

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

#for_entity(entity) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/bumbleworks/task/finder.rb', line 78

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

#for_process(process) ⇒ Object



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

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

#for_processes(processes) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/bumbleworks/task/finder.rb', line 86

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



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

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

#for_roles(identifiers) ⇒ Object



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

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

#next_available(options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bumbleworks/task/finder.rb', line 121

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



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

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

#where(filters) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bumbleworks/task/finder.rb', line 14

def where(filters)
  key_method_map = {
    :available => :available,
    :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
  }
  fields = filters.select { |k,v| !key_method_map.keys.include? k }
  methods = filters.select { |k,v| key_method_map.keys.include? k }
  query = methods.inject(self) { |query, (method, args)|
    query.send(key_method_map[method], args)
  }
  unless fields.empty?
    query.with_fields(fields)
  end
  query
end

#with_fields(field_hash) ⇒ Object



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

def with_fields(field_hash)
  @queries << proc { |wi| field_hash.all? { |k, v| wi['fields'][k.to_s] == v } }
  self
end