Class: MicroMicro::Collections::PropertiesCollection

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

Return the first Property from a search.

Parameters:

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

Returns:

See Also:



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

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

#namesArray<String>

Retrieve an Array of this collection’s unique Property names.

Returns:

  • (Array<String>)

See Also:



39
40
41
# File 'lib/micro_micro/collections/properties_collection.rb', line 39

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

#plain_text_propertiesMicroMicro::Collections::PropertiesCollection

A collection of plain text Propertys parsed from the node.



48
49
50
# File 'lib/micro_micro/collections/properties_collection.rb', line 48

def plain_text_properties
  @plain_text_properties ||= self.class.new(select(&:plain_text_property?))
end

#plain_text_properties?Boolean

Does this MicroMicro::Collections::PropertiesCollection include any plain text Propertys?

Returns:

  • (Boolean)


56
57
58
# File 'lib/micro_micro/collections/properties_collection.rb', line 56

def plain_text_properties?
  plain_text_properties.any?
end

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

Return a Hash of this collection’s Propertys as Arrays.

Returns:

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

See Also:



66
67
68
# File 'lib/micro_micro/collections/properties_collection.rb', line 66

def to_h
  group_by(&:name).transform_keys(&:to_sym).deep_transform_values(&:value)
end

#url_propertiesMicroMicro::Collections::PropertiesCollection

A collection of url Propertys parsed from the node.



75
76
77
# File 'lib/micro_micro/collections/properties_collection.rb', line 75

def url_properties
  @url_properties ||= self.class.new(select(&:url_property?))
end

#url_properties?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/micro_micro/collections/properties_collection.rb', line 83

def url_properties?
  url_properties.any?
end

#valuesArray<String, Hash>

Return an Array of this collection’s unique Property values.

Returns:

  • (Array<String, Hash>)

See Also:



93
94
95
# File 'lib/micro_micro/collections/properties_collection.rb', line 93

def values
  @values ||= Set[*map(&:value)].to_a
end

#where(**args) {|property| ... } ⇒ MicroMicro::Collections::PropertiesCollection

Search this collection for Propertys matching the given conditions.

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

When passing a block, each Property in this collection is yielded to the block and the returned collection will include Propertys 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).properties.where(name: "url")

Search using a Hash with an Array value

MicroMicro.parse(markup, url).properties.where(name: ["name", "url"])

Search using a block

MicroMicro.parse(markup, url).properties.where do |property|
  property.value.is_a?(Hash)
end

Parameters:

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

Yield Parameters:

Returns:



124
125
126
127
128
# File 'lib/micro_micro/collections/properties_collection.rb', line 124

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

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