lib/core/facets/hash/keyize.rb in facets-2.1.3 vs lib/core/facets/hash/keyize.rb in facets-2.2.0

- old
+ new

@@ -1,38 +1,21 @@ -# TITLE: -# -# Hash Key-ize Extensions -# -# SUMMARY: -# -# Extensions of Hash for converting keys. -# -# CREDIT: -# -# - David H. H. -# - Thomas Sawyer -# -# NOTES: -# -# - These methods are largely usurped by Hash#rekey. -# They remain available for the time being for improved -# intercompability with Rail's ActiveSupport library. - -# class Hash + # NOTE: These methods are largely usurped by Hash#rekey. + # They remain available for the time being for improved + # intercompability with Rail's ActiveSupport library. + # Converts all keys in the Hash accroding to the given block. # If the block return +nil+ for given key, then that key will be # left intact. # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.normalize_keys{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } # foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa } # - #-- - # Adapted from Gavin Sinclair's #convert_keys. - #++ + # CREDIT: Trans + # CREDIT: Gavin Sinclair def normalize_keys( &block ) dup.send(:normalize_keys!, &block) end @@ -40,13 +23,12 @@ # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.normalize_keys!{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa } # foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa } # - #-- - # Adapted from Gavin Sinclair's #convert_keys. - #++ + # CREDIT: Trans + # CREDIT: Gavin Sinclair def normalize_keys!( &block ) keys.each{ |k| nk = block[k] self[nk]=delete(k) if nk @@ -59,13 +41,12 @@ # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.stringify_keys #=> { "name"=>"Gavin", "wife"=>:Lisa } # foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa } # - #-- - # Adapted from Gavin Sinclair's #convert_keys. - #++ + # CREDIT: Trans + # CREDIT: Gavin Sinclair def stringify_keys( &filter ) if filter normalize_keys{ |k| filter[k] ? k.to_s : nil } else @@ -80,13 +61,13 @@ # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.stringify_keys! #=> { "name"=>"Gavin", "wife"=>:Lisa } # foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa } # - #-- - # Adapted from Gavin Sinclair's #convert_keys. - #++ + # CREDIT: Trans + # CREDIT: Gavin Sinclair + def stringify_keys!( &filter ) if filter normalize_keys!{ |k| filter[k] ? k.to_s : nil } else normalize_keys!{ |k| k.to_s } @@ -100,13 +81,12 @@ # # foo = { :name=>'Gavin', 'wife'=>:Lisa } # foo.symbolize_keys #=> { :name=>"Gavin", :wife=>:Lisa } # foo.inspect #=> { "name" =>"Gavin", "wife"=>:Lisa } # - #-- - # Adapted from Gavin Sinclair's graceous work #symbolify_keys. - #++ + # CREDIT: Trans + # CREDIT: Gavin Sinclair def symbolize_keys( &filter ) if filter normalize_keys{ |k| filter[k] ? k.to_sym : nil } else @@ -127,13 +107,12 @@ # # foo = { 'name'=>'Gavin', 'wife'=>:Lisa } # foo.symbolize_keys! #=> { :name=>"Gavin", :wife=>:Lisa } # foo.inspect #=> { :name=>"Gavin", :wife=>:Lisa } # - #-- - # Adapted from Gavin Sinclair's graceous work #symbolify_keys! - #++ + # CREDIT: Trans + # CREDIT: Gavin Sinclair def symbolize_keys!( &filter ) if filter normalize_keys!{ |k| filter[k] ? k.to_sym : nil } else @@ -155,11 +134,15 @@ # It defaults to nil which convert any key class. # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.variablize_keys #=> { "@name"=>"Gavin", "@wife"=>:Lisa } # foo.inspect #=> { :name =>"Gavin", :wife=>:Lisa } + # + # CREDIT: Trans + # CREDIT: David Hansson + def variablize_keys( of_class=nil ) self.dup.variablize_keys!( of_class ) end # Synonym for Hash#keys_to_string, but modifies the receiver in place (and returns it). @@ -167,10 +150,13 @@ # It defaults to nil which convert any key class. # # foo = { :name=>'Gavin', :wife=>:Lisa } # foo.variablize_keys! #=> { "@name"=>"Gavin", "@wife"=>:Lisa } # foo.inspect #=> { "@name"=>"Gavin", "@wife"=>:Lisa } + # + # CREDIT: Trans + # CREDIT: David Hansson def variablize_keys!( of_class=nil ) raise ArgumentError, "Parameter must be a class" unless of_class.kind_of?(Class) if of_class if of_class self.each_key do |k| @@ -191,59 +177,5 @@ end self end end - - - -# _____ _ -# |_ _|__ ___| |_ -# | |/ _ \/ __| __| -# | | __/\__ \ |_ -# |_|\___||___/\__| -# -=begin test - - require 'test/unit' - - class TestHashKeyize < Test::Unit::TestCase - - def test_normalize_keys_01 - foo = { :a=>1, :b=>2 } - assert_equal( { "a"=>1, "b"=>2 }, foo.normalize_keys{|k|k.to_s} ) - assert_equal( { :a =>1, :b=>2 }, foo ) - end - - def test_normalize_keys_02 - foo = { :a=>1, :b=>2 } - assert_equal( { "a"=>1, "b"=>2 }, foo.normalize_keys!{|k|k.to_s} ) - assert_equal( { "a"=>1, "b"=>2 }, foo ) - end - - def test_keys_to_s - foo = { :a=>1, :b=>2 } - assert_equal( { "a"=>1, "b"=>2 }, foo.stringify_keys ) - assert_equal( { :a =>1, :b=>2 }, foo ) - end - - def test_keys_to_s! - foo = { :a=>1, :b=>2 } - assert_equal( { "a"=>1, "b"=>2 }, foo.stringify_keys! ) - assert_equal( { "a"=>1, "b"=>2 }, foo ) - end - - def test_keys_to_sym - foo = { 'a'=>1, 'b'=>2 } - assert_equal( { :a=>1, :b=>2 }, foo.symbolize_keys ) - assert_equal( { "a" =>1, "b"=>2 }, foo ) - end - - def test_keys_to_sym! - foo = { 'a'=>1, 'b'=>2 } - assert_equal( { :a=>1, :b=>2 }, foo.symbolize_keys! ) - assert_equal( { :a=>1, :b=>2 }, foo ) - end - - end - -=end