Permalink
Browse files

Array of fields as argument to only and without

Allow to pass array of fields as argument to only and without methods

Now you can call this methods in two ways:

  query.only(:first, :second)
  query.without(:first, :second)

  query.only([:first, :second])
  query.without([:first, :second])
  • Loading branch information...
1 parent fd6ede1 commit 11c5c483dde005bc001e23d55c426a912e94ac5e @flexoid flexoid committed with durran Nov 10, 2012
Showing with 62 additions and 20 deletions.
  1. +2 −0 lib/origin/optional.rb
  2. +60 −20 spec/origin/optional_spec.rb
View
@@ -124,6 +124,7 @@ def no_timeout
#
# @since 1.0.0
def only(*args)
+ args = args.flatten
option(*args) do |options|
options.store(
:fields, args.inject({}){ |sub, field| sub.tap { sub[field] = 1 }}
@@ -232,6 +233,7 @@ def snapshot
#
# @since 1.0.0
def without(*args)
+ args = args.flatten
option(*args) do |options|
options.store(
:fields, args.inject({}){ |sub, field| sub.tap { sub[field] = 0 }}
@@ -773,20 +773,40 @@
end
end
- context "when provided arguments" do
+ context "when provided fields" do
- let(:selection) do
- query.only(:first, :second)
- end
+ context "as several arguments" do
- it "adds the field options" do
- selection.options.should eq(
- { fields: { "first" => 1, "second" => 1 }}
- )
+ let(:selection) do
+ query.only(:first, :second)
+ end
+
+ it "adds the field options" do
+ selection.options.should eq(
+ { fields: { "first" => 1, "second" => 1 }}
+ )
+ end
+
+ it "returns a cloned query" do
+ selection.should_not equal(query)
+ end
end
- it "returns a cloned query" do
- selection.should_not equal(query)
+ context "as one argument - array" do
+
+ let(:selection) do
+ query.only([:first, :second])
+ end
+
+ it "adds the field options" do
+ selection.options.should eq(
+ { fields: { "first" => 1, "second" => 1 }}
+ )
+ end
+
+ it "returns a cloned query" do
+ selection.should_not equal(query)
+ end
end
end
end
@@ -1665,20 +1685,40 @@
end
end
- context "when provided arguments" do
+ context "when provided fields" do
- let(:selection) do
- query.without(:first, :second)
- end
+ context "as sevaral arguments" do
- it "adds the field options" do
- selection.options.should eq(
- { fields: { "first" => 0, "second" => 0 }}
- )
+ let(:selection) do
+ query.without(:first, :second)
+ end
+
+ it "adds the field options" do
+ selection.options.should eq(
+ { fields: { "first" => 0, "second" => 0 }}
+ )
+ end
+
+ it "returns a cloned query" do
+ selection.should_not equal(query)
+ end
end
- it "returns a cloned query" do
- selection.should_not equal(query)
+ context "as one argument - array" do
+
+ let(:selection) do
+ query.without([:first, :second])
+ end
+
+ it "adds the field options" do
+ selection.options.should eq(
+ { fields: { "first" => 0, "second" => 0 }}
+ )
+ end
+
+ it "returns a cloned query" do
+ selection.should_not equal(query)
+ end
end
end
end

0 comments on commit 11c5c48

Please sign in to comment.