Module: Collectible::Collection::Finder

Extended by:
ActiveSupport::Concern
Included in:
Collectible::CollectionBase
Defined in:
lib/collectible/collection/finder.rb

Instance Method Summary collapse

Instance Method Details

#find_by(**attributes) ⇒ *

Returns The first item in the collection that matches the specified attributes.

Parameters:

  • attributes (Hash)

    A hash of arbitrary attributes to match against the collection

Returns:

  • (*)

    The first item in the collection that matches the specified attributes



10
11
12
13
14
15
16
# File 'lib/collectible/collection/finder.rb', line 10

def find_by(**attributes)
  find do |item|
    attributes.all? do |attribute, value|
      item.public_send(attribute) == value
    end
  end
end

#where(**attributes) ⇒ ApplicationCollection

Returns A collection of all items in the collection that match the specified attributes.

Parameters:

  • attributes (Hash)

    A hash of arbitrary attributes to match against the collection

Returns:

  • (ApplicationCollection)

    A collection of all items in the collection that match the specified attributes



20
21
22
23
24
25
26
# File 'lib/collectible/collection/finder.rb', line 20

def where(**attributes)
  select do |item|
    attributes.all? do |attribute, value|
      item.public_send(attribute) == value
    end
  end
end