Skip to content

Commit

Permalink
Add pluck method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Apr 4, 2020
1 parent ebd557c commit 8d6bda8
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/dynamoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Criteria
extend ActiveSupport::Concern

module ClassMethods
%i[where all first last each record_limit scan_limit batch start scan_index_forward find_by_pages project].each do |meth|
%i[where all first last each record_limit scan_limit batch start scan_index_forward find_by_pages project pluck].each do |meth|
# Return a criteria chain in response to a method that will begin or end a chain. For more information,
# see Dynamoid::Criteria::Chain.
#
Expand Down
43 changes: 34 additions & 9 deletions lib/dynamoid/criteria/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ def project(*fields)
self
end

def pluck(*args)
fields = args.map(&:to_sym)
@project = fields

if fields.many?
items.map do |item|
fields.map { |key| Undumping.undump_field(item[key], source.attributes[key]) }
end.to_a
else
key = fields.first
items.map { |item| Undumping.undump_field(item[key], source.attributes[key]) }.to_a
end
end

private

# The actual records referenced by the association.
Expand All @@ -172,7 +186,12 @@ def project(*fields)
#
# @since 0.2.0
def records
pages.lazy.flat_map { |i| i }
pages.lazy.flat_map { |items, _| items }
end

# Raw items like they are stored before type casting
def items
raw_pages.lazy.flat_map { |items, _| items }
end

# Arrays of records, sized based on the actual pages produced by DynamoDB
Expand All @@ -181,11 +200,19 @@ def records
#
# @since 3.1.0
def pages
raw_pages.lazy.map do |items, options|
models = items.map { |i| source.from_database(i) }
[models, options]
end.each
end

# Pages of items before type casting
def raw_pages
if @key_fields_detector.key_present?
pages_via_query
raw_pages_via_query
else
issue_scan_warning if Dynamoid::Config.warn_on_scan && query.present?
pages_via_scan
raw_pages_via_scan
end
end

Expand All @@ -194,13 +221,12 @@ def pages
# @return [Enumerator] an iterator of the found pages. An array of records
#
# @since 3.1.0
def pages_via_query
def raw_pages_via_query
Enumerator.new do |y|
Dynamoid.adapter.query(source.table_name, range_query).each do |items, metadata|
page = items.map { |h| source.from_database(h) }
options = metadata.slice(:last_evaluated_key)

y.yield page, options
y.yield items, options
end
end
end
Expand All @@ -210,13 +236,12 @@ def pages_via_query
# @return [Enumerator] an iterator of the found pages. An array of records
#
# @since 3.1.0
def pages_via_scan
def raw_pages_via_scan
Enumerator.new do |y|
Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).each do |items, metadata|
page = items.map { |h| source.from_database(h) }
options = metadata.slice(:last_evaluated_key)

y.yield page, options
y.yield items, options
end
end
end
Expand Down
Loading

0 comments on commit 8d6bda8

Please sign in to comment.