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.



102
103
104
105
106
107
108
109
# File 'lib/sunspot/util.rb', line 102

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.



138
139
140
# File 'lib/sunspot/util.rb', line 138

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



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

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

.extract_options_from(args) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/sunspot/util.rb', line 90

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.



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

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



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

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



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

def instance_eval_or_call(object, &block)
  if block.arity > 0
    block.call(object)
  else
    ContextBoundDelegate.instance_eval_with_context(object, &block)
  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