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

#==(other) ⇒ Object



9
10
11
# File 'lib/supernova/condition.rb', line 9

def ==(other)
  self.hash == other.hash
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/supernova/condition.rb', line 13

def eql?(other)
  self == other
end

#hashObject



17
18
19
# File 'lib/supernova/condition.rb', line 17

def hash
  [type, key].hash
end

#nil_filterObject



44
45
46
# File 'lib/supernova/condition.rb', line 44

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

#or_key_and_value(values) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/supernova/condition.rb', line 48

def or_key_and_value(values)
  if values.is_a?(Range)
    "#{key}:[#{values.first} TO #{values.last}]"
  elsif values.respond_to?(:ne) && values.respond_to?(:sw)
    "#{key}:#{type == :inside ? "{" : "["}#{values.sw.lat},#{values.sw.lng} TO #{values.ne.lat},#{values.ne.lng}#{type == :inside ? "}" : "]"}"
  elsif values.is_a?(Supernova::Circle)
    "{!geofilt}"
  else
    values.map { |v| v.nil? ? "!#{nil_filter}" : "#{key}:#{v}"}.join(" OR ")
  end
end

#solr_filter_for(value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/supernova/condition.rb', line 21

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, :inside
      or_key_and_value(value)
  end
end