Module: Sunspot::Util

Defined in:
lib/sunspot/util.rb

Overview

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

Class Method Summary collapse

Class Method Details

.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.



103
104
105
# File 'lib/sunspot/util.rb', line 103

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



119
120
121
# File 'lib/sunspot/util.rb', line 119

def deep_merge!(left, right)
  deep_merge_into(left, left, right)
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_get(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
    object.instance_eval(&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