Sha256: b7fc0b84e3c717aaab407b8246e168fa47ef19012e94616b67eb5eb53fe4b312

Contents?: true

Size: 1.28 KB

Versions: 20

Compression:

Stored size: 1.28 KB

Contents

module Volt
  module Query
    # Normalizes queries so queries that are the same have the same order and parts
    class Normalizer
      def self.normalize(query)
        query = merge_finds_and_move_to_front(query)

        query = reject_skip_zero(query)

        query
      end

      def self.merge_finds_and_move_to_front(query)
        # Map first parts to string
        query = query.map {|v| v[0] = v[0].to_s ; v }
        has_find = query.find {|v| v[0] == 'find' }

        if has_find
          # merge any finds
          merged_find_query = {}
          query = query.reject do |query_part|
            if query_part[0] == 'find'
              # on a find, merge into finds
              find_query = query_part[1]
              merged_find_query.merge!(find_query) if find_query

              # reject
              true
            else
              false
            end
          end

          # Add finds to the front
          query.insert(0, ['find', merged_find_query])
        else
          # No find was done, add it in the first position
          query.insert(0, ['find'])
        end

        query
      end

      def self.reject_skip_zero(query)
        query.reject do |query_part|
          query_part[0] == 'skip' && query_part[1] == 0
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
volt-0.9.1 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.1.pre5 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.1.pre4 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.1.pre3 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.1.pre2 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.1.pre1 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre7 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre6 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre5 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre4 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre3 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre2 lib/volt/models/persistors/query/normalizer.rb
volt-0.9.0.pre1 lib/volt/models/persistors/query/normalizer.rb
volt-0.8.27.beta9 lib/volt/models/persistors/query/normalizer.rb
volt-0.8.27.beta8 lib/volt/models/persistors/query/normalizer.rb
volt-0.8.27.beta7 lib/volt/models/persistors/query/normalizer.rb
volt-0.8.27.beta6 lib/volt/models/persistors/query/normalizer.rb
volt-0.8.27.beta5 lib/volt/models/persistors/query/normalizer.rb
volt-0.8.27.beta4 lib/volt/models/persistors/query/normalizer.rb