Sha256: db91eae9bde86bacbeb80e0a6449823ab3365473064e4ed5be37bec3c68b40a2

Contents?: true

Size: 957 Bytes

Versions: 9

Compression:

Stored size: 957 Bytes

Contents

class Supernova::Condition
  attr_accessor :key, :type
  
  def initialize(key, type)
    self.key = key
    self.type = type
  end
  
  def solr_filter_for(value)
    case type
      when :not, :ne
        if value.nil?
          nil_filter
        else
          "!#{key}:#{value}"
        end
      when :gt
        "#{key}:{#{value} TO *}"
      when :gte
        "#{key}:[#{value} TO *]"
      when :lt
        "#{key}:{* TO #{value}}"
      when :lte
        "#{key}:[* TO #{value}]"
      when :nin
        value.is_a?(Range) ? "#{key}:{* TO #{value.first}} OR #{key}:{#{value.last} TO *}" : "!(#{or_key_and_value(value)})"
      when :in
        or_key_and_value(value)
    end
  end
  
  def nil_filter
    "#{key}:[* TO *]"
  end
  
  def or_key_and_value(values)
    if values.is_a?(Range)
      "#{key}:[#{values.first} TO #{values.last}]"
    else
      values.map { |v| v.nil? ? "!#{nil_filter}" : "#{key}:#{v}"}.join(" OR ")
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
supernova-0.7.3 lib/supernova/condition.rb
supernova-0.7.2 lib/supernova/condition.rb
supernova-0.7.1 lib/supernova/condition.rb
supernova-0.7.0 lib/supernova/condition.rb
supernova-0.6.8 lib/supernova/condition.rb
supernova-0.6.7 lib/supernova/condition.rb
supernova-0.6.6 lib/supernova/condition.rb
supernova-0.6.5 lib/supernova/condition.rb
supernova-0.6.4 lib/supernova/condition.rb