Sha256: 68560ea7f5c38c19416c0d8d4b4d607f6a7108acf82b8fa2a44aeda658d66281

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require_relative 'base_resource_list'

module Resync
  # An extension to +BaseResourceList+ for resource lists that
  # should be sorted by modification time.
  class SortedResourceList < BaseResourceList

    # ------------------------------------------------------------
    # Custom setters

    # 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.
    def resources=(value)
      @resources = sorted(value)
      @resources_by_uri = by_uri(@resources)
    end

    # ------------------------------------------------------------
    # Custom accessors

    attr_reader :resources_by_uri

    def latest_for(uri:)
      uri = XML.to_uri(uri)
      @resources_by_uri[uri].last
    end

    def all_uris
      @resources_by_uri.keys
    end

    # ------------------------------------------------------------
    # Private methods

    private

    # ------------------------------
    # Conversions

    def sorted(value)
      return [] unless value
      value.sort do |left, right|
        if left.modified_time && right.modified_time
          left.modified_time <=> right.modified_time
        else
          right.modified_time ? 1 : -1
        end
      end
    end

    def by_uri(resources)
      by_uri = {}
      resources.each do |r|
        (by_uri[r.uri] ||= []) << r
      end
      by_uri
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resync-0.1.0 lib/resync/shared/sorted_resource_list.rb