Class: Sunspot::Query::Restriction::Base
- Inherits:
-
Object
- Object
- Sunspot::Query::Restriction::Base
- Includes:
- RSolr::Char, Filter
- Defined in:
- lib/sunspot/query/restriction.rb
Overview
Subclasses of this class represent restrictions that can be applied to a Sunspot query. The Sunspot::DSL::Restriction class presents a builder API for instances of this class.
Implementations of this class must respond to #to_params and #to_negated_params. Instead of implementing those methods, they may choose to implement any of:
-
#to_positive_boolean_phrase, and optionally #to_negated_boolean_phrase
-
#to_solr_conditional
Direct Known Subclasses
AllOf, AnyOf, Between, EqualTo, GreaterThan, GreaterThanOrEqualTo, InRadius, LessThan, LessThanOrEqualTo, StartingWith
Constant Summary collapse
- RESERVED_WORDS =
Instance Method Summary collapse
-
#initialize(negated, field, value) ⇒ Base
constructor
A new instance of Base.
-
#negate ⇒ Object
Return a new restriction that is the negated version of this one.
-
#negated? ⇒ Boolean
Whether this restriction should be negated from its original meaning.
-
#to_boolean_phrase ⇒ Object
Return the boolean phrase associated with this restriction object.
-
#to_negated_boolean_phrase ⇒ Object
Boolean phrase representing this restriction in the negated.
-
#to_params ⇒ Object
A hash representing this restriction in solr-ruby’s parameter format.
-
#to_positive_boolean_phrase ⇒ Object
Boolean phrase representing this restriction in the positive.
Methods included from Filter
Constructor Details
#initialize(negated, field, value) ⇒ Base
Returns a new instance of Base.
45 46 47 48 |
# File 'lib/sunspot/query/restriction.rb', line 45 def initialize(negated, field, value) raise ArgumentError.new("RFCTR") unless [true, false].include?(negated) @negated, @field, @value = negated, field, value end |
Instance Method Details
#negate ⇒ Object
Return a new restriction that is the negated version of this one. It is used by disjunction denormalization.
122 123 124 |
# File 'lib/sunspot/query/restriction.rb', line 122 def negate self.class.new(!@negated, @field, @value) end |
#negated? ⇒ Boolean
Whether this restriction should be negated from its original meaning
114 115 116 |
# File 'lib/sunspot/query/restriction.rb', line 114 def negated? #:nodoc: !!@negated end |
#to_boolean_phrase ⇒ Object
Return the boolean phrase associated with this restriction object. Differentiates between positive and negated boolean phrases depending on whether this restriction is negated.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sunspot/query/restriction.rb', line 70 def to_boolean_phrase phrase = [] phrase << @field.local_params if @field.respond_to? :local_params unless negated? phrase << to_positive_boolean_phrase else phrase << to_negated_boolean_phrase end phrase.join end |
#to_negated_boolean_phrase ⇒ Object
Boolean phrase representing this restriction in the negated. Subclasses may choose to implement this method, but it is not necessary, as the base implementation delegates to #to_positive_boolean_phrase.
Returns
- String
-
Boolean phrase for restriction in the negated
107 108 109 |
# File 'lib/sunspot/query/restriction.rb', line 107 def to_negated_boolean_phrase "-#{to_positive_boolean_phrase}" end |
#to_params ⇒ Object
A hash representing this restriction in solr-ruby’s parameter format. All restriction implementations must respond to this method; however, the base implementation delegates to the #to_positive_boolean_phrase method, so subclasses may (and probably should) choose to implement that method instead.
Returns
- Hash
-
Representation of this restriction as solr-ruby parameters
61 62 63 |
# File 'lib/sunspot/query/restriction.rb', line 61 def to_params { :fq => [to_filter_query] } end |
#to_positive_boolean_phrase ⇒ Object
Boolean phrase representing this restriction in the positive. Subclasses may choose to implement this method rather than #to_params; however, this method delegates to the abstract #to_solr_conditional method, which in most cases will be what subclasses will want to implement. #to_solr_conditional contains the boolean phrase representing the condition but leaves out the field name (see built-in implementations for examples)
Returns
- String
-
Boolean phrase for restriction in the positive
94 95 96 |
# File 'lib/sunspot/query/restriction.rb', line 94 def to_positive_boolean_phrase "#{escape(@field.indexed_name)}:#{to_solr_conditional}" end |