mwmitchell-rsolr-ext 0.5.2 → 0.5.3
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/lib/rsolr-ext/hash_methodizer.rb +43 -0
- data/lib/rsolr-ext/mapable.rb +44 -0
- data/lib/rsolr-ext/request/queryable.rb +42 -0
- data/lib/rsolr-ext.rb +1 -1
- data/rsolr-ext.gemspec +4 -4
- metadata +4 -4
@@ -0,0 +1,43 @@
|
|
1
|
+
#
|
2
|
+
# A hash modifier that creates method readers from key names.
|
3
|
+
# NOTE: reader methods are created recursively.
|
4
|
+
# The method names are the same as the key names,
|
5
|
+
# except that the values are snake-cased, for example:
|
6
|
+
# - QTime -> q_time
|
7
|
+
# - debugQuery -> debug_query
|
8
|
+
#
|
9
|
+
class RSolr::Ext::HashMethodizer
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
def snake_case(v)
|
14
|
+
v = v.to_s
|
15
|
+
return v.downcase if v =~ /^[A-Z]+$/
|
16
|
+
v.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
|
17
|
+
return $+.downcase
|
18
|
+
end
|
19
|
+
|
20
|
+
def methodize!(h)
|
21
|
+
h.keys.each do |k|
|
22
|
+
meth = snake_case(k)
|
23
|
+
h.instance_variable_set("@#{k}", h[k])
|
24
|
+
h.instance_eval <<-RUBY
|
25
|
+
def #{meth}
|
26
|
+
val = @#{k}
|
27
|
+
if val.respond_to?(:each_pair)
|
28
|
+
RSolr::Ext::HashMethodizer.methodize!(val)
|
29
|
+
elsif val.is_a?(Array)
|
30
|
+
val.each do |item|
|
31
|
+
RSolr::Ext::HashMethodizer.methodize!(item) if item.respond_to?(:each_pair)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
val
|
35
|
+
end
|
36
|
+
RUBY
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# A module that provides method mapping capabilities.
|
2
|
+
# The basic idea is to pass in a hash to the #map method,
|
3
|
+
# the map method then goes through a list of keys (MAPPED_PARAMS) to
|
4
|
+
# be processed. Each key name can match a key in the input hash.
|
5
|
+
# If there is a match, a method by the name of "map_#{key}" is
|
6
|
+
# called with the following args: input[key], output_hash
|
7
|
+
# The method is responsible for processing the value.
|
8
|
+
# The return value from the method is not used.
|
9
|
+
#
|
10
|
+
# For example: if the mapped params list has :query,
|
11
|
+
# there should be a method like: map_query(input_value, output_hash)
|
12
|
+
# The output_hash is the final hash the the #map method returns,
|
13
|
+
# so whatever you do to output_hash gets returned in the end.
|
14
|
+
module RSolr::Ext::Mapable
|
15
|
+
|
16
|
+
# accepts an input hash.
|
17
|
+
# prepares a return hash by copying the input.
|
18
|
+
# runs through all of the keys in MAPPED_PARAMS.
|
19
|
+
# calls any mapper methods that match the current key in MAPPED_PARAMS.
|
20
|
+
# The mapped keys from the input hash are deleted.
|
21
|
+
# returns a new hash.
|
22
|
+
def map(input)
|
23
|
+
result = input.dup
|
24
|
+
self.class::MAPPED_PARAMS.each do |meth|
|
25
|
+
input_value = result.delete(meth)
|
26
|
+
next if input_value.to_s.empty?
|
27
|
+
send("map_#{meth}", input_value, result)
|
28
|
+
end
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
# creates an array where the "existing_value" param is first
|
33
|
+
# and the "new_value" is the last.
|
34
|
+
# All empty/nil items are removed.
|
35
|
+
# the return result is either the result of the
|
36
|
+
# array being joined on a space, or the array itself.
|
37
|
+
# "auto_join" should be true or false.
|
38
|
+
def append_to_param(existing_value, new_value, auto_join=true)
|
39
|
+
values = [existing_value, new_value]
|
40
|
+
values.delete_if{|v|v.nil?}
|
41
|
+
auto_join ? values.join(' ') : values
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# a module to help the creation of solr queries.
|
2
|
+
module RSolr::Ext::Request::Queryable
|
3
|
+
|
4
|
+
# Wraps a string around double quotes
|
5
|
+
def quote(value)
|
6
|
+
%("#{value}")
|
7
|
+
end
|
8
|
+
|
9
|
+
# builds a solr range query from a Range object
|
10
|
+
def build_range(r)
|
11
|
+
"[#{r.min} TO #{r.max}]"
|
12
|
+
end
|
13
|
+
|
14
|
+
# builds a solr query fragment
|
15
|
+
# if "quote_string" is true, the values will be quoted.
|
16
|
+
# if "value" is a string/symbol, the #to_s method is called
|
17
|
+
# if the "value" is an array, each item in the array is
|
18
|
+
# send to build_query (recursive)
|
19
|
+
# if the "value" is a Hash, a fielded query is built
|
20
|
+
# where the keys are used as the field names and
|
21
|
+
# the values are either processed as a Range or
|
22
|
+
# passed back into build_query (recursive)
|
23
|
+
def build_query(value, quote_string=false)
|
24
|
+
case value
|
25
|
+
when String,Symbol
|
26
|
+
return quote_string ? quote(value.to_s) : value.to_s
|
27
|
+
when Array
|
28
|
+
value.collect do |v|
|
29
|
+
build_query(v, quote_string)
|
30
|
+
end.flatten
|
31
|
+
when Hash
|
32
|
+
return value.collect do |(k,v)|
|
33
|
+
if v.is_a?(Range)
|
34
|
+
"#{k}:#{build_range(v)}"
|
35
|
+
else
|
36
|
+
"#{k}:#{build_query(v, quote_string)}"
|
37
|
+
end
|
38
|
+
end.flatten
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/rsolr-ext.rb
CHANGED
data/rsolr-ext.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "rsolr-ext"
|
3
|
-
s.version = "0.5.
|
3
|
+
s.version = "0.5.3"
|
4
4
|
s.date = "2009-03-17"
|
5
5
|
s.summary = "An extension lib for RSolr"
|
6
6
|
s.email = "goodieboy@gmail.com"
|
@@ -13,10 +13,10 @@ Gem::Specification.new do |s|
|
|
13
13
|
"lib/core_ext.rb",
|
14
14
|
"lib/mash.rb",
|
15
15
|
|
16
|
-
"lib/rsolr-ext/hash_methodizer",
|
16
|
+
"lib/rsolr-ext/hash_methodizer.rb",
|
17
|
+
"lib/rsolr-ext/mapable.rb",
|
17
18
|
|
18
|
-
"lib/rsolr-ext/request/
|
19
|
-
"lib/rsolr-ext/request/standard.rb",
|
19
|
+
"lib/rsolr-ext/request/queryable.rb",
|
20
20
|
"lib/rsolr-ext/request.rb",
|
21
21
|
|
22
22
|
"lib/rsolr-ext/response/doc_ext.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mwmitchell-rsolr-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Mitchell
|
@@ -25,9 +25,9 @@ extra_rdoc_files:
|
|
25
25
|
files:
|
26
26
|
- lib/core_ext.rb
|
27
27
|
- lib/mash.rb
|
28
|
-
- lib/rsolr-ext/hash_methodizer
|
29
|
-
- lib/rsolr-ext/
|
30
|
-
- lib/rsolr-ext/request/
|
28
|
+
- lib/rsolr-ext/hash_methodizer.rb
|
29
|
+
- lib/rsolr-ext/mapable.rb
|
30
|
+
- lib/rsolr-ext/request/queryable.rb
|
31
31
|
- lib/rsolr-ext/request.rb
|
32
32
|
- lib/rsolr-ext/response/doc_ext.rb
|
33
33
|
- lib/rsolr-ext/response/facet_paginator.rb
|