Sha256: 0f14767031ba1534b15cdc1518a537e8c652c910e55d2025109e0f6e70d31f8c

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true
module Mongoid
  module Document::ClassMethods
    def pluck_array(*fields)
      where(nil).pluck_array(*fields)
    end

    def pluck_all(*fields)
      where(nil).pluck_all(*fields)
    end
  end

  module Findable
    delegate :pluck_all, :pluck_array, to: :with_default_scope
  end

  module Contextual
    delegate :pluck_all, :pluck_array, to: :context

    class None
      def pluck_array(*)
        []
      end

      def pluck_all(*)
        []
      end
    end

    class Mongo
      def pluck_array(*fields)
        normalized_select = get_normalized_select(fields)
        get_query_data(normalized_select).reduce([]) do |plucked, doc|
          values = normalized_select.keys.map(&plucked_value_mapper(:array, doc))
          plucked << (values.size == 1 ? values.first : values)
        end
      end

      def pluck_all(*fields)
        normalized_select = get_normalized_select(fields)
        get_query_data(normalized_select).reduce([]) do |plucked, doc|
          values = normalized_select.keys.map(&plucked_value_mapper(:all, doc))
          plucked << values.to_h
        end
      end

      private

      def plucked_value_mapper(type, doc)
        proc do |n|
          values = [n, n =~ /\./ ? doc[n.partition('.')[0]] : doc[n]]
          case type
          when :array then values[1]
          when :all   then values
          end
        end
      end

      def get_query_data(normalized_select)
        return (@view ? @view.projection(normalized_select) : query.dup.select(normalized_select))
      end

      def get_normalized_select(fields)
        fields.each_with_object({}) do |f, hash|
          hash[klass.database_field_name(f)] = 1
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pluck_all-2.3.0 lib/pluck_all/models/mongoid_extension.rb
pluck_all-2.2.1 lib/pluck_all/models/mongoid_extension.rb
pluck_all-2.1.0 lib/pluck_all/models/mongoid_extension.rb
pluck_all-2.0.4 lib/pluck_all/models/mongoid_extension.rb