Sha256: 2b2dc7646381869fe8a0086504f4c7b56634103bf35f9ec658d57c500477fed6

Contents?: true

Size: 1.29 KB

Versions: 11

Compression:

Stored size: 1.29 KB

Contents

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

  attr_reader :jar

  def initialize(jar)
    @jar = jar
  end

  def strategies
    self.class::STRATEGIES
  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.keys.include?(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

11 entries across 11 versions & 1 rubygems

Version Path
agilibox-1.2.3 app/filters/agilibox/small_data/filter.rb
agilibox-1.2.2 app/filters/agilibox/small_data/filter.rb
agilibox-1.2.1 app/filters/agilibox/small_data/filter.rb
agilibox-1.2.0 app/filters/agilibox/small_data/filter.rb
agilibox-1.1.0 app/filters/agilibox/small_data/filter.rb
agilibox-1.0.15 app/filters/agilibox/small_data/filter.rb
agilibox-1.0.14 app/filters/agilibox/small_data/filter.rb
agilibox-1.0.13 app/filters/agilibox/small_data/filter.rb
agilibox-1.0.12 app/filters/agilibox/small_data/filter.rb
agilibox-1.0.11 app/filters/agilibox/small_data/filter.rb
agilibox-1.0.10 app/filters/agilibox/small_data/filter.rb