Class: Supernova::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/supernova/condition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, type) ⇒ Condition

Returns a new instance of Condition.



4
5
6
7
# File 'lib/supernova/condition.rb', line 4

def initialize(key, type)
  self.key = key
  self.type = type
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



2
3
4
# File 'lib/supernova/condition.rb', line 2

def key
  @key
end

#typeObject

Returns the value of attribute type.



2
3
4
# File 'lib/supernova/condition.rb', line 2

def type
  @type
end

Instance Method Details

#nil_filterObject



32
33
34
# File 'lib/supernova/condition.rb', line 32

def nil_filter
  "#{key}:[* TO *]"
end

#or_key_and_value(values) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/supernova/condition.rb', line 36

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

#solr_filter_for(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/supernova/condition.rb', line 9

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