GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Resource-oriented open source Ruby framework for Web apps.
Homepage: http://rubywaves.com/
Clone URL: git://github.com/dyoder/waves.git
[#19] request param destructuring fixed for more than two levels
automatthew (author)
Tue Jun 24 07:09:38 -0700 2008
commit  b42d4009e6c86bc87fed1e05d7486676ad87a679
tree    d2ceb8696e0b955ae31522248fe896a92d71d29f
parent  a7c132b34b526c2e01c994a5e06a80d337f5dd06
...
131
132
133
134
135
136
137
 
 
 
 
138
139
 
140
141
142
 
143
144
145
146
147
 
 
 
 
 
 
 
 
 
148
149
150
151
 
 
 
152
153
154
...
131
132
133
 
 
 
 
134
135
136
137
138
 
139
140
141
 
142
143
 
 
 
 
144
145
146
147
148
149
150
151
152
153
154
 
 
155
156
157
158
159
160
0
@@ -131,24 +131,30 @@ module Waves
0
 
0
       private
0
 
0
- def destructure(hash)
0
- rval = {}
0
- hash.keys.map{ |key|key.split('.') }.each do |keys|
0
- destructure_with_array_keys(hash,'',keys,rval)
0
+ def destructure( hash )
0
+ destructured = {}
0
+ hash.keys.map { |key| key.split('.') }.each do |keys|
0
+ destructure_with_array_keys(hash, '', keys, destructured)
0
         end
0
- rval
0
+ destructured
0
       end
0
 
0
- def destructure_with_array_keys(hash,prefix,keys,rval)
0
+ def destructure_with_array_keys( hash, prefix, keys, destructured )
0
         if keys.length == 1
0
- val = hash[prefix+keys.first]
0
- rval[keys.first.intern] = case val
0
- when String then val.strip
0
- when Hash then val
0
+ key = "#{prefix}#{keys.first}"
0
+ val = hash[key]
0
+ destructured[keys.first.intern] = case val
0
+ when String
0
+ val.strip
0
+ when Hash
0
+ val
0
+ when nil
0
+ raise key.inspect
0
           end
0
         else
0
- rval = ( rval[keys.first.intern] ||= {} )
0
- destructure_with_array_keys(hash,(keys.shift<<'.'),keys,rval)
0
+ destructured = ( destructured[keys.first.intern] ||= {} )
0
+ new_prefix = "#{prefix}#{keys.shift}."
0
+ destructure_with_array_keys( hash, new_prefix, keys, destructured )
0
         end
0
       end
0
 
...
16
17
18
19
 
 
20
21
22
 
 
 
23
24
25
...
16
17
18
 
19
20
21
22
 
23
24
25
26
27
28
0
@@ -16,10 +16,13 @@ describe "A Waves controller" do
0
   it "can access a destructured version of the request params" do
0
     request_hash = { 'beer.name' => 'Pale Rye Ale',
0
                           'beer.brew.kind' => 'mini-mash',
0
- 'beer.brew.yeast' => 'White Labs British Ale' }
0
+ 'beer.brew.yeast' => 'White Labs British Ale' ,
0
+ 'beer.brew.hops.variety' => 'Fuggles' }
0
     destructured_hash = { :beer =>
0
                             { :name => 'Pale Rye Ale',
0
- :brew => { :kind => 'mini-mash', :yeast => 'White Labs British Ale'}}}
0
+ :brew => { :kind => 'mini-mash',
0
+ :yeast => 'White Labs British Ale',
0
+ :hops => { :variety => 'Fuggles' }}}}
0
                               
0
     @request.should.receive(:params).and_return(request_hash)
0
     @dc.params.should == destructured_hash

Comments

    No one has commented yet.