Skip to content

Commit

Permalink
Extract RSolr::Array.wrap and use in Generator#add (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer authored and jcoyne committed Oct 3, 2016
1 parent ecd31d6 commit ba3c339
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
17 changes: 16 additions & 1 deletion lib/rsolr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,20 @@ def self.solr_escape(str)
# so the result sent to Solr is ultimately a single backslash in front of the particular character
str.gsub(/([+\-&|!\(\)\{\}\[\]\^"~\*\?:\\\/])/, '\\\\\1')
end


module Array
def self.wrap(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
elsif object.is_a? Hash
[object]
elsif object.is_a? Enumerable
object
else
[object]
end
end
end
end
18 changes: 1 addition & 17 deletions lib/rsolr/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def field_by_name(name)
# document.add_field('title', 'A Title', :boost => 2.0)
#
def add_field(name, values, options = {})
wrap(values).each do |v|
RSolr::Array.wrap(values).each do |v|
next if v.nil?

field_attrs = { name: name }
Expand All @@ -54,22 +54,6 @@ def as_json
result[field] = v
end
end

private

def wrap(object)
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
elsif object.is_a? Hash
[object]
elsif object.is_a? Enumerable
object
else
[object]
end
end
end

class Field
Expand Down
2 changes: 1 addition & 1 deletion lib/rsolr/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module RSolr::JSON
class Generator
def add data, add_attrs = {}
add_attrs ||= {}
data = [data] unless data.is_a?(Array)
data = RSolr::Array.wrap(data)

if add_attrs.empty? && data.none? { |doc| doc.is_a?(RSolr::Document) && !doc.attrs.empty? }
data.map do |doc|
Expand Down
4 changes: 2 additions & 2 deletions lib/rsolr/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize_rsolr_response(request, response, result)
@request = request
@response = response
self.merge!(result)
if self["response"] && self["response"]["docs"].is_a?(Array)
if self["response"] && self["response"]["docs"].is_a?(::Array)
docs = PaginatedDocSet.new(self["response"]["docs"])
docs.per_page = request[:params]["rows"]
docs.page_start = request[:params]["start"]
Expand Down Expand Up @@ -41,7 +41,7 @@ def initialize(request, response, result)
end

# A response module which gets mixed into the solr ["response"]["docs"] array.
class PaginatedDocSet < Array
class PaginatedDocSet < ::Array

attr_accessor :page_start, :per_page, :page_total
if not (Object.const_defined?("RUBY_ENGINE") and Object::RUBY_ENGINE=='rbx')
Expand Down
2 changes: 1 addition & 1 deletion lib/rsolr/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.params_to_solr(params, escape = true)
# escape = false if we are here
mapped = params.map do |k, v|
next if v.to_s.empty?
if v.class == Array
if v.class == ::Array
params_to_solr(v.map { |x| [k, x] }, false)
else
"#{k}=#{v}"
Expand Down

0 comments on commit ba3c339

Please sign in to comment.