Sha256: 3bde1f20734a7c941681bfc074e093e5fe5aed726cfd9815292f65d00d87c3a3

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module MicroMicro
  module Collections
    class RelationshipsCollection < BaseCollection
      # @see https://microformats.org/wiki/microformats2-parsing#parse_a_hyperlink_element_for_rel_microformats
      #
      # @return [Hash{Symbol => Hash{Symbol => Array, String}}]
      def group_by_url
        group_by(&:href).symbolize_keys.transform_values { |relationships| relationships.first.to_h.slice!(:href) }
      end

      # @see https://microformats.org/wiki/microformats2-parsing#parse_a_hyperlink_element_for_rel_microformats
      #
      # @return [Hash{Symbol => Array<String>}]
      def group_by_rel
        # flat_map { |member| member.rels.map { |rel| [rel, member.href] } }.group_by(&:shift).symbolize_keys.transform_values(&:flatten).transform_values(&:uniq)
        each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |member, hash|
          member.rels.each { |rel| hash[rel] << member.href }
        end.symbolize_keys.transform_values(&:uniq)
      end

      # @return [Array<String>]
      def rels
        @rels ||= map(&:rels).flatten.uniq.sort
      end

      # @return [Array<String>]
      def urls
        @urls ||= map(&:href).uniq.sort
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micromicro-1.1.0 lib/micro_micro/collections/relationships_collection.rb