Sha256: 63d2a7024103c5fe7d7e461b4e6c1190884595e0fbdc18e819fc7c05fb44cb8c
Contents?: true
Size: 1.77 KB
Versions: 2
Compression:
Stored size: 1.77 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] 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.6 | lib/mongoid/report/queries_builder.rb |
mongoid-report-0.1.5 | lib/mongoid/report/queries_builder.rb |