Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement === in enumerable targets.

[ fix #3044 ]
  • Loading branch information...
commit 89048097b790b2f47180d8712411ab2a36e06aed 1 parent f26b41f
@durran durran authored
View
2  CHANGELOG.md
@@ -7,6 +7,8 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#3044 Ensure enumerable targets match arrays in case statements.
+
* \#3034 `first_or_create` on criterion now properly passes the block to create
instead of calling after the document was created.
View
17 lib/mongoid/relations/targets/enumerable.rb
@@ -16,7 +16,7 @@ class Enumerable
# @attribute [rw] _unloaded A criteria representing persisted docs.
attr_accessor :_added, :_loaded, :_unloaded
- delegate :===, :is_a?, :kind_of?, to: []
+ delegate :is_a?, :kind_of?, to: []
# Check if the enumerable is equal to the other object.
#
@@ -33,6 +33,21 @@ def ==(other)
entries == other.entries
end
+ # Check equality of the enumerable against the provided object for case
+ # statements.
+ #
+ # @example Check case equality.
+ # enumerable === Array
+ #
+ # @param [ Object ] other The object to check.
+ #
+ # @return [ true, false ] If the objects are equal in a case.
+ #
+ # @since 3.1.4
+ def ===(other)
+ other.class == Class ? Array == other : self == other
+ end
+
# Append a document to the enumerable.
#
# @example Append the document.
View
46 spec/mongoid/relations/targets/enumerable_spec.rb
@@ -109,6 +109,52 @@
end
end
+ describe "#===" do
+
+ let(:enumerable) do
+ described_class.new([])
+ end
+
+ context "when compared to an array class" do
+
+ it "returns true" do
+ expect(enumerable === Array).to be_true
+ end
+ end
+
+ context "when compared to a different class" do
+
+ it "returns false" do
+ expect(enumerable === Mongoid::Document).to be_false
+ end
+ end
+
+ context "when compared to an array instance" do
+
+ context "when the entries are equal" do
+
+ let(:other) do
+ described_class.new([])
+ end
+
+ it "returns true" do
+ expect(enumerable === other).to be_true
+ end
+ end
+
+ context "when the entries are not equal" do
+
+ let(:other) do
+ described_class.new([ Band.new ])
+ end
+
+ it "returns false" do
+ expect(enumerable === other).to be_false
+ end
+ end
+ end
+ end
+
describe "#<<" do
let(:person) do
Please sign in to comment.
Something went wrong with that request. Please try again.