Class: Sunspot::Query::Connective::Abstract

Inherits:
Scope
  • Object
show all
Defined in:
lib/sunspot/query/connective.rb

Overview

Base class for connectives (conjunctions and disjunctions).

Direct Known Subclasses

Conjunction, Disjunction

Instance Method Summary collapse

Methods inherited from Scope

#add_conjunction, #add_disjunction, #add_negated_restriction, #add_negated_shorthand_restriction, #add_restriction, #add_shorthand_restriction, #dynamic_query, #exclude_instance

Constructor Details

#initialize(setup, negated = false) ⇒ Abstract

:nodoc:



8
9
10
11
# File 'lib/sunspot/query/connective.rb', line 8

def initialize(setup, negated = false) #:nodoc:
  @setup, @negated = setup, negated
  @components = []
end

Instance Method Details

#add_component(component) ⇒ Object

Add a component to the connective. All components must implement the #to_boolean_phrase method.



43
44
45
# File 'lib/sunspot/query/connective.rb', line 43

def add_component(component) #:nodoc:
  @components << component
end

#negateObject



51
52
53
54
55
56
57
# File 'lib/sunspot/query/connective.rb', line 51

def negate
  negated = self.class.new(@setup, !negated?)
  for component in @components
    negated.add_component(component)
  end
  negated
end

#negated?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/sunspot/query/connective.rb', line 47

def negated?
  @negated
end

#to_boolean_phraseObject

Express the connective as a Lucene boolean phrase.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sunspot/query/connective.rb', line 23

def to_boolean_phrase #:nodoc:
  phrase = if @components.length == 1
    @components.first.to_boolean_phrase
  else
    component_phrases = @components.map do |component|
      component.to_boolean_phrase
    end
    "(#{component_phrases.join(" #{connector} ")})"
  end
  if negated?
    "-#{phrase}"
  else
    phrase
  end
end

#to_paramsObject

Connective as solr params.



16
17
18
# File 'lib/sunspot/query/connective.rb', line 16

def to_params #:nodoc:
  { :fq => to_boolean_phrase }
end