Sha256: dbec3f00e35c1aea94f19a8e7e68c13149405032d764da839dcfecd0e0baa070

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

module Sunspot
  module Query
    module Connective #:nodoc:
      # 
      # Base class for connectives (conjunctions and disjunctions).
      #
      class Abstract < Scope
        def initialize(setup) #:nodoc:
          @setup = setup
          @components = []
        end

        # 
        # Connective as solr params.
        #
        def to_params #:nodoc:
          { :fq => to_boolean_phrase }
        end

        # 
        # Express the connective as a Lucene boolean phrase.
        #
        def to_boolean_phrase #:nodoc:
          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
        end

        # 
        # Add a component to the connective. All components must implement the
        # #to_boolean_phrase method.
        #
        def add_component(component) #:nodoc:
          @components << component
        end
      end

      # 
      # Disjunctions combine their components with an OR operator.
      #
      class Disjunction < Abstract
        # 
        # Add a conjunction to the disjunction. This overrides the method in
        # the Scope class since scopes are implicitly conjunctive and thus
        # can return themselves as a conjunction. Inside a disjunction, however,
        # a conjunction must explicitly be created.
        #
        def add_conjunction
          @components << conjunction = Conjunction.new(setup)
          conjunction
        end

        private

        def connector
          'OR'
        end
      end

      # 
      # Conjunctions combine their components with an AND operator.
      #
      class Conjunction < Abstract
        private

        def connector
          'AND'
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
UnderpantsGnome-sunspot-0.9.1.1 lib/sunspot/query/connective.rb
outoftime-sunspot-0.9.0 lib/sunspot/query/connective.rb
outoftime-sunspot-0.9.1 lib/sunspot/query/connective.rb
outoftime-sunspot-0.9.2 lib/sunspot/query/connective.rb