Sha256: 04e3390a2f67fcd04a2b3813836a7e44dc335bc76e23339a0a7691cef5570b90

Contents?: true

Size: 1.36 KB

Versions: 13

Compression:

Stored size: 1.36 KB

Contents

module Measures
  # keys can't contain periods https://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 MongoHashKeyWrapper

    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

    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

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bonnie_bundler-3.0.0 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.2.5 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.2.4 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.2.3 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.2.1 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.2.0 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.1.2 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.1.1 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.1.0 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.0.3 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.0.2 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.0.1 lib/measures/mongo_hash_key_wrapper.rb
bonnie_bundler-2.0.0 lib/measures/mongo_hash_key_wrapper.rb