Class: Measures::MongoHashKeyWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/measures/mongo_hash_key_wrapper.rb

Overview

keys can’t contain periods docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names technically they can’t start with dollar sign either, but that’s prohibited for CQL naming so:

periods are converted to '^p'
and carets are converted to '^c' therefore we aren't invalidating the use of any characters (e.g. caret)

Class Method Summary collapse

Class Method Details

.unwrapKeys(theHash) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/measures/mongo_hash_key_wrapper.rb', line 25

def self.unwrapKeys(theHash)
  newKeys = Hash.new
  theHash.keys.each do |key|
    if (key.include? '^p') || (key.include? '^c')
      newKey = key.gsub(/\^p/, '.')
      newKey.gsub!(/\^c/, '^')
      newKeys[key] = newKey
    end
  end
  newKeys.each { |old, new| theHash[new] = theHash.delete old}
  # now recurse on any contained hashes
  theHash.each do |key,value|
    if value.respond_to?(:key)
      unwrapKeys(value)
    end
  end
end

.wrapKeys(theHash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/measures/mongo_hash_key_wrapper.rb', line 9

def self.wrapKeys(theHash)
  newKeys = Hash.new
  theHash.keys.each do |key|
    if (key.include? '.') || (key.include? '^')
      newKeys[key] = key.gsub(/[\^\.]/, '^' => '^c', '.' => '^p')
    end
  end
  newKeys.each { |old, new| theHash[new] = theHash.delete old}
  # now recurse on any contained hashes
  theHash.each do |key,value|
    if value.respond_to?(:key)
      wrapKeys(value)
    end
  end
end