Skip to content

Commit a31dfb1

Browse files
committed
Rename KeysDetector to KeyFieldsDetector
1 parent d6e42b0 commit a31dfb1

File tree

3 files changed

+77
-77
lines changed

3 files changed

+77
-77
lines changed

lib/dynamoid/criteria/chain.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22

3-
require_relative 'keys_detector'
3+
require_relative 'key_fields_detector'
44
require_relative 'nonexistent_fields_detector'
55

66
module Dynamoid #:nodoc:
77
module Criteria
88
# The criteria chain is equivalent to an ActiveRecord relation (and realistically I should change the name from
99
# chain to relation). It is a chainable object that builds up a query and eventually executes it by a Query or Scan.
1010
class Chain
11-
attr_reader :query, :source, :consistent_read, :keys_detector
11+
attr_reader :query, :source, :consistent_read, :key_fields_detector
1212

1313
include Enumerable
1414
# Create a new criteria chain.
@@ -27,7 +27,7 @@ def initialize(source)
2727
end
2828

2929
# we should re-initialize keys detector every time we change query
30-
@keys_detector = KeysDetector.new(@query, @source)
30+
@key_fields_detector = KeyFieldsDetector.new(@query, @source)
3131
end
3232

3333
# The workhorse method of the criteria chain. Each key in the passed in hash will become another criteria that the
@@ -57,7 +57,7 @@ def where(args)
5757
end
5858

5959
# we should re-initialize keys detector every time we change query
60-
@keys_detector = KeysDetector.new(@query, @source)
60+
@key_fields_detector = KeyFieldsDetector.new(@query, @source)
6161

6262
self
6363
end
@@ -75,7 +75,7 @@ def all
7575
end
7676

7777
def count
78-
if @keys_detector.key_present?
78+
if @key_fields_detector.key_present?
7979
count_via_query
8080
else
8181
count_via_scan
@@ -96,7 +96,7 @@ def delete_all
9696
ids = []
9797
ranges = []
9898

99-
if @keys_detector.key_present?
99+
if @key_fields_detector.key_present?
100100
Dynamoid.adapter.query(source.table_name, range_query).flat_map{ |i| i }.collect do |hash|
101101
ids << hash[source.hash_key.to_sym]
102102
ranges << hash[source.range_key.to_sym] if source.range_key
@@ -171,7 +171,7 @@ def records
171171
#
172172
# @since 3.1.0
173173
def pages
174-
if @keys_detector.key_present?
174+
if @key_fields_detector.key_present?
175175
pages_via_query
176176
else
177177
issue_scan_warning if Dynamoid::Config.warn_on_scan && query.present?
@@ -279,24 +279,24 @@ def range_query
279279
opts = {}
280280

281281
# Add hash key
282-
opts[:hash_key] = @keys_detector.hash_key
283-
opts[:hash_value] = type_cast_condition_parameter(@keys_detector.hash_key, query[@keys_detector.hash_key])
282+
opts[:hash_key] = @key_fields_detector.hash_key
283+
opts[:hash_value] = type_cast_condition_parameter(@key_fields_detector.hash_key, query[@key_fields_detector.hash_key])
284284

285285
# Add range key
286-
if @keys_detector.range_key
287-
opts[:range_key] = @keys_detector.range_key
288-
if query[@keys_detector.range_key].present?
289-
value = type_cast_condition_parameter(@keys_detector.range_key, query[@keys_detector.range_key])
286+
if @key_fields_detector.range_key
287+
opts[:range_key] = @key_fields_detector.range_key
288+
if query[@key_fields_detector.range_key].present?
289+
value = type_cast_condition_parameter(@key_fields_detector.range_key, query[@key_fields_detector.range_key])
290290
opts.update(range_eq: value)
291291
end
292292

