Module: Sunspot::Util

Defined in:
lib/sunspot/util.rb

Overview

The Sunspot::Util module provides utility methods used elsewhere in the library.

Defined Under Namespace

Classes: ContextBoundDelegate, Coordinates

Class Method Summary collapse

Class Method Details

.Array(object) ⇒ Object

Ruby’s treatment of Strings as Enumerables is heavily annoying. As far as I know the behavior of Kernel.Array() is otherwise fine.



118
119
120
121
122
123
124
125
# File 'lib/sunspot/util.rb', line 118

def Array(object)
  case object
  when String, Hash
    [object]
  else
    super
  end
end

.camel_case(string) ⇒ Object

Convert a string to camel case

Parameters

string<String>

String to convert to camel case

Returns

String

String in camel case



52
53
54
# File 'lib/sunspot/util.rb', line 52

def camel_case(string)
  string.split('_').map! { |word| word.capitalize }.join
end

.deep_merge(left, right) ⇒ Object

Perform a deep merge of hashes, returning the result as a new hash. See #deep_merge_into for rules used to merge the hashes

Parameters

left<Hash>

Hash to merge

right<Hash>

The other hash to merge

Returns

Hash

New hash containing the given hashes deep-merged.



154
155
156
# File 'lib/sunspot/util.rb', line 154

def deep_merge(left, right)
  deep_merge_into({}, left, right)
end

.deep_merge!(left, right) ⇒ Object

Perform a deep merge of the right hash into the left hash

Parameters

left

Hash to receive merge

right

Hash to merge into left

Returns

Hash

left



170
171
172
# File 'lib/sunspot/util.rb', line 170

def deep_merge!(left, right)
  deep_merge_into(left, left, right)
end

.escape(value) ⇒ Object

Escapes characters for the Solr query parser

Parameters

string<String>

String to escape

Returns

String

escaped string



185
186
187
188
189
# File 'lib/sunspot/util.rb', line 185

def escape(value)
  # RSolr.solr_escape doesn't handle spaces or period chars,
  # which do need to be escaped
  RSolr.solr_escape(value).gsub(/([\s\.])/, '\\\\\1')
end

.extract_options_from(args) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/sunspot/util.rb', line 106

def extract_options_from(args)
  if args.last.is_a?(Hash)
    args.pop
  else
    {}
  end
end

.format_float(f, digits) ⇒ Object

When generating boosts, Solr requires that the values be in standard (not scientific) notation. We would like to ensure a minimum number of significant digits (i.e., digits that are not prefix zeros) for small float values.



133
134
135
136
137
138
139
# File 'lib/sunspot/util.rb', line 133

def format_float(f, digits)
  if f < 1
    sprintf('%.*f', digits - Math.log10(f), f)
  else
    f.to_s
  end
end

.full_const_get(string) ⇒ Object

Get a constant from a fully qualified name

Parameters

string<String>

The fully qualified name of a constant

Returns

Object

Value of constant named



83
84
85
86
87
# File 'lib/sunspot/util.rb', line 83

def full_const_get(string)
  string.split('::').inject(Object) do |context, const_name|
    context.const_defined?(const_name) ? context.const_get(const_name) : context.const_missing(const_name)
  end
end

.instance_eval_or_call(object, &block) ⇒ Object

Evaluate the given proc in the context of the given object if the block’s arity is non-positive, or by passing the given object as an argument if it is negative.

Parameters

object<Object>

Object to pass to the proc



98
99
100
101
102
103
104
# File 'lib/sunspot/util.rb', line 98

def instance_eval_or_call(object, &block)
  if block.arity > 0
    block.call(object)
  else
    ContextBoundDelegate.instance_eval_with_context(object, &block)
  end
end

.method_case(string) ⇒ Object

Convert snake case string to method case (Java style)

Parameters

string<String>

String to convert to method case

Returns

String

String in method case



67
68
69
70
# File 'lib/sunspot/util.rb', line 67

def method_case(string)
  first = true
  string.split('_').map! { |word| word = first ? word : word.capitalize; first = false; word }.join
end

.parse_json_facet(field_name, options, setup) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sunspot/util.rb', line 191

def parse_json_facet(field_name, options, setup)
  field = setup.field(field_name)
  if options[:range] || options[:time_range]
    unless [Sunspot::Type::TimeType, Sunspot::Type::FloatType, Sunspot::Type::IntegerType ].find{|type| field.type.is_a?(type)}
      raise(
        ArgumentError,
        ':range can only be specified for date, time, or numeric fields'
      )
    end
    facet_klass = field.type.is_a?(Sunspot::Type::TimeType) ? Sunspot::Query::DateFieldJsonFacet : Sunspot::Query::RangeJsonFacet
    facet_klass.new(field, options, setup)
  else
    Sunspot::Query::FieldJsonFacet.new(field, options, setup)
  end
end

.snake_case(string) ⇒ Object

Convert a string to snake case

Parameters

string<String>

String to convert to snake case

Returns

String

String in snake case



37
38
39
# File 'lib/sunspot/util.rb', line 37

def snake_case(string)
  string.scan(/(^|[A-Z])([^A-Z]+)/).map! { |word| word.join.downcase }.join('_')
end

.superclasses_for(clazz) ⇒ Object

Get all of the superclasses for a given class, including the class itself.

Parameters

clazz<Class>

class for which to get superclasses

Returns

Array

Collection containing class and its superclasses



20
21
22
23
24
# File 'lib/sunspot/util.rb', line 20

def superclasses_for(clazz)
  superclasses = [clazz]
  superclasses << (clazz = clazz.superclass) while clazz.superclass != Object
  superclasses
end