class SleeperRb::Utilities::ArrayProxy

A wrapper for arrays of various SleeperRb::Resources that implements a light ActiveRecord-inspired syntax for filtering the underlying collection.

Valid operators for use in where are: gt, gte, lt, lte, not.

Constants

VALID_OPERATORS

Public Instance Methods

where(options = {}) click to toggle source
# File lib/sleeper_rb/utilities/array_proxy.rb, line 23
def where(options = {})
  filtered = __getobj__.dup
  options.each do |field, value|
    value = { eq: value } unless value.is_a?(Hash)
    raise ArgumentError, "Invalid operator, must be one of: #{valid_operators}" unless valid_keys?(value)

    value.each do |operator, comparison|
      filtered = filtered.select { |object| VALID_OPERATORS[operator].call(object.send(field), comparison) }
    end
  end
  self.class.new(filtered)
end

Private Instance Methods

valid_keys?(hash) click to toggle source
# File lib/sleeper_rb/utilities/array_proxy.rb, line 42
def valid_keys?(hash)
  hash.keys.all? { |key| VALID_OPERATORS.keys.include?(key) }
end
valid_operators() click to toggle source
# File lib/sleeper_rb/utilities/array_proxy.rb, line 38
def valid_operators
  VALID_OPERATORS.keys.join(", ")
end