Sha256: acc0b9b737646f76515df0c0141f3c64ae93a5defe70f327c64cd3e374053748

Contents?: true

Size: 1.19 KB

Versions: 9

Compression:

Stored size: 1.19 KB

Contents

# module for adding helper methods to each solr response[:docs] object
module RSolr::Ext::Response::DocExt

  # Helper method to check if value/multi-values exist for a given key.
  # The value can be a string, or a RegExp
  # Example:
  # doc.has?(:location_facet)
  # doc.has?(:location_facet, 'Clemons')
  # doc.has?(:id, 'h009', /^u/i)
  def has?(k, *values)
    return if self[k].nil?
    return true if self.key?(k) and values.empty?
    target = self[k]
    if target.is_a?(Array)
      values.each do |val|
        return target.any?{|tv| val.is_a?(Regexp) ? (tv =~ val) : (tv==val)}
      end
    else
      return values.any? {|val| val.is_a?(Regexp) ? (target =~ val) : (target == val)}
    end
  end

  # helper
  # key is the name of the field
  # opts is a hash with the following valid keys:
  #  - :sep - a string used for joining multivalued field values
  #  - :default - a value to return when the key doesn't exist
  # if :sep is nil and the field is a multivalued field, the array is returned
  def get(key, opts={:sep=>', ', :default=>nil})
    if self.key? key
      val = self[key]
      (val.is_a?(Array) and opts[:sep]) ? val.join(opts[:sep]) : val
    else
      opts[:default]
    end
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mwmitchell-rsolr-ext-0.5.0 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.1 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.2 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.3 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.4 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.5 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.6 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.7 lib/rsolr-ext/response/doc_ext.rb
mwmitchell-rsolr-ext-0.5.8 lib/rsolr-ext/response/doc_ext.rb