Sha256: 522a91dd8730198e518bb726c6ec5e92fe111d27f138f615cb48b927e0da21bd

Contents?: true

Size: 1.3 KB

Versions: 33

Compression:

Stored size: 1.3 KB

Contents

class Agilibox::SmallData::Filter
  STRATEGIES = {}

  attr_reader :jar

  def initialize(jar)
    @jar = jar
  end

  def strategies
    self.class::STRATEGIES.with_indifferent_access
  end

  def apply(query)
    strategies.each do |key, strategy|
      value = get(key)

      next if value.blank?

      query = strategy.apply(query, value)
    end

    return query
  end

  def method_missing(method, *args)
    if method.to_s.end_with?("=")
      key    = method.to_s[0..-2]
      value  = args.first
      action = :write
    else
      key    = method.to_s
      action = :read
    end

    if strategies.key?(key) && action == :read
      get(key)
    elsif strategies.key?(key) && action == :write
      set(key, value)
    else
      super
    end
  end

  def respond_to_missing?(method, *)
    strategies.key?(method.to_s) || strategies.key?(method.to_s.chomp("="))
  end

  def read
    JSON.parse jar["filters"].to_s
  rescue JSON::ParserError
    {}
  end

  def write(filters)
    jar["filters"] = filters.to_json
  end

  def merge(new_filters)
    write read.merge(new_filters)
  end

  def any?
    read.select { |k, v| strategies.key?(k.to_s) && v.present? }.any?
  end

  def empty?
    !any?
  end

  private

  def get(key)
    read[key.to_s]
  end

  def set(key, value)
    merge(key.to_s => value)
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
agilibox-1.9.5 app/filters/agilibox/small_data/filter.rb
agilibox-1.9.4 app/filters/agilibox/small_data/filter.rb
agilibox-1.9.3 app/filters/agilibox/small_data/filter.rb
agilibox-1.9.1 app/filters/agilibox/small_data/filter.rb
agilibox-1.9.0 app/filters/agilibox/small_data/filter.rb
agilibox-1.8.0 app/filters/agilibox/small_data/filter.rb
agilibox-1.7.4 app/filters/agilibox/small_data/filter.rb
agilibox-1.7.3 app/filters/agilibox/small_data/filter.rb
agilibox-1.7.2 app/filters/agilibox/small_data/filter.rb
agilibox-1.7.1 app/filters/agilibox/small_data/filter.rb
agilibox-1.7.0 app/filters/agilibox/small_data/filter.rb
agilibox-1.6.2 app/filters/agilibox/small_data/filter.rb
agilibox-1.6.1 app/filters/agilibox/small_data/filter.rb
agilibox-1.6.0 app/filters/agilibox/small_data/filter.rb
agilibox-1.5.13 app/filters/agilibox/small_data/filter.rb
agilibox-1.5.12 app/filters/agilibox/small_data/filter.rb
agilibox-1.5.11 app/filters/agilibox/small_data/filter.rb
agilibox-1.5.10 app/filters/agilibox/small_data/filter.rb
agilibox-1.5.9 app/filters/agilibox/small_data/filter.rb
agilibox-1.5.8 app/filters/agilibox/small_data/filter.rb