Permalink
Browse files

Merge pull request #36 from cheald/master

Fix issues with OptionsKeyNormalizer acting on sort fields
  • Loading branch information...
2 parents 2615e5a + f224a66 commit 93df88dde5d75a147a81bbd0154bf955c71c8cca @jnunemaker jnunemaker committed Jul 8, 2013
@@ -0,0 +1,19 @@
+module Plucky
+ module Normalizers
+ class HashKey
+
+ def initialize(keys)
+ @keys = keys
+ end
+
+ # Public: Normalizes an options hash key
+ #
+ # key - The key to normalize
+ #
+ # Returns a Symbol.
+ def call(key)
+ @keys.fetch key.to_sym, key
+ end
+ end
+ end
+end
@@ -1,23 +0,0 @@
-module Plucky
- module Normalizers
- class OptionsHashKey
-
- # Internal: Keys with values that they should normalize to
- NormalizedKeys = {
- :order => :sort,
- :select => :fields,
- :offset => :skip,
- :id => :_id,
- }
-
- # Public: Normalizes an options hash key
- #
- # key - The key to normalize
- #
- # Returns a Symbol.
- def call(key)
- NormalizedKeys.fetch key.to_sym, key
- end
- end
- end
-end
@@ -5,9 +5,9 @@
module Plucky
module Normalizers
class OptionsHashValue
-
- # Public: Initialize an OptionsHashValue.
- #
+
+ # Public: Initialize an OptionsHashValue.
+ #
# args - The hash of arguments (default: {})
# :key_normalizer - The key normalizer to use, must respond to call
# :value_normalizers - Hash where key is name of options hash key
@@ -66,9 +66,7 @@ def default_fields_value_normalizer
# Private
def default_sort_value_normalizer
- Normalizers::SortValue.new({
- :key_normalizer => @key_normalizer,
- })
+ Normalizers::SortValue.new(:key_normalizer => Normalizers::HashKey.new({:_id => :id}))
end
# Private
@@ -1,6 +1,6 @@
# encoding: UTF-8
-require 'plucky/normalizers/options_hash_key'
+require 'plucky/normalizers/hash_key'
require 'plucky/normalizers/options_hash_value'
module Plucky
@@ -82,7 +82,12 @@ def normalized_value(key, value)
# Private
def key_normalizer
@key_normalizer ||= @options.fetch(:key_normalizer) {
- Normalizers::OptionsHashKey.new
+ Normalizers::HashKey.new({
+ :order => :sort,
+ :select => :fields,
+ :offset => :skip,
+ :id => :_id,
+ })
}
end
@@ -0,0 +1,36 @@
+require 'helper'
+
+describe Plucky::OptionsHash do
+ subject { described_class.new }
+
+ describe "#[]=" do
+ it "changes order to sort" do
+ subject[:order] = "foo asc"
+ subject[:sort].should == [["foo", 1]]
+ subject[:order].should be_nil
+ end
+
+ it "changes select to fields" do
+ subject[:select] = [:foo]
+ subject[:fields].should == [:foo]
+ subject[:select].should be_nil
+ end
+
+ it "changes offset to skip" do
+ subject[:offset] = 10
+ subject[:skip].should == 10
+ subject[:offset].should be_nil
+ end
+
+ it "changes id to _id" do
+ subject[:id] = :foo
+ subject[:_id].should == :foo
+ subject[:id].should be_nil
+ end
+
+ it "does not change the sort field" do
+ subject[:order] = :order.asc
+ subject[:sort].should == [["order", 1]]
+ end
+ end
+end
@@ -0,0 +1,15 @@
+require 'helper'
+
+describe Plucky::Normalizers::HashKey do
+ subject {
+ described_class.new(:bacon => :sizzle)
+ }
+
+ it "changes defined fields" do
+ subject.call(:bacon).should eq(:sizzle)
+ end
+
+ it "does not change undefined fields" do
+ subject.call(:sausage).should eq(:sausage)
+ end
+end
@@ -1,23 +0,0 @@
-require 'helper'
-
-describe Plucky::Normalizers::OptionsHashKey do
- subject {
- described_class.new
- }
-
- it "changes order to sort" do
- subject.call(:order).should eq(:sort)
- end
-
- it "changes select to fields" do
- subject.call(:select).should eq(:fields)
- end
-
- it "changes offset to skip" do
- subject.call(:offset).should eq(:skip)
- end
-
- it "changes id to _id" do
- subject.call(:id).should eq(:_id)
- end
-end
@@ -3,7 +3,7 @@
describe Plucky::Normalizers::SortValue do
let(:key_normalizer) {
- lambda { |key| key == :id ? :_id : key.to_sym }
+ Plucky::Normalizers::HashKey.new({:id => :_id})
}
subject {
@@ -88,6 +88,10 @@
subject.call([:id.asc]).should eq([['_id', 1]])
end
+ it "doesn't convert keys like :sort to :order via key normalizer" do
+ subject.call(:order.asc).should eq([['order', 1]])
+ end
+
it "converts string with $natural correctly" do
subject.call('$natural desc').should eq([['$natural', -1]])
end

0 comments on commit 93df88d

Please sign in to comment.