Changeset 1184
- Timestamp:
- 01/06/08 15:06:18 (11 months ago)
- Files:
-
- trunk/lib/merb/core_ext/hash.rb (modified) (4 diffs)
- trunk/spec/merb/core_ext_spec.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/merb/core_ext/hash.rb
r1133 r1184 75 75 hash 76 76 end 77 78 # convert this hash to a query string param79 # {:name => "Bob", :address => {:street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222']}}77 78 # convert this hash to a query string param 79 # {:name => "Bob", :address => {:street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222']}}.to_params 80 80 # #=> "name=Bob&address[city]=Ruby Central&address[phones]=111-111-1111222-222-2222&address[street]=111 Ruby Ave." 81 81 def to_params … … 100 100 101 101 # lets through the keys in the argument 102 # ${:one => 1, :two => 2, :three => 3}.pass(:one)102 # {:one => 1, :two => 2, :three => 3}.pass(:one) 103 103 # #=> {:one=>1} 104 def pass(*allowed)104 def only(*allowed) 105 105 self.reject { |k,v| ! allowed.include?(k) } 106 106 end 107 alias only pass 108 107 109 108 # blocks the keys in the arguments 110 # ${:one => 1, :two => 2, :three => 3}.block(:one)109 # {:one => 1, :two => 2, :three => 3}.block(:one) 111 110 # #=> {:two=>2, :three=>3} 112 def block(*rejected)111 def except(*rejected) 113 112 self.reject { |k,v| rejected.include?(k) } 114 113 end 115 alias except block116 114 117 115 # Converts the hash into xml attributes … … 120 118 def to_xml_attributes 121 119 map do |k,v| 122 "#{k.to_s.camelize.sub(/^(.{1,1})/){|m| m.downcase}}=\"#{v}\""120 %{#{k.to_s.camelize.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"} 123 121 end.join(" ") 124 122 end … … 142 140 end 143 141 end 144 142 145 143 # Destructively convert all keys to symbols recursively. 144 # { 'one' => 1, 'two' => 2 }.symbolize_keys! 145 # #=> { :one => 1, :two => 2 } 146 146 def symbolize_keys! 147 keys.each do |key| 148 unless key.is_a?(Symbol) 149 self[key.to_sym] = self[key] 150 delete(key) 151 end 152 if Hash === (sub = self[key.to_sym]) 153 sub.symbolize_keys! 154 end 147 each do |k,v| 148 sym = k.respond_to?(:to_sym) ? k.to_sym : k 149 self[sym] = Hash === v ? v.symbolize_keys! : v 150 delete(k) unless k == sym 155 151 end 156 152 self 157 153 end 158 154 159 155 # Destructively convert each key to an uppercase string non-recursively. 160 156 # {:name => "Bob", "age" => 12, "nick" => "Bobinator"}.environmentize_keys! trunk/spec/merb/core_ext_spec.rb
r1133 r1184 202 202 { :test_1 => { :test2 => 'test'}}.environmentize_keys!.should == 203 203 { 'TEST_1' => { :test2 => 'test'}} 204 end 205 end 206 207 describe Hash, "only" do 208 209 before do 210 @hash = { :one => 'ONE', 'two' => 'TWO', 3 => 'THREE' } 211 end 212 213 it "should return a hash with only the given key(s)" do 214 @hash.only(:one).should == { :one => 'ONE' } 215 @hash.only(:one, 3).should == { :one => 'ONE', 3 => 'THREE' } 216 end 217 end 218 219 describe Hash, "except" do |variable| 220 221 before do 222 @hash = { :one => 'ONE', 'two' => 'TWO', 3 => 'THREE' } 223 end 224 225 it "should return a hash without only the given key(s)" do 226 @hash.except(:one).should == { 'two' => 'TWO', 3 => 'THREE' } 227 @hash.except(:one, 3).should == { 'two' => 'TWO' } 228 end 229 end 230 231 describe Hash, "symbolize_keys!" do 232 233 before do 234 @hash = { 'one' => 1, 'two' => 2 } 235 @hash_with_another_hash = { 'a' => 'A', 'prefs' => { 'private' => true, 'sex' => 'yes please' } } 236 @hash_with_non_string_keys = { 'one' => 1, 2 => 'TWO', '' => 'blank' } 237 end 238 239 it "should convert all keys to symbols" do 240 @hash.symbolize_keys! 241 @hash.should == { :one => 1, :two => 2 } 242 end 243 244 it "should recursively convert all keys to symbols" do 245 @hash_with_another_hash.symbolize_keys! 246 @hash_with_another_hash.should == { :a => 'A', :prefs => { :private => true, :sex => 'yes please' } } 204 247 end 205 248 end … … 379 422 } 380 423 end 381 382 383 424 end 425 426 describe Hash, 'to_params' do 427 428 before do 429 @hash = { :name => 'Bob', :address => { :street => '111 Ruby Ave.', :city => 'Ruby Central', :phones => ['111-111-1111', '222-222-2222'] } } 430 end 431 432 it "should convert correctly into query parameters" do 433 @hash.to_params.split('&').sort.should == 'name=Bob&address[city]=Ruby Central&address[phones]=111-111-1111222-222-2222&address[street]=111 Ruby Ave.'.split('&').sort 434 end 384 435 end 385 436
