Class: MicroMicro::Collections::RelationshipsCollection

Inherits:
BaseCollection
  • Object
show all
Defined in:
lib/micro_micro/collections/relationships_collection.rb

Instance Method Summary collapse

Methods inherited from BaseCollection

#initialize, #inspect, #push

Constructor Details

This class inherits a constructor from MicroMicro::Collections::BaseCollection

Instance Method Details

#find_by(**args, &block) ⇒ MicroMicro::Relationship?

Return the first Relationship from a search.

Parameters:

  • args (Hash{Symbol => String, Array<String>})

Returns:

See Also:

[View source]

29
30
31
# File 'lib/micro_micro/collections/relationships_collection.rb', line 29

def find_by(**args, &block)
  where(**args, &block).first
end

#group_by_relHash{Symbol => Array<String>}

Return a Hash of this collection’s Relationships grouped by their rel attribute value.

[View source]

40
41
42
43
44
45
# File 'lib/micro_micro/collections/relationships_collection.rb', line 40

def group_by_rel
  obj = Hash.new { |hash, key| hash[key] = Set.new }

  each_with_object(obj) { |member, hash| member.rels.each { |rel| hash[rel.to_sym] << member.href } }
    .transform_values(&:to_a)
end

#group_by_urlHash{Symbol => Hash{Symbol => Array, String}}

Return a Hash of this collection’s Relationships grouped by their href attribute value.

Returns:

  • (Hash{Symbol => Hash{Symbol => Array, String}})

See Also:

[View source]

54
55
56
# File 'lib/micro_micro/collections/relationships_collection.rb', line 54

def group_by_url
  group_by(&:href).to_h { |k, v| [k.to_sym, v.first.to_h.except(:href)] }
end

#relsArray<String>

Retrieve an Array of this collection’s unique Relationship rel attrivute values.

Returns:

  • (Array<String>)

See Also:

[View source]

64
65
66
# File 'lib/micro_micro/collections/relationships_collection.rb', line 64

def rels
  @rels ||= Set[*flat_map(&:rels)].to_a.sort
end

#urlsArray<String>

Retrieve an Array of this collection’s unique Relationship href attribute values.

Returns:

  • (Array<String>)

See Also:

  • Relationship#urls
[View source]

74
75
76
# File 'lib/micro_micro/collections/relationships_collection.rb', line 74

def urls
  @urls ||= Set[*map(&:href)].to_a.sort
end

#where(**args) {|relationship| ... } ⇒ MicroMicro::Collections::RelationshipsCollection

Search this collection for Relationships matching the given conditions.

If a Hash is supplied, the returned collection will include Relationships matching all conditions. Keys must be Symbols matching an instance method on Relationship and values may be either a String or an Array of Strings.

When passing a block, each Relationship in this collection is yielded to the block and the returned collection will include Relationships that cause the block to return a value other than false or nil.

Examples:

Search using a Hash with a String value

MicroMicro.parse(markup, url).relationships.where(rels: "webmention")

Search using a Hash with an Array value

MicroMicro.parse(markup, url).relationships.where(rels: ["me", "webmention"])

Search using a block

MicroMicro.parse(markup, url).relationships.where do |relationship|
  relationship.href.match?(%r{https://webmention.io/.+})
end

Parameters:

  • args (Hash{Symbol => String, Array<String>})

Yield Parameters:

Returns:

[View source]

105
106
107
108
109
# File 'lib/micro_micro/collections/relationships_collection.rb', line 105

def where(**args, &block)
  return self if args.none? && !block

  self.class.new(RelationshipsCollectionSearch.new.search(self, **args, &block))
end