Permalink
Browse files

Move options hash value normalization to multiple files

Each piece is customizable now and definitely getting more clear.
Slowly but surely.
  • Loading branch information...
1 parent dc8d681 commit 8e2548973b0c7f6c1435986c2c53af75db9b0d70 @jnunemaker jnunemaker committed Oct 11, 2012
@@ -0,0 +1,26 @@
+module Plucky
+ module Normalizers
+ class FieldsValue
+
+ # Public: Given a value returns it normalized for Mongo's fields option
+ def call(value)
+ return nil if value.respond_to?(:empty?) && value.empty?
+
+ case value
+ when Array
+ if value.size == 1 && value.first.is_a?(Hash)
+ value.first
+ else
+ value.flatten
+ end
+ when Symbol
+ [value]
+ when String
+ value.split(',').map { |v| v.strip }
+ else
+ value
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+module Plucky
+ module Normalizers
+ class Integer
+ def call(value)
+ if value.nil?
+ nil
+ else
+ value.to_i
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,62 @@
+require 'plucky/normalizers/integer'
+require 'plucky/normalizers/fields_value'
+require 'plucky/normalizers/sort_value'
+
+module Plucky
+ module Normalizers
+ class OptionsHashValue
+ def initialize(args = {})
+ @key_normalizer = args.fetch(:key_normalizer) {
+ raise KeyError, "Missing required argument :key_normalizer"
+ }
+
+ @value_normalizers = {
+ :fields => default_fields_value_normalizer,
+ :sort => default_sort_value_normalizer,
+ :limit => default_limit_value_normalizer,
+ :skip => default_skip_value_normalizer,
+ }
+
+ if (value_normalizers = args[:value_normalizers])
+ @value_normalizers.update(value_normalizers)
+ end
+ end
+
+ # Public: Returns value normalized for Mongo
+ #
+ # key -The name of the key whose value is being normalized
+ # value -The value to normalize
+ #
+ # Returns value normalized for Mongo.
+ def call(key, value)
+ if (value_normalizer = @value_normalizers[key])
+ value_normalizer.call(value)
+ else
+ value
+ end
+ end
+
+ # Private
+ def default_fields_value_normalizer
+ Normalizers::FieldsValue.new
+ end
+
+ # Private
+ def default_sort_value_normalizer
+ Normalizers::SortValue.new({
+ :key_normalizer => @key_normalizer,
+ })
+ end
+
+ # Private
+ def default_limit_value_normalizer
+ Normalizers::Integer.new
+ end
+
+ # Private
+ def default_skip_value_normalizer
+ Normalizers::Integer.new
+ end
+ end
+ end
+end
@@ -0,0 +1,47 @@
+module Plucky
+ module Normalizers
+ class SortValue
+
+ def initialize(args = {})
+ @key_normalizer = args.fetch(:key_normalizer) { raise KeyError, "Missing required argument :key_normalizer" }
+ end
+
+ # Public: Given a value returns it normalized for Mongo's sort option
+ def call(value)
+ case value
+ when Array
+ if value.size == 1 && value[0].is_a?(String)
+ normalized_sort_piece(value[0])
+ else
+ value.compact.map { |v| normalized_sort_piece(v).flatten }
+ end
+ else
+ normalized_sort_piece(value)
+ end
+ end
+
+ # Private
+ def normalized_sort_piece(value)
+ case value
+ when SymbolOperator
+ [normalized_direction(value.field, value.operator)]
+ when String
+ value.split(',').map do |piece|
+ normalized_direction(*piece.split(' '))
+ end
+ when Symbol
+ [normalized_direction(value)]
+ else
+ value
+ end
+ end
+
+ # Private
+ def normalized_direction(field, direction=nil)
+ direction ||= 'ASC'
+ direction = direction.upcase == 'ASC' ? 1 : -1
+ [@key_normalizer.call(field).to_s, direction]
+ end
+ end
+ end
+end
View
@@ -1,6 +1,7 @@
# encoding: UTF-8
require 'plucky/normalizers/options_hash_key'
+require 'plucky/normalizers/options_hash_value'
module Plucky
class OptionsHash
@@ -47,88 +48,34 @@ def merge!(other)
self
end
+ # Private
+ def normalized_key(key)
+ key_normalizer.call(key)
+ end
+
+ # Private
+ def normalized_value(key, value)
+ value_normalizer.call(key, value)
+ end
+
+ # Private
def key_normalizer
- @key_normalizer ||= options.fetch(: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)
- key_normalizer.call(key)
- end
-
- def normalized_value(key, value)
- case key
- when :fields
- normalized_fields(value)
- when :sort
- normalized_sort(value)
- when :limit, :skip
- normalized_integer(value)
- else
- value
- end
- end
-
- def normalized_fields(value)
- return nil if value.respond_to?(:empty?) && value.empty?
- case value
- when Array
- if value.size == 1 && value.first.is_a?(Hash)
- value.first
- else
- value.flatten
- end
- when Symbol
- [value]
- when String
- value.split(',').map { |v| v.strip }
- else
- value
- end
- end
-
- def normalized_sort(value)
- case value
- when Array
- if value.size == 1 && value[0].is_a?(String)
- normalized_sort_piece(value[0])
- else
- value.compact.map { |v| normalized_sort_piece(v).flatten }
- end
- else
- normalized_sort_piece(value)
- end
- end
-
- def normalized_integer(value)
- value.nil? ? nil : value.to_i
- end
-
- def normalized_sort_piece(value)
- case value
- when SymbolOperator
- [normalized_direction(value.field, value.operator)]
- when String
- value.split(',').map do |piece|
- normalized_direction(*piece.split(' '))
- end
- when Symbol
- [normalized_direction(value)]
- else
- value
- end
- end
+ # Private
+ def value_normalizer
+ @value_normalizer ||= @options.fetch(:value_normalizer) {
+ Normalizers::OptionsHashValue.new({
+ :key_normalizer => key_normalizer,
+ })
+ }
+ end
- def normalized_direction(field, direction=nil)
- direction ||= 'ASC'
- direction = direction.upcase == 'ASC' ? 1 : -1
- [normalized_key(field).to_s, direction]
- end
+ def method_missing(method, *args, &block)
+ @source.send(method, *args, &block)
+ end
end
end
@@ -0,0 +1,45 @@
+require 'helper'
+require 'plucky/normalizers/fields_value'
+
+describe Plucky::Normalizers::FieldsValue do
+ it "defaults to nil" do
+ subject.call(nil).should be_nil
+ end
+
+ it "returns nil if empty string" do
+ subject.call('').should be_nil
+ end
+
+ it "returns nil if empty array" do
+ subject.call([]).should be_nil
+ end
+
+ it "works with array" do
+ subject.call(['one', 'two']).should eq(['one', 'two'])
+ end
+
+ # Ruby 1.9.x was sending array [{:age => 20}], instead of hash.
+ it "works with array that has one hash" do
+ subject.call([{:age => 20}]).should eq({:age => 20})
+ end
+
+ it "flattens multi-dimensional array" do
+ subject.call([[:one, :two]]).should eq([:one, :two])
+ end
+
+ it "works with symbol" do
+ subject.call(:one).should eq([:one])
+ end
+
+ it "works with array of symbols" do
+ subject.call([:one, :two]).should eq([:one, :two])
+ end
+
+ it "works with hash" do
+ subject.call({:one => 1, :two => -1}).should eq({:one => 1, :two => -1})
+ end
+
+ it "converts comma separated list to array" do
+ subject.call('one, two').should eq(['one', 'two'])
+ end
+end
@@ -0,0 +1,24 @@
+require 'helper'
+require 'plucky/normalizers/integer'
+
+describe Plucky::Normalizers::Integer do
+ context "with nil" do
+ it "returns nil" do
+ subject.call(nil).should be_nil
+ end
+ end
+
+ context "with an integer" do
+ it "returns an integer" do
+ subject.call(1).should be(1)
+ subject.call(3232).should be(3232)
+ end
+ end
+
+ context "with a string" do
+ it "returns a string" do
+ subject.call('1').should be(1)
+ subject.call('3232').should be(3232)
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 8e25489

Please sign in to comment.