Module: Sunspot::Util

Defined in:
sunspot/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

+ (Object) Array(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 'sunspot/lib/sunspot/util.rb', line 102

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

+ (Object) camel_case(string)

Convert a string to camel case

Parameters

string

String to convert to camel case

Returns

String

String in camel case



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

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

+ (Object) deep_merge(left, right)

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 to merge

right

The other hash to merge

Returns

Hash

New hash containing the given hashes deep-merged.



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

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

+ (Object) deep_merge!(left, right)

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 'sunspot/lib/sunspot/util.rb', line 154

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

+ (Object) extract_options_from(args)



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

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

+ (Object) format_float(f, digits)

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 'sunspot/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

+ (Object) full_const_get(string)

Get a constant from a fully qualified name

Parameters

string

The fully qualified name of a constant

Returns

Object

Value of constant named



67
68
69
70
71
# File 'sunspot/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

+ (Object) instance_eval_or_call(object, &block)

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 to pass to the proc



82
83
84
85
86
87
88
# File 'sunspot/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

+ (Object) snake_case(string)

Convert a string to snake case

Parameters

string

String to convert to snake case

Returns

String

String in snake case



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

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

+ (Object) superclasses_for(clazz)

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

Parameters

clazz

class for which to get superclasses

Returns

Array

Collection containing class and its superclasses



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

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