From b34681ce40d8ea8073ddcbda8c2e1048d5abaaf9 Mon Sep 17 00:00:00 2001 From: Seth Jeffery Date: Fri, 24 Apr 2015 14:37:39 +0100 Subject: [PATCH] add support for index hinting --- lib/plucky/query.rb | 5 +++++ spec/plucky/query_spec.rb | 23 +++++++++++++++++++++++ spec/plucky_spec.rb | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/plucky/query.rb b/lib/plucky/query.rb index c0bb6a8..c7b0859 100755 --- a/lib/plucky/query.rb +++ b/lib/plucky/query.rb @@ -127,6 +127,11 @@ def fields(*args) clone.tap { |query| query.options[:fields] = *args } end + def hint(*fields) + hint_fields = fields.first.is_a?(Hash) ? fields.first : Hash[fields.map{|hint| [hint, 1]}] + clone.tap { |query| query.options[:hint] = hint_fields } + end + def ignore(*args) set_field_inclusion(args, 0) end diff --git a/spec/plucky/query_spec.rb b/spec/plucky/query_spec.rb index 1a2b258..92d29dc 100755 --- a/spec/plucky/query_spec.rb +++ b/spec/plucky/query_spec.rb @@ -404,6 +404,29 @@ end end + context "#hint" do + before { @query = described_class.new(@collection) } + subject { @query } + + it "works" do + subject.hint(:name, :age). + options[:hint]. + should == { :name => 1, :age => 1 } + end + + it "returns new instance of query" do + new_query = subject.hint(:name) + new_query.should_not equal(subject) + subject[:hint].should be_nil + end + + it "works with hash" do + subject.hint(:name => 1, :_id => 1). + options[:hint]. + should == { :name => 1, :_id => 1 } + end + end + context "#ignore" do before { @query = described_class.new(@collection) } subject { @query } diff --git a/spec/plucky_spec.rb b/spec/plucky_spec.rb index 08e780f..438836a 100644 --- a/spec/plucky_spec.rb +++ b/spec/plucky_spec.rb @@ -61,7 +61,7 @@ :count, :size, :distinct, :last, :first, :all, :to_a, :exists?, :exist?, :empty?, - :remove, + :remove, :hint ].sort_by(&:to_s) end end