lib/ruote/part/storage_participant.rb in ruote-2.1.10 vs lib/ruote/part/storage_participant.rb in ruote-2.1.11
- old
+ new
@@ -136,11 +136,11 @@
# Returns the count of workitems stored in this participant.
#
def size
- fetch_all.size
+ fetch_all(:count => true)
end
# Iterates over the workitems stored in here.
#
def each (&block)
@@ -148,13 +148,13 @@
all.each { |wi| block.call(wi) }
end
# Returns all the workitems stored in here.
#
- def all
+ def all (opts={})
- fetch_all.map { |hwi| Ruote::Workitem.new(hwi) }
+ fetch_all(opts).map { |hwi| Ruote::Workitem.new(hwi) }
end
# A convenience method (especially when testing), returns the first
# (only ?) workitem in the participant.
#
@@ -167,26 +167,28 @@
# Return all workitems for the specified wfid
#
def by_wfid (wfid)
- @context.storage.get_many('workitems', /!#{wfid}$/).map { |hwi|
+ @context.storage.get_many('workitems', wfid).collect { |hwi|
Ruote::Workitem.new(hwi)
}
end
# Returns all workitems for the specified participant name
#
- def by_participant (participant_name)
+ def by_participant (participant_name, opts={})
hwis = if @context.storage.respond_to?(:by_participant)
- @context.storage.by_participant('workitems', participant_name)
+ @context.storage.by_participant('workitems', participant_name, opts)
else
- fetch_all.select { |wi| wi['participant_name'] == participant_name }
+ fetch_all(opts).select { |wi|
+ wi['participant_name'] == participant_name
+ }
end
hwis.collect { |hwi| Ruote::Workitem.new(hwi) }
end
@@ -228,45 +230,41 @@
# There are two 'reserved' criterion : 'wfid' and 'participant'
# ('participant_name' as well). The rest of the criteria are considered
# constraints for fields.
#
# 'offset' and 'limit' are reserved as well. They should prove useful
- # for pagination.
+ # for pagination. 'skip' can be used instead of 'offset'.
#
# Note : the criteria is AND only, you'll have to do ORs (aggregation)
# by yourself.
#
def query (criteria)
cr = criteria.inject({}) { |h, (k, v)| h[k.to_s] = v; h }
- return @context.storage.query_workitems(cr).collect { |h|
- Ruote::Workitem.new(h)
- } if @context.storage.respond_to?(:query_workitems)
+ if @context.storage.respond_to?(:query_workitems)
+ return @context.storage.query_workitems(cr)
+ end
- offset = cr.delete('offset')
- limit = cr.delete('limit')
+ opts = {}
+ opts[:skip] = cr.delete('offset') || cr.delete('skip')
+ opts[:limit] = cr.delete('limit')
+ opts[:count] = cr.delete('count')
wfid = cr.delete('wfid')
pname = cr.delete('participant_name') || cr.delete('participant')
- hwis = if wfid
- @context.storage.get_many('workitems', /!#{wfid}$/)
- else
- fetch_all
- end
+ hwis = wfid ?
+ @context.storage.get_many('workitems', wfid, opts) : fetch_all(opts)
- hwis = hwis.select { |hwi|
+ return hwis if opts[:count]
+
+ hwis.select { |hwi|
Ruote::StorageParticipant.matches?(hwi, pname, cr)
}.collect { |hwi|
Ruote::Workitem.new(hwi)
}
-
- offset = offset || 0
- limit = limit || hwis.length
-
- hwis[offset, limit]
end
# Cleans this participant out completely
#
def purge!
@@ -292,14 +290,15 @@
protected
# Fetches all the workitems. If there is a @store_name, will only fetch
# the workitems in that store.
#
- def fetch_all
+ def fetch_all (opts={})
- key = @store_name ? /^wi!#{@store_name}::/ : nil
-
- @context.storage.get_many('workitems', key)
+ @context.storage.get_many(
+ 'workitems',
+ @store_name ? /^wi!#{@store_name}::/ : nil,
+ opts)
end
# Computes the id for the document representing the document in the storage.
#
def to_id (fei)