Class: Sunspot::Restriction::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/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_negative_params. Instead of implementing those methods, they may choose to implement any of:

  • #to_positive_boolean_phrase, and optionally #to_negative_boolean_phrase

  • #to_solr_conditional

Direct Known Subclasses

AllOf, AnyOf, Between, EqualTo, GreaterThan, LessThan, SameAs

Instance Method Summary collapse

Constructor Details

#initialize(field, value, negative = false) ⇒ Base

:nodoc:



30
31
32
# File 'lib/sunspot/restriction.rb', line 30

def initialize(field, value, negative = false)
  @field, @value, @negative = field, value, negative
end

Instance Method Details

#to_boolean_phraseObject

Return the boolean phrase associated with this restriction object. Differentiates between positive and negative boolean phrases depending on whether this restriction is negated.



54
55
56
57
58
59
60
# File 'lib/sunspot/restriction.rb', line 54

def to_boolean_phrase
  unless negative?
    to_positive_boolean_phrase
  else
    to_negative_boolean_phrase
  end
end

#to_negative_boolean_phraseObject

Boolean phrase representing this restriction in the negative. 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 negative



88
89
90
# File 'lib/sunspot/restriction.rb', line 88

def to_negative_boolean_phrase
  "-#{to_positive_boolean_phrase}"
end

#to_paramsObject

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



45
46
47
# File 'lib/sunspot/restriction.rb', line 45

def to_params
  { :filter_queries => [to_boolean_phrase] }
end

#to_positive_boolean_phraseObject

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



75
76
77
# File 'lib/sunspot/restriction.rb', line 75

def to_positive_boolean_phrase
  "#{@field.indexed_name}:#{to_solr_conditional}"
end