From e7286176dec19ba3a4c191e8c300feb5f5015fa9 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 20 Nov 2023 07:54:59 -0800 Subject: [PATCH] Make any_not_empty? extension support passing pattern argument to any? Passing an argument is supported since Ruby 2.5. --- CHANGELOG | 2 ++ lib/sequel/extensions/any_not_empty.rb | 4 ++-- spec/extensions/any_not_empty_spec.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index dfa16a703c..82eddfa9b8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ === master +* Make any_not_empty? extension support passing pattern argument to any? (jeremyevans) (#2100) + * Respect :skip_transaction option in PostgreSQL Dataset#paged_each (jeremyevans) (#2097) * Add TimestampMigrator.run_single to run a single migration file up or down (opya, jeremyevans) (#2093) diff --git a/lib/sequel/extensions/any_not_empty.rb b/lib/sequel/extensions/any_not_empty.rb index 87e24051b6..0eab81ba52 100644 --- a/lib/sequel/extensions/any_not_empty.rb +++ b/lib/sequel/extensions/any_not_empty.rb @@ -32,8 +32,8 @@ module Sequel module AnyNotEmpty # If a block is not given, return whether the dataset is not empty. - def any? - if defined?(yield) + def any?(*a) + if !a.empty? || defined?(yield) super else !empty? diff --git a/spec/extensions/any_not_empty_spec.rb b/spec/extensions/any_not_empty_spec.rb index 4dd1d9974b..7ac9e9b690 100644 --- a/spec/extensions/any_not_empty_spec.rb +++ b/spec/extensions/any_not_empty_spec.rb @@ -20,4 +20,13 @@ @ds.with_fetch([]).any?{|x| x[:one] == 1}.must_equal false @ds.db.sqls.must_equal ["SELECT * FROM t"] end + + it "should use default behavior if argument is given" do + @ds.with_fetch(:one=>1).any?(Hash).must_equal true + @ds.db.sqls.must_equal ["SELECT * FROM t"] + @ds.with_fetch(:one=>1).any?(Array).must_equal false + @ds.db.sqls.must_equal ["SELECT * FROM t"] + @ds.with_fetch([]).any?(Hash).must_equal false + @ds.db.sqls.must_equal ["SELECT * FROM t"] + end if RUBY_VERSION >= '2.5' end