293-
query.keys.select { |k| k.to_s =~ /^#{@keys_detector.range_key}\./ }.each do |key|
293+
query.keys.select { |k| k.to_s =~ /^#{@key_fields_detector.range_key}\./ }.each do |key|
294294
opts.merge!(range_hash(key))
295295
end
296296
end
297297

298-
(query.keys.map(&:to_sym) - [@keys_detector.hash_key.to_sym, @keys_detector.range_key.try(:to_sym)])
299-
.reject { |k, _| k.to_s =~ /^#{@keys_detector.range_key}\./ }
298+
(query.keys.map(&:to_sym) - [@key_fields_detector.hash_key.to_sym, @key_fields_detector.range_key.try(:to_sym)])
299+
.reject { |k, _| k.to_s =~ /^#{@key_fields_detector.range_key}\./ }
300300
.each do |key|
301301
if key.to_s.include?('.')
302302
opts.update(field_hash(key))
@@ -331,8 +331,8 @@ def type_cast_condition_parameter(key, value)
331331
def start_key
332332
return @start if @start.is_a?(Hash)
333333

334-
hash_key = @keys_detector.hash_key || source.hash_key
335-
range_key = @keys_detector.range_key || source.range_key
334+
hash_key = @key_fields_detector.hash_key || source.hash_key
335+
range_key = @key_fields_detector.range_key || source.range_key
336336

337337
key = {}
338338
key[hash_key] = type_cast_condition_parameter(hash_key, @start.send(hash_key))
@@ -351,7 +351,7 @@ def start_key
351351

352352
def query_opts
353353
opts = {}
354-
opts[:index_name] = @keys_detector.index_name if @keys_detector.index_name
354+
opts[:index_name] = @key_fields_detector.index_name if @key_fields_detector.index_name
355355
opts[:select] = 'ALL_ATTRIBUTES'
356356
opts[:record_limit] = @record_limit if @record_limit
357357
opts[:scan_limit] = @scan_limit if @scan_limit

lib/dynamoid/criteria/keys_detector.rb renamed to lib/dynamoid/criteria/key_fields_detector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Dynamoid #:nodoc:
44
module Criteria
5-
class KeysDetector
5+
class KeyFieldsDetector
66
attr_reader :hash_key, :range_key, :index_name
77

88
def initialize(query, source)

spec/dynamoid/criteria/chain_spec.rb

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ def request_params
134134
chain = Dynamoid::Criteria::Chain.new(model)
135135
expect(chain).to receive(:pages_via_query).and_call_original
136136
expect(chain.where(name: 'Bob', age: 10).all).to contain_exactly(customer1)
137-
expect(chain.keys_detector.hash_key).to eq(:name)
138-
expect(chain.keys_detector.range_key).to eq(:age)
139-
expect(chain.keys_detector.index_name).to be_nil
137+
expect(chain.key_fields_detector.hash_key).to eq(:name)
138+
expect(chain.key_fields_detector.range_key).to eq(:age)
139+
expect(chain.key_fields_detector.index_name).to be_nil
140140
end
141141

142142
it 'supports lt' do
@@ -211,9 +211,9 @@ def request_params
211211
chain = Dynamoid::Criteria::Chain.new(model)
212212
expect(chain).to receive(:pages_via_query).and_call_original
213213
expect(chain.where(name: 'a', age: 10).all).to contain_exactly(customer1)
214-
expect(chain.keys_detector.hash_key).to eq(:name)
215-
expect(chain.keys_detector.range_key).to be_nil
216-
expect(chain.keys_detector.index_name).to be_nil
214+
expect(chain.key_fields_detector.hash_key).to eq(:name)
215+
expect(chain.key_fields_detector.range_key).to be_nil
216+
expect(chain.key_fields_detector.index_name).to be_nil
217217
end
218218

219219
it 'supports eq for set' do
@@ -359,9 +359,9 @@ def request_params
359359
chain = Dynamoid::Criteria::Chain.new(model)
360360
expect(chain).to receive(:pages_via_scan).and_call_original
361361
expect(chain.where(age: 10).all).to contain_exactly(customer1)
362-
expect(chain.keys_detector.hash_key).to be_nil
363-
expect(chain.keys_detector.range_key).to be_nil
364-
expect(chain.keys_detector.index_name).to be_nil
362+
expect(chain.key_fields_detector.hash_key).to be_nil
363+
expect(chain.key_fields_detector.range_key).to be_nil
364+
expect(chain.key_fields_detector.index_name).to be_nil
365365
end
366366

367367
it 'supports eq for set' do
@@ -535,41 +535,41 @@ def request_params
535535
chain = Dynamoid::Criteria::Chain.new(model)
536536
expect(chain).to receive(:pages_via_query).and_call_original
537537
expect(chain.where(name: 'Bob', 'range.lt': 3, 'range2.gt': 15).to_a.size).to eq(1)
538-
expect(chain.keys_detector.hash_key).to eq(:name)
539-
expect(chain.keys_detector.range_key).to eq(:range)
540-
expect(chain.keys_detector.index_name).to be_nil
538+
expect(chain.key_fields_detector.hash_key).to eq(:name)
539+
expect(chain.key_fields_detector.range_key).to eq(:range)
540+
expect(chain.key_fields_detector.index_name).to be_nil
541541
end
542542

543543
it 'supports query on local secondary index' do
544544
chain = Dynamoid::Criteria::Chain.new(model)
545545
expect(chain).to receive(:pages_via_query).and_call_original
546546
expect(chain.where(name: 'Bob', 'range2.gt': 15).to_a.size).to eq(2)
547-
expect(chain.keys_detector.hash_key).to eq(:name)
548-
expect(chain.keys_detector.range_key).to eq(:range2)
549-
expect(chain.keys_detector.index_name).to eq(:range2index)
547+
expect(chain.key_fields_detector.hash_key).to eq(:name)
548+
expect(chain.key_fields_detector.range_key).to eq(:range2)
549+
expect(chain.key_fields_detector.index_name).to eq(:range2index)
550550

551551
chain = Dynamoid::Criteria::Chain.new(model)
552552
expect(chain).to receive(:pages_via_query).and_call_original
553553
expect(chain.where(name: 'Bob', 'range3.lt': 200).to_a.size).to eq(1)
554-
expect(chain.keys_detector.hash_key).to eq(:name)
555-
expect(chain.keys_detector.range_key).to eq(:range3)
556-
expect(chain.keys_detector.index_name).to eq(:range3index)
554+
expect(chain.key_fields_detector.hash_key).to eq(:name)
555+
expect(chain.key_fields_detector.range_key).to eq(:range3)
556+
expect(chain.key_fields_detector.index_name).to eq(:range3index)
557557
end
558558

559559
it 'supports query on local secondary index with start' do
560560
chain = Dynamoid::Criteria::Chain.new(model)
561561
expect(chain).to receive(:pages_via_query).and_call_original
562562
expect(chain.where(name: 'Bob', 'range2.gt': 15).to_a.size).to eq(2)
563-
expect(chain.keys_detector.hash_key).to eq(:name)
564-
expect(chain.keys_detector.range_key).to eq(:range2)
565-
expect(chain.keys_detector.index_name).to eq(:range2index)
563+
expect(chain.key_fields_detector.hash_key).to eq(:name)
564+
expect(chain.key_fields_detector.range_key).to eq(:range2)
565+
expect(chain.key_fields_detector.index_name).to eq(:range2index)
566566

567567
chain = Dynamoid::Criteria::Chain.new(model)
568568
expect(chain).to receive(:pages_via_query).and_call_original
569569
expect(chain.where(name: 'Bob', 'range2.gt': 15).start(@customer2).all).to contain_exactly(@customer3)
570-
expect(chain.keys_detector.hash_key).to eq(:name)
571-
expect(chain.keys_detector.range_key).to eq(:range2)
572-
expect(chain.keys_detector.index_name).to eq(:range2index)
570+
expect(chain.key_fields_detector.hash_key).to eq(:name)
571+
expect(chain.key_fields_detector.range_key).to eq(:range2)
572+
expect(chain.key_fields_detector.index_name).to eq(:range2index)
573573
end
574574
end
575575

@@ -592,9 +592,9 @@ def request_params
592592
expect(chain).to receive(:pages_via_scan).and_call_original
593593
expect(chain.where(city: 'San Francisco').to_a.size).to eq(2)
594594
# Does not use GSI since not projecting all attributes
595-
expect(chain.keys_detector.hash_key).to be_nil
596-
expect(chain.keys_detector.range_key).to be_nil
597-
expect(chain.keys_detector.index_name).to be_nil
595+
expect(chain.key_fields_detector.hash_key).to be_nil
596+
expect(chain.key_fields_detector.range_key).to be_nil
597+
expect(chain.key_fields_detector.index_name).to be_nil
598598
end
599599

600600
context 'with full composite key for table' do
@@ -628,57 +628,57 @@ def request_params
628628
chain = Dynamoid::Criteria::Chain.new(model)
629629
expect(chain).to receive(:pages_via_query).and_call_original
630630
expect(chain.where(name: 'Bob').to_a.size).to eq(1)
631-
expect(chain.keys_detector.hash_key).to eq(:name)
632-
expect(chain.keys_detector.range_key).to be_nil
633-
expect(chain.keys_detector.index_name).to be_nil
631+
expect(chain.key_fields_detector.hash_key).to eq(:name)
632+
expect(chain.key_fields_detector.range_key).to be_nil
633+
expect(chain.key_fields_detector.index_name).to be_nil
634634
end
635635

636636
it 'supports query on global secondary index' do
637637
chain = Dynamoid::Criteria::Chain.new(model)
638638
expect(chain).to receive(:pages_via_query).and_call_original
639639
expect(chain.where(city: 'San Francisco').to_a.size).to eq(3)
640-
expect(chain.keys_detector.hash_key).to eq(:city)
641-
expect(chain.keys_detector.range_key).to eq(:age)
642-
expect(chain.keys_detector.index_name).to eq(:cityage)
640+
expect(chain.key_fields_detector.hash_key).to eq(:city)
641+
expect(chain.key_fields_detector.range_key).to eq(:age)
642+
expect(chain.key_fields_detector.index_name).to eq(:cityage)
643643

644644
chain = Dynamoid::Criteria::Chain.new(model)
645645
expect(chain).to receive(:pages_via_query).and_call_original
646646
expect(chain.where(city: 'San Francisco', 'age.gt': 12).to_a.size).to eq(2)
647-
expect(chain.keys_detector.hash_key).to eq(:city)
648-
expect(chain.keys_detector.range_key).to eq(:age)
649-
expect(chain.keys_detector.index_name).to eq(:cityage)
647+
expect(chain.key_fields_detector.hash_key).to eq(:city)
648+
expect(chain.key_fields_detector.range_key).to eq(:age)
649+
expect(chain.key_fields_detector.index_name).to eq(:cityage)
650650

651651
chain = Dynamoid::Criteria::Chain.new(model)
652652
expect(chain).to receive(:pages_via_query).and_call_original
653653
expect(chain.where(email: '[email protected]').to_a.size).to eq(1)
654-
expect(chain.keys_detector.hash_key).to eq(:email)
655-
expect(chain.keys_detector.range_key).to eq(:age)
656-
expect(chain.keys_detector.index_name).to eq(:emailage)
654+
expect(chain.key_fields_detector.hash_key).to eq(:email)
655+
expect(chain.key_fields_detector.range_key).to eq(:age)
656+
expect(chain.key_fields_detector.index_name).to eq(:emailage)
657657

658658
chain = Dynamoid::Criteria::Chain.new(model)
659659
expect(chain).to receive(:pages_via_query).and_call_original
660660
expect(chain.where(email: '[email protected]', 'age.gt': 12).to_a.size).to eq(1)
661-
expect(chain.keys_detector.hash_key).to eq(:email)
662-
expect(chain.keys_detector.range_key).to eq(:age)
663-
expect(chain.keys_detector.index_name).to eq(:emailage)
661+
expect(chain.key_fields_detector.hash_key).to eq(:email)
662+
expect(chain.key_fields_detector.range_key).to eq(:age)
663+
expect(chain.key_fields_detector.index_name).to eq(:emailage)
664664
end
665665

666666
it 'supports scan when no global secondary index available' do
667667
chain = Dynamoid::Criteria::Chain.new(model)
668668
expect(chain).to receive(:pages_via_scan).and_call_original
669669
expect(chain.where(gender: 'male').to_a.size).to eq(4)
670-
expect(chain.keys_detector.hash_key).to be_nil
671-
expect(chain.keys_detector.range_key).to be_nil
672-
expect(chain.keys_detector.index_name).to be_nil
670+
expect(chain.key_fields_detector.hash_key).to be_nil
671+
expect(chain.key_fields_detector.range_key).to be_nil
672+
expect(chain.key_fields_detector.index_name).to be_nil
673673
end
674674

675675
it 'supports query on global secondary index with start' do
676676
chain = Dynamoid::Criteria::Chain.new(model)
677677
expect(chain).to receive(:pages_via_query).and_call_original
678678
expect(chain.where(city: 'San Francisco').to_a.size).to eq(3)
679-
expect(chain.keys_detector.hash_key).to eq(:city)
680-
expect(chain.keys_detector.range_key).to eq(:age)
681-
expect(chain.keys_detector.index_name).to eq(:cityage)
679+
expect(chain.key_fields_detector.hash_key).to eq(:city)
680+
expect(chain.key_fields_detector.range_key).to eq(:age)
681+
expect(chain.key_fields_detector.index_name).to eq(:cityage)
682682

683683
# Now query with start at customer2 and we should only see customer3
684684
chain = Dynamoid::Criteria::Chain.new(model)
@@ -690,25 +690,25 @@ def request_params
690690
chain = Dynamoid::Criteria::Chain.new(model)
691691
expect(chain).to receive(:pages_via_scan).and_call_original
692692
expect(chain.where('city.begins_with': 'San').to_a.size).to eq(3)
693-
expect(chain.keys_detector.hash_key).to be_nil
694-
expect(chain.keys_detector.range_key).to be_nil
695-
expect(chain.keys_detector.index_name).to be_nil
693+
expect(chain.key_fields_detector.hash_key).to be_nil
694+
expect(chain.key_fields_detector.range_key).to be_nil
695+
expect(chain.key_fields_detector.index_name).to be_nil
696696
end
697697

698698
it 'prefers global secondary index with range key used in conditions to index w/o such range key' do
699699
chain = Dynamoid::Criteria::Chain.new(model)
700700
expect(chain).to receive(:pages_via_query).and_call_original
701701
expect(chain.where(city: 'San Francisco', 'age.lte': 15).to_a.size).to eq(2)
702-
expect(chain.keys_detector.hash_key).to eq(:city)
703-
expect(chain.keys_detector.range_key).to eq(:age)
704-
expect(chain.keys_detector.index_name).to eq(:cityage)
702+
expect(chain.key_fields_detector.hash_key).to eq(:city)
703+
expect(chain.key_fields_detector.range_key).to eq(:age)
704+
expect(chain.key_fields_detector.index_name).to eq(:cityage)
705705

706706
chain = Dynamoid::Criteria::Chain.new(model)
707707
expect(chain).to receive(:pages_via_query).and_call_original
708708
expect(chain.where(city: 'San Francisco', gender: 'male').to_a.size).to eq(3)
709-
expect(chain.keys_detector.hash_key).to eq(:city)
710-
expect(chain.keys_detector.range_key).to eq(:gender)
711-
expect(chain.keys_detector.index_name).to eq(:citygender)
709+
expect(chain.key_fields_detector.hash_key).to eq(:city)
710+
expect(chain.key_fields_detector.range_key).to eq(:gender)
711+
expect(chain.key_fields_detector.index_name).to eq(:citygender)
712712
end
713713
end
714714

0 commit comments

Comments
 (0)