Class: MicroMicro::Collections::ItemsCollection

Inherits:
BaseCollection show all
Defined in:
lib/micro_micro/collections/items_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::Item?

Return the first Item from a search.

Parameters:

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

Returns:

See Also:

[View source]

42
43
44
# File 'lib/micro_micro/collections/items_collection.rb', line 42

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

#to_aArray<Hash{Symbol => Array<String, Hash>}>

Return an Array of this collection’s Items as Hashes.

Returns:

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

See Also:

[View source]

51
52
53
# File 'lib/micro_micro/collections/items_collection.rb', line 51

def to_a
  map(&:to_h)
end

#typesArray<String>

Retrieve an Array of this collection’s unique Item types.

Returns:

  • (Array<String>)

See Also:

[View source]

60
61
62
# File 'lib/micro_micro/collections/items_collection.rb', line 60

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

#where(**args) {|item| ... } ⇒ MicroMicro::Collections::ItemsCollection

Recursively search this collection for Items matching the given conditions.

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

When passing a block, each Item in this collection is yielded to the block and the returned collection will include Items 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).items.where(types: "h-card")

Search using a Hash with an Array value

MicroMicro.parse(markup, url).items.where(types: ["h-card", "h-entry"])

Search using a block

MicroMicro.parse(markup, url).items.where do |item|
  item.properties.names.include?("email")
end

Parameters:

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

Yield Parameters:

Returns:

[View source]

91
92
93
94
95
# File 'lib/micro_micro/collections/items_collection.rb', line 91

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

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