Class: SimCtl::List

Inherits:
Array
  • Object
show all
Defined in:
lib/simctl/list.rb

Instance Method Summary collapse

Instance Method Details

#where(filter) ⇒ Array

Filters an array of objects by a given hash. The keys of the hash must be methods implemented by the objects. The values of the hash are compared to the values the object returns when calling the methods.

Parameters:

  • filter (Hash)

    the filters that should be applied

Returns:

  • (Array)

    the filtered array.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/simctl/list.rb', line 10

def where(filter)
  return self if filter.nil?
  select do |item|
    matches = true
    filter.each do |key, value|
      matches &= case value
                 when Regexp
                   item.send(key) =~ value
                 else
                   item.send(key) == value
                 end
    end
    matches
  end
end