Sha256: e3752b37a2fe27c1e628818e4b53bcb496cf9dcfd818d672524fbf354c712226

Contents?: true

Size: 861 Bytes

Versions: 2

Compression:

Stored size: 861 Bytes

Contents

class BMC::Filter
  include ActiveModel::Model

  STRATEGIES = {}

  attr_reader :values, :options

  def initialize(values, options = {})
    @values = values.with_indifferent_access
    @options = options
  end

  def strategies
    self.class::STRATEGIES.with_indifferent_access
  end

  def call(query)
    strategies.each do |key, strategy|
      value = values[key]

      next if value.blank?

      query = strategy.call(query, value)
    end

    query
  end

  def method_missing(method, ...)
    if strategies.key?(method)
      values[method]
    else
      super
    end
  end

  def respond_to_missing?(method, include_private = false)
    strategies.key?(method) || super
  end

  def actives_count
    values.count { |k, v| strategies.key?(k) && v.present? }
  end

  def any?
    actives_count.positive?
  end

  def empty?
    !any?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bmc-1.6.1 app/filters/bmc/filter.rb
bmc-1.6.0 app/filters/bmc/filter.rb