Skip to content

Commit 4ff430a

Browse files
committed
Add efficient method for none?, add spec
1 parent acf5237 commit 4ff430a

2 files changed

Lines changed: 58 additions & 46 deletions

File tree

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/collection.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,21 @@ def empty?
619619
end
620620

621621
def any?(*args, &block)
622-
# If are doing anything other than just checking if there is an object in the collection,
623-
# proceed to the normal behavior
624-
return super if args&.length&.positive? || block.present?
622+
# If there are any args passed in, then the collection is being used in the condition
623+
# and we must load it all into memory.
624+
return all.any?(*args, &block) if args&.length&.positive? || block.present?
625625

626-
# Otherwise just check the count for efficiency
627-
count.positive?
626+
# Otherwise we can just check the count for efficiency
627+
!empty?
628+
end
629+
630+
def none?(*args, &block)
631+
# If there are any args passed in, then the collection is being used in the condition
632+
# and we must load it all into memory.
633+
return all.none?(*args, &block) if args&.length&.positive? || block.present?
634+
635+
# Otherwise we can just check the count for efficiency
636+
empty?
628637
end
629638

630639
def method_missing(method, *args, &block)

ruby/hyper-model/spec/batch2/collection_aggregate_methods_spec.rb

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,58 +32,61 @@
3232
size_window(:small, :portrait)
3333
end
3434

35-
[:count, :empty?, :any?].each do |method|
35+
%i[count empty? any? none?].each do |method|
3636
it "will not retrieve the entire collection when using #{method}" do
3737
FactoryBot.create(:test_model)
3838

39-
expect_promise(<<-RUBY
40-
Hyperstack::Model
41-
.load { TestModel.#{method} }
42-
.then do |val|
43-
if TestModel.all.instance_variable_get('@collection')
44-
"unnecessary fetch of all"
45-
else
46-
val
39+
expect_promise(
40+
<<~RUBY
41+
Hyperstack::Model
42+
.load { TestModel.#{method} }
43+
.then do |val|
44+
if TestModel.all.instance_variable_get('@collection')
45+
"unnecessary fetch of all"
46+
else
47+
val
48+
end
4749
end
48-
end
49-
RUBY
50-
).to eq(TestModel.all.send(method))
50+
RUBY
51+
).to eq(TestModel.all.send(method))
5152
end
5253
end
5354

54-
it 'will retrieve the entire collection when using any? if an arg is passed in' do
55-
FactoryBot.create(:test_model)
55+
%i[any? none?].each do |method|
56+
it 'will retrieve the entire collection when using any? if an arg is passed in' do
57+
FactoryBot.create(:test_model)
5658

57-
expect_promise(
58-
<<~RUBY
59-
Hyperstack::Model.load do
60-
TestModel.any?(TestModel)
61-
end.then do |val|
62-
if TestModel.all.instance_variable_get('@collection')
63-
'necessary fetch of all'
64-
else
65-
val
59+
expect_promise(
60+
<<~RUBY
61+
Hyperstack::Model.load do
62+
TestModel.#{method}(TestModel)
63+
end.then do |val|
64+
if TestModel.all.instance_variable_get('@collection')
65+
'necessary fetch of all'
66+
else
67+
val
68+
end
6669
end
67-
end
68-
RUBY
69-
).to eq('necessary fetch of all')
70-
end
70+
RUBY
71+
).to eq('necessary fetch of all')
72+
end
7173

72-
it 'will retrieve the entire collection when using any? if a block is passed in' do
73-
FactoryBot.create(:test_model)
74+
it 'will retrieve the entire collection when using any? if a block is passed in' do
75+
FactoryBot.create(:test_model)
7476

75-
expect_promise(
76-
<<~RUBY
77-
Hyperstack::Model.load do
78-
TestModel.any? { |test_model| test_model }
79-
end.then do |val|
80-
if TestModel.all.instance_variable_get('@collection')
81-
'necessary fetch of all'
82-
else
83-
val
77+
expect_promise(
78+
<<~RUBY
79+
Hyperstack::Model.load do
80+
TestModel.#{method} { |test_model| test_model }
81+
end.then do |val|
82+
if TestModel.all.instance_variable_get('@collection')
83+
'necessary fetch of all'
84+
else
85+
val
86+
end
8487
end
85-
end
86-
RUBY
87-
).to eq('necessary fetch of all')
88+
RUBY
89+
).to eq('necessary fetch of all')
90+
end
8891
end
8992
end

0 commit comments

Comments
 (0)