Sha256: 9f7fe5faac454f10a93dfd7afdc39f094f622a1620ba4a144195718ae23292a2

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

module Mongoid
  module Report

    QueriesBuilder = Struct.new(:settings) do
      def do
        [].tap do |queries|
          queries.concat([{ '$project' => project_query }])
          queries.concat([{ '$group'   => group_query }])
          queries.concat([{ '$project' => project_group_fields_query }])
        end
      end

      private

      def groups
        @group_by ||= settings.fetch(:group_by, [])
      end

      def fields
        @fields ||= settings[:fields].select do |field, _|
          !settings[:columns].include?(field.to_sym)
        end
      end

      def in_fields
        @in_fields ||= fields.keys
      end

      def output_fields
        @output_fields ||= fields.values
      end

      def all_fields
        [:_id]
          .concat(in_fields)
          .concat(groups)
      end

      # Example: { '$project' => { :field1 => 1 } }
      def project_query
        all_fields.inject({}) do |hash, field|
          hash.merge!(field => 1)
        end
      end

      GROUP_TEMPLATE = "$%s"
      def group_query
        {}.tap do |query|
          query[:_id] = {}

          groups.inject(query[:_id]) do |hash, group|
            hash.merge!(group => GROUP_TEMPLATE % group)
          end

          in_fields.inject(query) do |hash, field|
            hash.merge!(field => { '$sum' => GROUP_TEMPLATE % field })
          end
        end
      end

      PROJECT_TEMPLATE = "$_id.%s"
      def project_group_fields_query
        {}.tap do |query|
          if groups == [:_id]
            query[:_id] = '$_id'
          else
            query[:_id] = 0

            groups.inject(query) do |hash, group|
              hash.merge!(group => PROJECT_TEMPLATE % group)
            end
          end

          fields.inject(query) do |hash, (field, name)|
            hash.merge!(name => "$#{field}")
          end
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-report-0.1.9 lib/mongoid/report/queries_builder.rb
mongoid-report-0.1.7 lib/mongoid/report/queries_builder.rb