Sha256: 375215051f4668803d2dec3de828e0414d59154abcf3e8b8e4bff4762661b4ea

Contents?: true

Size: 733 Bytes

Versions: 17

Compression:

Stored size: 733 Bytes

Contents

module SimCtl
  class List < 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.
    #
    # @param filter [Hash] the filters that should be applied
    # @return [Array] the filtered array.
    def where(filter)
      return self if filter.nil?
      select do |item|
        matches = true
        filter.each do |key, value|
          case value
          when Regexp
            matches &= item.send(key) =~ value
          else
            matches &= item.send(key) == value
          end
        end
        matches
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
simctl-1.6.2 lib/simctl/list.rb
simctl-1.6.1 lib/simctl/list.rb
simctl-1.6.0 lib/simctl/list.rb
simctl-1.5.8 lib/simctl/list.rb
simctl-1.5.7 lib/simctl/list.rb
simctl-1.5.6 lib/simctl/list.rb
simctl-1.5.5 lib/simctl/list.rb
simctl-1.5.4 lib/simctl/list.rb
simctl-1.5.3 lib/simctl/list.rb
simctl-1.5.2 lib/simctl/list.rb
simctl-1.5.1 lib/simctl/list.rb
simctl-1.5.0 lib/simctl/list.rb
simctl-1.4.1 lib/simctl/list.rb
simctl-1.4.0 lib/simctl/list.rb
simctl-1.3.1 lib/simctl/list.rb
simctl-1.3.0 lib/simctl/list.rb
simctl-1.2.3 lib/simctl/list.rb