Fix Searchable when collection contains nil.

This commit is contained in:
Markus Reiter 2018-07-22 21:50:54 +02:00
parent c5f92bfb8b
commit 8f462ddb5d
2 changed files with 23 additions and 5 deletions

View File

@ -17,7 +17,8 @@ module Searchable
def search_regex(regex)
select do |*args|
args = yield(*args) if block_given?
[*args].any? { |arg| arg.match?(regex) }
args = [*args].compact
args.any? { |arg| arg.match?(regex) }
end
end
@ -25,7 +26,8 @@ module Searchable
simplified_string = simplify_string(string)
select do |*args|
args = yield(*args) if block_given?
[*args].any? { |arg| simplify_string(arg).include?(simplified_string) }
args = [*args].compact
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
end
end
end

View File

@ -1,13 +1,13 @@
require "searchable"
describe Searchable do
subject { ary.extend(described_class) }
subject { collection.extend(described_class) }
let(:ary) { ["with-dashes"] }
let(:collection) { ["with-dashes"] }
describe "#search" do
context "when given a block" do
let(:ary) { [["with-dashes", "withdashes"]] }
let(:collection) { [["with-dashes", "withdashes"]] }
it "searches by the selected argument" do
expect(subject.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
@ -26,5 +26,21 @@ describe Searchable do
expect(subject.search("with dashes")).to eq ["with-dashes"]
end
end
context "when searching a Hash" do
let(:collection) { { "foo" => "bar" } }
it "returns a Hash" do
expect(subject.search("foo")).to eq "foo" => "bar"
end
context "containing nil" do
let(:collection) { { "foo" => nil } }
it "does not raise an error" do
expect(subject.search("foo")).to eq "foo" => nil
end
end
end
end
end