Class: MicroMicro::Collections::RelationshipsCollection
- Inherits:
-
BaseCollection
- Object
- BaseCollection
- MicroMicro::Collections::RelationshipsCollection
- Defined in:
- lib/micro_micro/collections/relationships_collection.rb
Instance Method Summary collapse
-
#find_by(**args, &block) ⇒ MicroMicro::Relationship?
Return the first Relationship from a search.
-
#group_by_rel ⇒ Hash{Symbol => Array<String>}
Return a Hash of this collection’s Relationships grouped by their
rel
attribute value. -
#group_by_url ⇒ Hash{Symbol => Hash{Symbol => Array, String}}
Return a Hash of this collection’s Relationships grouped by their
href
attribute value. -
#rels ⇒ Array<String>
Retrieve an Array of this collection’s unique Relationship
rel
attrivute values. -
#urls ⇒ Array<String>
Retrieve an Array of this collection’s unique Relationship
href
attribute values. -
#where(**args) {|relationship| ... } ⇒ MicroMicro::Collections::RelationshipsCollection
Search this collection for Relationships matching the given conditions.
Methods inherited from BaseCollection
Constructor Details
This class inherits a constructor from MicroMicro::Collections::BaseCollection
Instance Method Details
permalink #find_by(**args, &block) ⇒ MicroMicro::Relationship?
Return the first Relationship from a search.
29 30 31 |
# File 'lib/micro_micro/collections/relationships_collection.rb', line 29 def find_by(**args, &block) where(**args, &block).first end |
permalink #group_by_rel ⇒ Hash{Symbol => Array<String>}
Return a Hash of this collection’s Relationships grouped by their rel
attribute value.
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 |
permalink #group_by_url ⇒ Hash{Symbol => Hash{Symbol => Array, String}}
Return a Hash of this collection’s Relationships grouped by their href
attribute value.
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 |
permalink #rels ⇒ Array<String>
Retrieve an Array of this collection’s unique Relationship rel
attrivute values.
64 65 66 |
# File 'lib/micro_micro/collections/relationships_collection.rb', line 64 def rels @rels ||= Set[*flat_map(&:rels)].to_a.sort end |
permalink #urls ⇒ Array<String>
Retrieve an Array of this collection’s unique Relationship href
attribute values.
74 75 76 |
# File 'lib/micro_micro/collections/relationships_collection.rb', line 74 def urls @urls ||= Set[*map(&:href)].to_a.sort end |
permalink #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
.
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 |