Sha256: e7e1e55c5572c25669930882e8651dab529c81473722631c4c0ca4779021e3a0
Contents?: true
Size: 1.31 KB
Versions: 17
Compression:
Stored size: 1.31 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, *) super || 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
17 entries across 17 versions & 1 rubygems