Permalink
Browse files

Add key converter extension.

  • Loading branch information...
1 parent f7b538f commit a0b648969021418d6f8bbae0962a834e8924ff16 Michael Bleigh committed Jul 29, 2011
Showing with 123 additions and 2 deletions.
  1. +1 −1 Gemfile.lock
  2. +4 −1 lib/hashie.rb
  3. +52 −0 lib/hashie/extensions/key_conversion.rb
  4. +66 −0 spec/hashie/extensions/key_conversion_spec.rb
View
2 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- hashie (1.1.0)
+ hashie (2.0.0.beta)
GEM
remote: http://rubygems.org/
View
5 lib/hashie.rb
@@ -8,6 +8,9 @@ module Hashie
autoload :Trash, 'hashie/trash'
module Extensions
- autoload :Coercion, 'hashie/extensions/coercion'
+ autoload :Coercion, 'hashie/extensions/coercion'
+ autoload :KeyConversion, 'hashie/extensions/key_conversion'
+ autoload :StringifyKeys, 'hashie/extensions/key_conversion'
+ autoload :SymbolizeKeys, 'hashie/extensions/key_conversion'
end
end
View
52 lib/hashie/extensions/key_conversion.rb
@@ -0,0 +1,52 @@
+module Hashie
+ module Extensions
+ module StringifyKeys
+ # Convert all keys in the hash to strings.
+ #
+ # @example
+ # test = {:abc => 'def'}
+ # test.stringify_keys!
+ # test # => {'abc' => 'def'}
+ def stringify_keys!
+ keys.each do |k|
+ self[k.to_s] = self.delete(k)
+ end
+ self
+ end
+
+ # Return a new hash with all keys converted
+ # to strings.
+ def stringify_keys
+ dup.stringify_keys!
+ end
+ end
+
+ module SymbolizeKeys
+ # Convert all keys in the hash to strings.
+ #
+ # @example
+ # test = {'abc' => 'def'}
+ # test.symbolize_keys!
+ # test # => {:abc => 'def'}
+ def symbolize_keys!
+ keys.each do |k|
+ self[k.to_sym] = self.delete(k)
+ end
+ self
+ end
+
+ # Return a new hash with all keys converted
+ # to symbols.
+ def symbolize_keys
+ dup.symbolize_keys!
+ end
+ end
+
+ module KeyConversion
+ def self.included(base)
+ base.send :include, SymbolizeKeys
+ base.send :include, StringifyKeys
+ end
+ end
+ end
+end
View
66 spec/hashie/extensions/key_conversion_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+describe Hashie::Extensions::KeyConversion do
+ subject do
+ klass = Class.new(Hash)
+ klass.send :include, Hashie::Extensions::KeyConversion
+ klass
+ end
+ let(:instance){ subject.new }
+
+ describe '#stringify_keys!' do
+ it 'should convert keys to strings' do
+ instance[:abc] = 'abc'
+ instance[123] = '123'
+ instance.stringify_keys!
+ (instance.keys & %w(abc 123)).size.should == 2
+ end
+
+ it 'should return itself' do
+ instance.stringify_keys!.should == instance
+ end
+ end
+
+ describe '#stringify_keys' do
+ it 'should convert keys to strings' do
+ instance[:abc] = 'def'
+ copy = instance.stringify_keys
+ copy['abc'].should == 'def'
+ end
+
+ it 'should not alter the original' do
+ instance[:abc] = 'def'
+ copy = instance.stringify_keys
+ instance.keys.should == [:abc]
+ copy.keys.should == %w(abc)
+ end
+ end
+
+ describe '#symbolize_keys!' do
+ it 'should convert keys to symbols' do
+ instance['abc'] = 'abc'
+ instance['def'] = 'def'
+ instance.symbolize_keys!
+ (instance.keys & [:abc, :def]).size.should == 2
+ end
+
+ it 'should return itself' do
+ instance.symbolize_keys!.should == instance
+ end
+ end
+
+ describe '#stringify_keys' do
+ it 'should convert keys to strings' do
+ instance['abc'] = 'def'
+ copy = instance.symbolize_keys
+ copy[:abc].should == 'def'
+ end
+
+ it 'should not alter the original' do
+ instance['abc'] = 'def'
+ copy = instance.symbolize_keys
+ instance.keys.should == ['abc']
+ copy.keys.should == [:abc]
+ end
+ end
+end

0 comments on commit a0b6489

Please sign in to comment.