Class: Sunspot::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/setup.rb

Overview

This class encapsulates the search/indexing setup for a given class. Its contents are built using the Sunspot.setup method.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz) ⇒ Setup

:nodoc:



7
8
9
10
11
# File 'lib/sunspot/setup.rb', line 7

def initialize(clazz)
  @class_name = clazz.name
  @fields, @text_fields = [], []
  @dsl = DSL::Fields.new(self)
end

Class Method Details

.for(clazz) ⇒ Object

Retrieve the setup instance for the given class, or for the nearest ancestor that has a setup, if any.

Parameters

clazz<Class>

Class for which to retrieve a setup

Returns

Sunspot::Setup

Setup instance associated with the given class or its nearest ancestor



137
138
139
# File 'lib/sunspot/setup.rb', line 137

def for(clazz) #:nodoc:
  setups[clazz.name.to_sym] || self.for(clazz.superclass) if clazz
end

.setup(clazz, &block) ⇒ Object

Retrieve or create the Setup instance for the given class, evaluating the given block to add to the setup’s configuration



120
121
122
# File 'lib/sunspot/setup.rb', line 120

def setup(clazz, &block) #:nodoc:
  self.for!(clazz).setup(&block)
end

Instance Method Details

#add_fields(fields) ⇒ Object

Add fields for scope/ordering

Parameters

fields<Array>

Array of Sunspot::Field objects



20
21
22
# File 'lib/sunspot/setup.rb', line 20

def add_fields(fields)
  @fields.concat(Array(fields))
end

#add_text_fields(fields) ⇒ Object

Add fields for fulltext search

Parameters

fields<Array>

Array of Sunspot::Field objects



31
32
33
# File 'lib/sunspot/setup.rb', line 31

def add_text_fields(fields)
  @text_fields.concat(Array(fields))
end

#all_fieldsObject

Get all scope and text fields associated with this setup as well as all inherited fields

Returns

Array

Collection of all text and scope fields associated with this setup



77
78
79
# File 'lib/sunspot/setup.rb', line 77

def all_fields
  fields + text_fields
end

#clazzObject

Return the class associated with this setup.

Returns

clazz<Class>

Class setup is configured for



98
99
100
# File 'lib/sunspot/setup.rb', line 98

def clazz
  Util.full_const_get(@class_name)
end

#fieldsObject

Get the fields associated with this setup as well as all inherited fields

Returns

Array

Collection of all fields associated with this setup



49
50
51
52
53
# File 'lib/sunspot/setup.rb', line 49

def fields
  fields = @fields.dup
  fields.concat(parent.fields) if parent
  fields
end

#indexer(connection) ⇒ Object

Factory method for an Indexer object configured to use this setup

Returns

Sunspot::Indexer

Indexer configured with this setup



87
88
89
# File 'lib/sunspot/setup.rb', line 87

def indexer(connection)
  Indexer.new(connection, self)
end

#setup(&block) ⇒ Object

Builder method for evaluating the setup DSL



38
39
40
# File 'lib/sunspot/setup.rb', line 38

def setup(&block)
  @dsl.instance_eval(&block)
end

#text_fieldsObject

Get the text fields associated with this setup as well as all inherited text fields

Returns

Array

Collection of all text fields associated with this setup



63
64
65
66
67
# File 'lib/sunspot/setup.rb', line 63

def text_fields
  text_fields = @text_fields.dup
  text_fields.concat(parent.text_fields) if parent
  text_fields
end