Permalink
Browse files

keep option also deletes attribute - that allows nested filtering of …

…kept data
  • Loading branch information...
1 parent 538a5ac commit d6e2967011a21be95625b66031cf5da87240ab18 @mkristian committed Feb 19, 2013
Showing with 11 additions and 4 deletions.
  1. +10 −3 lib/ixtlan/babel/params_filter.rb
  2. +1 −1 spec/params_filter_spec.rb
@@ -59,19 +59,26 @@ def use(context_or_options)
@context_or_options = context_or_options
self
end
+
def filter_it( data )
filter.options = self.class.config.single_options( @context_or_options )
+ data = data.dup
data = data[ filter.options[ :root ] ] if filter.options[ :root ]
keeps = {}
( filter.options[ :keep ] || [] ).each do |k|
- keeps[ k.to_s ] = data[ k.to_s ] || data[ k.to_sym ]
+ keep = data[ k.to_s ] || data[ k.to_sym ]
+ keeps[ k.to_s ] = data.delete( k.to_s ) || data.delete( k.to_sym ) unless keep.is_a? Hash
+ end
+ filtered_data = filter.filter( data )
+ ( filter.options[ :keep ] || [] ).each do |k|
+ keeps[ k.to_s ] = filtered_data.delete( k.to_s ) || filtered_data.delete( k.to_sym ) unless keeps.member?( k.to_s )
end
- [ filter.filter( data ), keeps ]
+ [ filtered_data, keeps ]
end
def new( data )
@model_class.new( filter( data ) )
end
end
end
-end
+end
@@ -41,7 +41,7 @@ def method_missing(method)
it 'should filter a hash with keep' do
json = filter.use( :keep => ['id'] ).filter_it( data )
- json[0].must_equal Hash[ 'id' => data['id'], 'name' => data['name'] ]
+ json[0].must_equal Hash[ 'name' => data['name'] ]
json[1].must_equal Hash[ 'id' => data['id'] ]
end

0 comments on commit d6e2967

Please sign in to comment.