Module: Elasticated::Helpers

Defined in:
lib/elasticated/helpers.rb

Class Method Summary collapse

Class Method Details

.hash_deep_dup(hash) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/elasticated/helpers.rb', line 13

def self.hash_deep_dup(hash)
  duplicate = hash.dup
  duplicate.each_pair do |k, v|
    if v.is_a? Hash
      duplicate[k] = hash_deep_dup v
    elsif v.is_a? Array
      duplicate[k] = v.clone
    else
      duplicate[k] = v
    end
  end
  duplicate
end

.hash_sum(*hashes) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elasticated/helpers.rb', line 31

def self.hash_sum(*hashes)
  hashes = hashes.compact
  return 0 if hashes.empty?
  hashes.map(&:keys).reduce(:|).each_with_object({}) do |key, result|
    result[key] =
      if hashes.any? { |h| h[key].is_a?(Hash) }
        new_args = hashes.map { |h| h[key] }
        hash_sum(*new_args)
      else
        hashes.map { |h| h.fetch(key, 0) }.reduce(:+)
      end
  end
end

.string_to_agg_name(element) ⇒ Object



4
5
6
# File 'lib/elasticated/helpers.rb', line 4

def self.string_to_agg_name(element)
  element.to_s.gsub /(?![a-zA-Z0-9])./, '_'
end

.string_to_camel_case(string) ⇒ Object



8
9
10
11
# File 'lib/elasticated/helpers.rb', line 8

def self.string_to_camel_case(string)
  return string if string !~ /_/ && string =~ /[A-Z]+.*/
  string.split('_').map(&:capitalize).join
end

.unscoped_class_name(klass) ⇒ Object



27
28
29
# File 'lib/elasticated/helpers.rb', line 27

def self.unscoped_class_name(klass)
  klass.name.gsub /^.*\:\:/, ''
end