class Resync::SortedResourceList

An extension to BaseResourceList for resource lists that should be sorted by modification time.

Attributes

resources_by_uri[R]

@return [Hash<URI, Array<Resource>>] resources grouped by URI.

Order is not guaranteed.

Public Instance Methods

all_uris() click to toggle source

@return [Array<URI>] the set of all URIs for which this list has

resources. Order is not guaranteed.
# File lib/resync/shared/sorted_resource_list.rb, line 36
def all_uris
  @resources_by_uri.keys
end
latest_for(uri:) click to toggle source

@param uri [URI] the URI of the resource @return [Resource] the resource with the most recent modified time

for the specified URI.
# File lib/resync/shared/sorted_resource_list.rb, line 29
def latest_for(uri:)
  uri = XML.to_uri(uri)
  @resources_by_uri[uri].last
end
resources=(value) click to toggle source

Sets the resources list, sorting the resources by modification time. (nil is treated as an empty list.) Resources without modification times will be sorted to the end.

Calls superclass method Resync::BaseResourceList#resources=
# File lib/resync/shared/sorted_resource_list.rb, line 14
def resources=(value)
  super(sorted(value))
  @resources_by_uri = by_uri(resources)
end

Private Instance Methods

by_uri(resources) click to toggle source
# File lib/resync/shared/sorted_resource_list.rb, line 65
def by_uri(resources)
  by_uri = {}
  resources.each do |r|
    (by_uri[r.uri] ||= []) << r
  end
  by_uri
end
compare(left, right) click to toggle source
# File lib/resync/shared/sorted_resource_list.rb, line 55
def compare(left, right)
  %i[modified_time from_time at_time until_time completed_time].each do |time_reader|
    left_time = left.send(time_reader)
    right_time = right.send(time_reader)
    return left_time <=> right_time if left_time && right_time
    return left_time ? -1 : 1 if left_time || right_time
  end
  0
end
sorted(value) click to toggle source

Conversions

# File lib/resync/shared/sorted_resource_list.rb, line 48
def sorted(value)
  return [] unless value
  value.sort do |left, right|
    compare(left, right)
  end
end