Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move options hash key normalization to separate file.

  • Loading branch information...
commit dc8d681679b871a64117b8c2c5e90ca5176d0292 1 parent 3a35c38
@jnunemaker jnunemaker authored
View
17 lib/plucky/normalizers/options_hash_key.rb
@@ -0,0 +1,17 @@
+module Plucky
+ module Normalizers
+ class OptionsHashKey
+
+ NormalizedKeys = {
+ :order => :sort,
+ :select => :fields,
+ :offset => :skip,
+ :id => :_id,
+ }
+
+ def call(key)
+ NormalizedKeys.fetch key.to_sym, key
+ end
+ end
+ end
+end
View
23 lib/plucky/options_hash.rb
@@ -1,18 +1,15 @@
# encoding: UTF-8
+
+require 'plucky/normalizers/options_hash_key'
+
module Plucky
class OptionsHash
- NormalizedKeys = {
- :order => :sort,
- :select => :fields,
- :offset => :skip,
- :id => :_id,
- }
-
- attr_reader :source
+ attr_reader :source, :options
- def initialize(hash={})
+ def initialize(hash={}, options={})
@source = {}
+ @options = options
hash.each { |key, value| self[key] = value }
end
@@ -50,13 +47,19 @@ def merge!(other)
self
end
+ def key_normalizer
+ @key_normalizer ||= options.fetch(:key_normalizer) {
+ Normalizers::OptionsHashKey.new
+ }
+ end
+
private
def method_missing(method, *args, &block)
@source.send(method, *args, &block)
end
def normalized_key(key)
- NormalizedKeys.fetch key.to_sym, key
+ key_normalizer.call(key)
end
def normalized_value(key, value)
View
23 spec/plucky/normalizers/options_hash_key_spec.rb
@@ -0,0 +1,23 @@
+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
View
17 spec/plucky/options_hash_spec.rb
@@ -36,23 +36,6 @@
end
describe "#[]=" do
- it "converts order to sort" do
- options = described_class.new(:order => :foo)
- options[:order].should be_nil
- options[:sort].should == [['foo', 1]]
- end
-
- it "converts select to fields" do
- options = described_class.new(:select => 'foo')
- options[:select].should be_nil
- options[:fields].should == ['foo']
- end
-
- it "converts offset to skip" do
- options = described_class.new(:offset => 1)
- options[:offset].should be_nil
- options[:skip].should == 1
- end
context ":fields" do
before { @options = described_class.new }
Please sign in to comment.
Something went wrong with that request. Please try again.