Skip to content

Commit

Permalink
Merge pull request #28 from projecthydra/refactor_format_node_value
Browse files Browse the repository at this point in the history
Refactor format_node_value so we don't need to check responds_to?
  • Loading branch information
awead committed Jun 9, 2014
2 parents 2c8164f + 17777a1 commit eb59692
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions lib/solrizer/extractor.rb
Expand Up @@ -9,34 +9,35 @@ module Solrizer
#
class Extractor

# Insert +field_value+ for +field_name+ into +solr_doc+
# Handles inserting new values into a Hash while ensuring that you don't destroy or overwrite any existing values in the hash.
# Ensures that field values are always appended to arrays within the values hash.
# Also ensures that values are run through format_node_value
# @param [Hash] solr_doc
# @param [String] field_name
# @param [String] field_value
def self.insert_solr_field_value(solr_doc, field_name, field_value)
formatted_value = self.format_node_value(field_value)
if solr_doc[field_name]
solr_doc[field_name] = Array(solr_doc[field_name]) << formatted_value
else
solr_doc[field_name] = formatted_value
class << self
# Insert +field_value+ for +field_name+ into +solr_doc+
# Handles inserting new values into a Hash while ensuring that you don't destroy or overwrite any existing values in the hash.
# Ensures that field values are always appended to arrays within the values hash.
# Also ensures that values are run through format_node_value
# @param [Hash] solr_doc
# @param [String] field_name
# @param [String] field_value
def insert_solr_field_value(solr_doc, field_name, field_value)
formatted_value = format_node_value(field_value)
if solr_doc[field_name]
solr_doc[field_name] = Array(solr_doc[field_name]) << formatted_value
else
solr_doc[field_name] = formatted_value
end
return solr_doc
end
return solr_doc
end

# Strips the majority of whitespace from the values array and then joins them with a single blank delimitter
# Returns an empty string if values argument is nil
#
# @param [Array] values Array of strings representing the values to be formatted
# @return [String]
def self.format_node_value values
if values.nil?
return ""
else
values = [values] unless values.respond_to? :map
return values.map{|val| val.gsub(/\s+/,' ').strip}.join(" ")

# Strips the majority of whitespace from the values array and then joins them with a single blank delimitter
# Returns an empty string if values argument is nil
#
# @param [Array] values Array of strings representing the values to be formatted
# @return [String]
def format_node_value values
if values.nil?
""
else
Array(values).map{|val| val.gsub(/\s+/,' ').strip}.join(" ")
end
end
end

Expand Down

0 comments on commit eb59692

Please sign in to comment.