Class: Sunspot::Query::Connective::Abstract
- Inherits:
-
Object
- Object
- Sunspot::Query::Connective::Abstract
- Defined in:
- lib/sunspot/query/connective.rb
Overview
Base class for connectives (conjunctions and disjunctions).
Direct Known Subclasses
Instance Method Summary collapse
-
#add_component(component) ⇒ Object
Add an arbitrary component to the conjunction, and return it.
-
#add_conjunction ⇒ Object
Add a new conjunction and return it.
-
#add_disjunction ⇒ Object
Add a new disjunction and return it.
-
#add_negated_restriction(field, restriction_type, value) ⇒ Object
Add a negated restriction.
-
#add_negated_shorthand_restriction(field, value) ⇒ Object
Add a negated shorthand restriction (see add_shorthand_restriction).
-
#add_restriction(field, restriction_type, value, negated = false) ⇒ Object
Add a restriction to the connective.
-
#add_shorthand_restriction(field, value, negated = false) ⇒ Object
Add a shorthand restriction; the restriction type is determined by the value.
-
#initialize(negated = false) ⇒ Abstract
constructor
:nodoc:.
-
#negate ⇒ Object
Returns a new connective that’s a negated version of this one.
-
#negated? ⇒ Boolean
Connectives can be negated during the process of denormalization that is performed when a disjunction contains a negated component.
-
#to_boolean_phrase ⇒ Object
Express the connective as a Lucene boolean phrase.
Constructor Details
#initialize(negated = false) ⇒ Abstract
:nodoc:
8 9 10 11 |
# File 'lib/sunspot/query/connective.rb', line 8 def initialize(negated = false) #:nodoc: @negated = negated @components = [] end |
Instance Method Details
#add_component(component) ⇒ Object
Add an arbitrary component to the conjunction, and return it. The component must respond to #to_boolean_phrase
67 68 69 70 |
# File 'lib/sunspot/query/connective.rb', line 67 def add_component(component) @components << component component end |
#add_conjunction ⇒ Object
Add a new conjunction and return it.
52 53 54 |
# File 'lib/sunspot/query/connective.rb', line 52 def add_conjunction add_component(Conjunction.new) end |
#add_disjunction ⇒ Object
Add a new disjunction and return it.
59 60 61 |
# File 'lib/sunspot/query/connective.rb', line 59 def add_disjunction add_component(Disjunction.new) end |
#add_negated_restriction(field, restriction_type, value) ⇒ Object
Add a negated restriction. The added restriction will match all documents who do not match the terms of the restriction.
38 39 40 |
# File 'lib/sunspot/query/connective.rb', line 38 def add_negated_restriction(field, restriction_type, value) add_restriction(field, restriction_type, value, true) end |
#add_negated_shorthand_restriction(field, value) ⇒ Object
Add a negated shorthand restriction (see add_shorthand_restriction)
45 46 47 |
# File 'lib/sunspot/query/connective.rb', line 45 def add_negated_shorthand_restriction(field, value) add_shorthand_restriction(field, value, true) end |
#add_restriction(field, restriction_type, value, negated = false) ⇒ Object
Add a restriction to the connective.
16 17 18 |
# File 'lib/sunspot/query/connective.rb', line 16 def add_restriction(field, restriction_type, value, negated = false) @components << restriction_type.new(field, value, negated) end |
#add_shorthand_restriction(field, value, negated = false) ⇒ Object
Add a shorthand restriction; the restriction type is determined by the value.
24 25 26 27 28 29 30 31 32 |
# File 'lib/sunspot/query/connective.rb', line 24 def add_shorthand_restriction(field, value, negated = false) restriction_type = case value when Array then Restriction::AnyOf when Range then Restriction::Between else Restriction::EqualTo end add_restriction(field, restriction_type, value, negated) end |
#negate ⇒ Object
Returns a new connective that’s a negated version of this one.
106 107 108 109 110 111 112 |
# File 'lib/sunspot/query/connective.rb', line 106 def negate negated = self.class.new(!negated?) @components.each do |component| negated.add_component(component) end negated end |
#negated? ⇒ Boolean
Connectives can be negated during the process of denormalization that is performed when a disjunction contains a negated component. This method conforms to the duck type for all boolean query components.
99 100 101 |
# File 'lib/sunspot/query/connective.rb', line 99 def negated? @negated end |
#to_boolean_phrase ⇒ Object
Express the connective as a Lucene boolean phrase.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/sunspot/query/connective.rb', line 75 def to_boolean_phrase #:nodoc: unless @components.empty? 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 end |