dpickett-thinking-sphinx 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENCE +20 -0
- data/README +107 -0
- data/lib/thinking_sphinx/active_record/delta.rb +74 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
- data/lib/thinking_sphinx/active_record/search.rb +57 -0
- data/lib/thinking_sphinx/active_record.rb +245 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +34 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +53 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +129 -0
- data/lib/thinking_sphinx/association.rb +144 -0
- data/lib/thinking_sphinx/attribute.rb +254 -0
- data/lib/thinking_sphinx/class_facet.rb +20 -0
- data/lib/thinking_sphinx/collection.rb +142 -0
- data/lib/thinking_sphinx/configuration.rb +236 -0
- data/lib/thinking_sphinx/core/string.rb +22 -0
- data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +65 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
- data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
- data/lib/thinking_sphinx/deltas.rb +22 -0
- data/lib/thinking_sphinx/facet.rb +58 -0
- data/lib/thinking_sphinx/facet_collection.rb +45 -0
- data/lib/thinking_sphinx/field.rb +172 -0
- data/lib/thinking_sphinx/index/builder.rb +233 -0
- data/lib/thinking_sphinx/index/faux_column.rb +110 -0
- data/lib/thinking_sphinx/index.rb +432 -0
- data/lib/thinking_sphinx/rails_additions.rb +133 -0
- data/lib/thinking_sphinx/search.rb +654 -0
- data/lib/thinking_sphinx/tasks.rb +128 -0
- data/lib/thinking_sphinx.rb +145 -0
- data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
- data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
- data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
- data/spec/unit/thinking_sphinx/active_record_spec.rb +256 -0
- data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
- data/spec/unit/thinking_sphinx/attribute_spec.rb +212 -0
- data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
- data/spec/unit/thinking_sphinx/configuration_spec.rb +136 -0
- data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/unit/thinking_sphinx/field_spec.rb +145 -0
- data/spec/unit/thinking_sphinx/index/builder_spec.rb +5 -0
- data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
- data/spec/unit/thinking_sphinx/index_spec.rb +54 -0
- data/spec/unit/thinking_sphinx/search_spec.rb +59 -0
- data/spec/unit/thinking_sphinx_spec.rb +129 -0
- data/tasks/distribution.rb +48 -0
- data/tasks/rails.rake +1 -0
- data/tasks/testing.rb +86 -0
- data/vendor/after_commit/LICENSE +20 -0
- data/vendor/after_commit/README +16 -0
- data/vendor/after_commit/Rakefile +22 -0
- data/vendor/after_commit/init.rb +5 -0
- data/vendor/after_commit/lib/after_commit/active_record.rb +91 -0
- data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
- data/vendor/after_commit/lib/after_commit.rb +42 -0
- data/vendor/after_commit/test/after_commit_test.rb +53 -0
- data/vendor/delayed_job/lib/delayed/job.rb +251 -0
- data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
- data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
- data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
- data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
- data/vendor/riddle/lib/riddle/client/message.rb +65 -0
- data/vendor/riddle/lib/riddle/client/response.rb +84 -0
- data/vendor/riddle/lib/riddle/client.rb +619 -0
- data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
- data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
- data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
- data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
- data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
- data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
- data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
- data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
- data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
- data/vendor/riddle/lib/riddle/configuration.rb +33 -0
- data/vendor/riddle/lib/riddle/controller.rb +44 -0
- data/vendor/riddle/lib/riddle.rb +30 -0
- metadata +158 -0
@@ -0,0 +1,172 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
# Fields - holding the string data which Sphinx indexes for your searches.
|
3
|
+
# This class isn't really useful to you unless you're hacking around with the
|
4
|
+
# internals of Thinking Sphinx - but hey, don't let that stop you.
|
5
|
+
#
|
6
|
+
# One key thing to remember - if you're using the field manually to
|
7
|
+
# generate SQL statements, you'll need to set the base model, and all the
|
8
|
+
# associations. Which can get messy. Use Index.link!, it really helps.
|
9
|
+
#
|
10
|
+
class Field
|
11
|
+
attr_accessor :alias, :columns, :sortable, :associations, :model, :infixes,
|
12
|
+
:prefixes, :faceted
|
13
|
+
|
14
|
+
# To create a new field, you'll need to pass in either a single Column
|
15
|
+
# or an array of them, and some (optional) options. The columns are
|
16
|
+
# references to the data that will make up the field.
|
17
|
+
#
|
18
|
+
# Valid options are:
|
19
|
+
# - :as => :alias_name
|
20
|
+
# - :sortable => true
|
21
|
+
# - :infixes => true
|
22
|
+
# - :prefixes => true
|
23
|
+
#
|
24
|
+
# Alias is only required in three circumstances: when there's
|
25
|
+
# another attribute or field with the same name, when the column name is
|
26
|
+
# 'id', or when there's more than one column.
|
27
|
+
#
|
28
|
+
# Sortable defaults to false - but is quite useful when set to true, as
|
29
|
+
# it creates an attribute with the same string value (which Sphinx converts
|
30
|
+
# to an integer value), which can be sorted by. Thinking Sphinx is smart
|
31
|
+
# enough to realise that when you specify fields in sort statements, you
|
32
|
+
# mean their respective attributes.
|
33
|
+
#
|
34
|
+
# If you have partial matching enabled (ie: enable_star), then you can
|
35
|
+
# specify certain fields to have their prefixes and infixes indexed. Keep
|
36
|
+
# in mind, though, that Sphinx's default is _all_ fields - so once you
|
37
|
+
# highlight a particular field, no other fields in the index will have
|
38
|
+
# these partial indexes.
|
39
|
+
#
|
40
|
+
# Here's some examples:
|
41
|
+
#
|
42
|
+
# Field.new(
|
43
|
+
# Column.new(:name)
|
44
|
+
# )
|
45
|
+
#
|
46
|
+
# Field.new(
|
47
|
+
# [Column.new(:first_name), Column.new(:last_name)],
|
48
|
+
# :as => :name, :sortable => true
|
49
|
+
# )
|
50
|
+
#
|
51
|
+
# Field.new(
|
52
|
+
# [Column.new(:posts, :subject), Column.new(:posts, :content)],
|
53
|
+
# :as => :posts, :prefixes => true
|
54
|
+
# )
|
55
|
+
#
|
56
|
+
def initialize(columns, options = {})
|
57
|
+
@columns = Array(columns)
|
58
|
+
@associations = {}
|
59
|
+
|
60
|
+
raise "Cannot define a field with no columns. Maybe you are trying to index a field with a reserved name (id, name). You can fix this error by using a symbol rather than a bare name (:id instead of id)." if @columns.empty? || @columns.any? { |column| !column.respond_to?(:__stack) }
|
61
|
+
|
62
|
+
@alias = options[:as]
|
63
|
+
@sortable = options[:sortable] || false
|
64
|
+
@infixes = options[:infixes] || false
|
65
|
+
@prefixes = options[:prefixes] || false
|
66
|
+
@faceted = options[:facet] || false
|
67
|
+
end
|
68
|
+
|
69
|
+
# Get the part of the SELECT clause related to this field. Don't forget
|
70
|
+
# to set your model and associations first though.
|
71
|
+
#
|
72
|
+
# This will concatenate strings if there's more than one data source or
|
73
|
+
# multiple data values (has_many or has_and_belongs_to_many associations).
|
74
|
+
#
|
75
|
+
def to_select_sql
|
76
|
+
clause = @columns.collect { |column|
|
77
|
+
column_with_prefix(column)
|
78
|
+
}.join(', ')
|
79
|
+
|
80
|
+
clause = adapter.concatenate(clause) if concat_ws?
|
81
|
+
clause = adapter.group_concatenate(clause) if is_many?
|
82
|
+
|
83
|
+
"#{adapter.cast_to_string clause } AS #{quote_column(unique_name)}"
|
84
|
+
end
|
85
|
+
|
86
|
+
# Get the part of the GROUP BY clause related to this field - if one is
|
87
|
+
# needed. If not, all you'll get back is nil. The latter will happen if
|
88
|
+
# there's multiple data values (read: a has_many or has_and_belongs_to_many
|
89
|
+
# association).
|
90
|
+
#
|
91
|
+
def to_group_sql
|
92
|
+
case
|
93
|
+
when is_many?, ThinkingSphinx.use_group_by_shortcut?
|
94
|
+
nil
|
95
|
+
else
|
96
|
+
@columns.collect { |column|
|
97
|
+
column_with_prefix(column)
|
98
|
+
}
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Returns the unique name of the field - which is either the alias of
|
103
|
+
# the field, or the name of the only column - if there is only one. If
|
104
|
+
# there isn't, there should be an alias. Else things probably won't work.
|
105
|
+
# Consider yourself warned.
|
106
|
+
#
|
107
|
+
def unique_name
|
108
|
+
if @columns.length == 1
|
109
|
+
@alias || @columns.first.__name
|
110
|
+
else
|
111
|
+
@alias
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def to_facet
|
116
|
+
return nil unless @faceted
|
117
|
+
|
118
|
+
ThinkingSphinx::Facet.new(self)
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def adapter
|
124
|
+
@adapter ||= @model.sphinx_database_adapter
|
125
|
+
end
|
126
|
+
|
127
|
+
def quote_column(column)
|
128
|
+
@model.connection.quote_column_name(column)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Indication of whether the columns should be concatenated with a space
|
132
|
+
# between each value. True if there's either multiple sources or multiple
|
133
|
+
# associations.
|
134
|
+
#
|
135
|
+
def concat_ws?
|
136
|
+
@columns.length > 1 || multiple_associations?
|
137
|
+
end
|
138
|
+
|
139
|
+
# Checks whether any column requires multiple associations (which only
|
140
|
+
# happens for polymorphic situations).
|
141
|
+
#
|
142
|
+
def multiple_associations?
|
143
|
+
associations.any? { |col,assocs| assocs.length > 1 }
|
144
|
+
end
|
145
|
+
|
146
|
+
# Builds a column reference tied to the appropriate associations. This
|
147
|
+
# dives into the associations hash and their corresponding joins to
|
148
|
+
# figure out how to correctly reference a column in SQL.
|
149
|
+
#
|
150
|
+
def column_with_prefix(column)
|
151
|
+
if column.is_string?
|
152
|
+
column.__name
|
153
|
+
elsif associations[column].empty?
|
154
|
+
"#{@model.quoted_table_name}.#{quote_column(column.__name)}"
|
155
|
+
else
|
156
|
+
associations[column].collect { |assoc|
|
157
|
+
assoc.has_column?(column.__name) ?
|
158
|
+
"#{@model.connection.quote_table_name(assoc.join.aliased_table_name)}" +
|
159
|
+
".#{quote_column(column.__name)}" :
|
160
|
+
nil
|
161
|
+
}.compact.join(', ')
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# Could there be more than one value related to the parent record? If so,
|
166
|
+
# then this will return true. If not, false. It's that simple.
|
167
|
+
#
|
168
|
+
def is_many?
|
169
|
+
associations.values.flatten.any? { |assoc| assoc.is_many? }
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
@@ -0,0 +1,233 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class Index
|
3
|
+
# The Builder class is the core for the index definition block processing.
|
4
|
+
# There are four methods you really need to pay attention to:
|
5
|
+
# - indexes (aliased to includes and attribute)
|
6
|
+
# - has (aliased to attribute)
|
7
|
+
# - where
|
8
|
+
# - set_property (aliased to set_properties)
|
9
|
+
#
|
10
|
+
# The first two of these methods allow you to define what data makes up
|
11
|
+
# your indexes. #where provides a method to add manual SQL conditions, and
|
12
|
+
# set_property allows you to set some settings on a per-index basis. Check
|
13
|
+
# out each method's documentation for better ideas of usage.
|
14
|
+
#
|
15
|
+
class Builder
|
16
|
+
class << self
|
17
|
+
# No idea where this is coming from - haven't found it in any ruby or
|
18
|
+
# rails documentation. It's not needed though, so it gets undef'd.
|
19
|
+
# Hopefully the list of methods that get in the way doesn't get too
|
20
|
+
# long.
|
21
|
+
HiddenMethods = [:parent, :name, :id, :type].each { |method|
|
22
|
+
define_method(method) {
|
23
|
+
caller.grep(/irb.completion/).empty? ? method_missing(method) : super
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
attr_accessor :fields, :attributes, :properties, :conditions,
|
28
|
+
:groupings
|
29
|
+
|
30
|
+
# Set up all the collections. Consider this the equivalent of an
|
31
|
+
# instance's initialize method.
|
32
|
+
#
|
33
|
+
def setup
|
34
|
+
@fields = []
|
35
|
+
@attributes = []
|
36
|
+
@properties = {}
|
37
|
+
@conditions = []
|
38
|
+
@groupings = []
|
39
|
+
end
|
40
|
+
|
41
|
+
# This is how you add fields - the strings Sphinx looks at - to your
|
42
|
+
# index. Technically, to use this method, you need to pass in some
|
43
|
+
# columns and options - but there's some neat method_missing stuff
|
44
|
+
# happening, so lets stick to the expected syntax within a define_index
|
45
|
+
# block.
|
46
|
+
#
|
47
|
+
# Expected options are :as, which points to a column alias in symbol
|
48
|
+
# form, and :sortable, which indicates whether you want to sort by this
|
49
|
+
# field.
|
50
|
+
#
|
51
|
+
# Adding Single-Column Fields:
|
52
|
+
#
|
53
|
+
# You can use symbols or methods - and can chain methods together to
|
54
|
+
# get access down the associations tree.
|
55
|
+
#
|
56
|
+
# indexes :id, :as => :my_id
|
57
|
+
# indexes :name, :sortable => true
|
58
|
+
# indexes first_name, last_name, :sortable => true
|
59
|
+
# indexes users.posts.content, :as => :post_content
|
60
|
+
# indexes users(:id), :as => :user_ids
|
61
|
+
#
|
62
|
+
# Keep in mind that if any keywords for Ruby methods - such as id or
|
63
|
+
# name - clash with your column names, you need to use the symbol
|
64
|
+
# version (see the first, second and last examples above).
|
65
|
+
#
|
66
|
+
# If you specify multiple columns (example #2), a field will be created
|
67
|
+
# for each. Don't use the :as option in this case. If you want to merge
|
68
|
+
# those columns together, continue reading.
|
69
|
+
#
|
70
|
+
# Adding Multi-Column Fields:
|
71
|
+
#
|
72
|
+
# indexes [first_name, last_name], :as => :name
|
73
|
+
# indexes [location, parent.location], :as => :location
|
74
|
+
#
|
75
|
+
# To combine multiple columns into a single field, you need to wrap
|
76
|
+
# them in an Array, as shown by the above examples. There's no
|
77
|
+
# limitations on whether they're symbols or methods or what level of
|
78
|
+
# associations they come from.
|
79
|
+
#
|
80
|
+
# Adding SQL Fragment Fields
|
81
|
+
#
|
82
|
+
# You can also define a field using an SQL fragment, useful for when
|
83
|
+
# you would like to index a calculated value.
|
84
|
+
#
|
85
|
+
# indexes "age < 18", :as => :minor
|
86
|
+
#
|
87
|
+
def indexes(*args)
|
88
|
+
options = args.extract_options!
|
89
|
+
args.each do |columns|
|
90
|
+
fields << Field.new(FauxColumn.coerce(columns), options)
|
91
|
+
|
92
|
+
if fields.last.sortable || fields.last.faceted
|
93
|
+
attributes << Attribute.new(
|
94
|
+
fields.last.columns.collect { |col| col.clone },
|
95
|
+
options.merge(
|
96
|
+
:type => :string,
|
97
|
+
:as => fields.last.unique_name.to_s.concat("_sort").to_sym
|
98
|
+
).except(:facet)
|
99
|
+
)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
alias_method :field, :indexes
|
104
|
+
alias_method :includes, :indexes
|
105
|
+
|
106
|
+
# This is the method to add attributes to your index (hence why it is
|
107
|
+
# aliased as 'attribute'). The syntax is the same as #indexes, so use
|
108
|
+
# that as starting point, but keep in mind the following points.
|
109
|
+
#
|
110
|
+
# An attribute can have an alias (the :as option), but it is always
|
111
|
+
# sortable - so you don't need to explicitly request that. You _can_
|
112
|
+
# specify the data type of the attribute (the :type option), but the
|
113
|
+
# code's pretty good at figuring that out itself from peering into the
|
114
|
+
# database.
|
115
|
+
#
|
116
|
+
# Attributes are limited to the following types: integers, floats,
|
117
|
+
# datetimes (converted to timestamps), booleans and strings. Don't
|
118
|
+
# forget that Sphinx converts string attributes to integers, which are
|
119
|
+
# useful for sorting, but that's about it.
|
120
|
+
#
|
121
|
+
# You can also have a collection of integers for multi-value attributes
|
122
|
+
# (MVAs). Generally these would be through a has_many relationship,
|
123
|
+
# like in this example:
|
124
|
+
#
|
125
|
+
# has posts(:id), :as => :post_ids
|
126
|
+
#
|
127
|
+
# This allows you to filter on any of the values tied to a specific
|
128
|
+
# record. Might be best to read through the Sphinx documentation to get
|
129
|
+
# a better idea of that though.
|
130
|
+
#
|
131
|
+
# Adding SQL Fragment Attributes
|
132
|
+
#
|
133
|
+
# You can also define an attribute using an SQL fragment, useful for
|
134
|
+
# when you would like to index a calculated value. Don't forget to set
|
135
|
+
# the type of the attribute though:
|
136
|
+
#
|
137
|
+
# has "age < 18", :as => :minor, :type => :boolean
|
138
|
+
#
|
139
|
+
# If you're creating attributes for latitude and longitude, don't
|
140
|
+
# forget that Sphinx expects these values to be in radians.
|
141
|
+
#
|
142
|
+
def has(*args)
|
143
|
+
options = args.extract_options!
|
144
|
+
args.each do |columns|
|
145
|
+
attributes << Attribute.new(FauxColumn.coerce(columns), options)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
alias_method :attribute, :has
|
149
|
+
|
150
|
+
def facet(*args)
|
151
|
+
options = args.extract_options!
|
152
|
+
options[:facet] = true
|
153
|
+
|
154
|
+
args.each do |columns|
|
155
|
+
attributes << Attribute.new(FauxColumn.coerce(columns), options)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Use this method to add some manual SQL conditions for your index
|
160
|
+
# request. You can pass in as many strings as you like, they'll get
|
161
|
+
# joined together with ANDs later on.
|
162
|
+
#
|
163
|
+
# where "user_id = 10"
|
164
|
+
# where "parent_type = 'Article'", "created_at < NOW()"
|
165
|
+
#
|
166
|
+
def where(*args)
|
167
|
+
@conditions += args
|
168
|
+
end
|
169
|
+
|
170
|
+
# Use this method to add some manual SQL strings to the GROUP BY
|
171
|
+
# clause. You can pass in as many strings as you'd like, they'll get
|
172
|
+
# joined together with commas later on.
|
173
|
+
#
|
174
|
+
# group_by "lat", "lng"
|
175
|
+
#
|
176
|
+
def group_by(*args)
|
177
|
+
@groupings += args
|
178
|
+
end
|
179
|
+
|
180
|
+
# This is what to use to set properties on the index. Chief amongst
|
181
|
+
# those is the delta property - to allow automatic updates to your
|
182
|
+
# indexes as new models are added and edited - but also you can
|
183
|
+
# define search-related properties which will be the defaults for all
|
184
|
+
# searches on the model.
|
185
|
+
#
|
186
|
+
# set_property :delta => true
|
187
|
+
# set_property :field_weights => {"name" => 100}
|
188
|
+
# set_property :order => "name ASC"
|
189
|
+
# set_property :include => :picture
|
190
|
+
# set_property :select => 'name'
|
191
|
+
#
|
192
|
+
# Also, the following two properties are particularly relevant for
|
193
|
+
# geo-location searching - latitude_attr and longitude_attr. If your
|
194
|
+
# attributes for these two values are named something other than
|
195
|
+
# lat/latitude or lon/long/longitude, you can dictate what they are
|
196
|
+
# when defining the index, so you don't need to specify them for every
|
197
|
+
# geo-related search.
|
198
|
+
#
|
199
|
+
# set_property :latitude_attr => "lt", :longitude_attr => "lg"
|
200
|
+
#
|
201
|
+
# Please don't forget to add a boolean field named 'delta' to your
|
202
|
+
# model's database table if enabling the delta index for it.
|
203
|
+
#
|
204
|
+
def set_property(*args)
|
205
|
+
options = args.extract_options!
|
206
|
+
if options.empty?
|
207
|
+
@properties[args[0]] = args[1]
|
208
|
+
else
|
209
|
+
@properties.merge!(options)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
alias_method :set_properties, :set_property
|
213
|
+
|
214
|
+
# Handles the generation of new columns for the field and attribute
|
215
|
+
# definitions.
|
216
|
+
#
|
217
|
+
def method_missing(method, *args)
|
218
|
+
FauxColumn.new(method, *args)
|
219
|
+
end
|
220
|
+
|
221
|
+
# A method to allow adding fields from associations which have names
|
222
|
+
# that clash with method names in the Builder class (ie: properties,
|
223
|
+
# fields, attributes).
|
224
|
+
#
|
225
|
+
# Example: indexes assoc(:properties).column
|
226
|
+
#
|
227
|
+
def assoc(assoc)
|
228
|
+
FauxColumn.new(method)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class Index
|
3
|
+
# Instances of this class represent database columns and the stack of
|
4
|
+
# associations that lead from the base model to them.
|
5
|
+
#
|
6
|
+
# The name and stack are accessible through methods starting with __ to
|
7
|
+
# avoid conflicting with the method_missing calls that build the stack.
|
8
|
+
#
|
9
|
+
class FauxColumn
|
10
|
+
# Create a new column with a pre-defined stack. The top element in the
|
11
|
+
# stack will get shifted to be the name value.
|
12
|
+
#
|
13
|
+
def initialize(*stack)
|
14
|
+
@name = stack.pop
|
15
|
+
@stack = stack
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.coerce(columns)
|
19
|
+
case columns
|
20
|
+
when Symbol, String
|
21
|
+
FauxColumn.new(columns)
|
22
|
+
when Array
|
23
|
+
columns.collect { |col| FauxColumn.coerce(col) }
|
24
|
+
when FauxColumn
|
25
|
+
columns
|
26
|
+
else
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Can't use normal method name, as that could be an association or
|
32
|
+
# column name.
|
33
|
+
#
|
34
|
+
def __name
|
35
|
+
@name
|
36
|
+
end
|
37
|
+
|
38
|
+
# Can't use normal method name, as that could be an association or
|
39
|
+
# column name.
|
40
|
+
#
|
41
|
+
def __stack
|
42
|
+
@stack
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns true if the stack is empty *and* if the name is a string -
|
46
|
+
# which is an indication that of raw SQL, as opposed to a value from a
|
47
|
+
# table's column.
|
48
|
+
#
|
49
|
+
def is_string?
|
50
|
+
@name.is_a?(String) && @stack.empty?
|
51
|
+
end
|
52
|
+
|
53
|
+
# This handles any 'invalid' method calls and sets them as the name,
|
54
|
+
# and pushing the previous name into the stack. The object returns
|
55
|
+
# itself.
|
56
|
+
#
|
57
|
+
# If there's a single argument, it becomes the name, and the method
|
58
|
+
# symbol goes into the stack as well. Multiple arguments means new
|
59
|
+
# columns with the original stack and new names (from each argument) gets
|
60
|
+
# returned.
|
61
|
+
#
|
62
|
+
# Easier to explain with examples:
|
63
|
+
#
|
64
|
+
# col = FauxColumn.new :a, :b, :c
|
65
|
+
# col.__name #=> :c
|
66
|
+
# col.__stack #=> [:a, :b]
|
67
|
+
#
|
68
|
+
# col.whatever #=> col
|
69
|
+
# col.__name #=> :whatever
|
70
|
+
# col.__stack #=> [:a, :b, :c]
|
71
|
+
#
|
72
|
+
# col.something(:id) #=> col
|
73
|
+
# col.__name #=> :id
|
74
|
+
# col.__stack #=> [:a, :b, :c, :whatever, :something]
|
75
|
+
#
|
76
|
+
# cols = col.short(:x, :y, :z)
|
77
|
+
# cols[0].__name #=> :x
|
78
|
+
# cols[0].__stack #=> [:a, :b, :c, :whatever, :something, :short]
|
79
|
+
# cols[1].__name #=> :y
|
80
|
+
# cols[1].__stack #=> [:a, :b, :c, :whatever, :something, :short]
|
81
|
+
# cols[2].__name #=> :z
|
82
|
+
# cols[2].__stack #=> [:a, :b, :c, :whatever, :something, :short]
|
83
|
+
#
|
84
|
+
# Also, this allows method chaining to build up a relevant stack:
|
85
|
+
#
|
86
|
+
# col = FauxColumn.new :a, :b
|
87
|
+
# col.__name #=> :b
|
88
|
+
# col.__stack #=> [:a]
|
89
|
+
#
|
90
|
+
# col.one.two.three #=> col
|
91
|
+
# col.__name #=> :three
|
92
|
+
# col.__stack #=> [:a, :b, :one, :two]
|
93
|
+
#
|
94
|
+
def method_missing(method, *args)
|
95
|
+
@stack << @name
|
96
|
+
@name = method
|
97
|
+
|
98
|
+
if (args.empty?)
|
99
|
+
self
|
100
|
+
elsif (args.length == 1)
|
101
|
+
method_missing(args.first)
|
102
|
+
else
|
103
|
+
args.collect { |arg|
|
104
|
+
FauxColumn.new(@stack + [@name, arg])
|
105
|
+
}
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|