Skip to content

Commit

Permalink
initial work on the 'filter'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Jan 30, 2011
1 parent 8ed454c commit afc772c
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/ruote/util/filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#--
# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Made in Japan.
#++

require 'ruote/util/misc'
require 'ruote/util/lookup'


module Ruote

def self.filter (filter, hash)

#hash = Ruote.fulldup(hash)
hash = Rufus::Json.dup(hash)
# since Yajl is faster than Marshal...

filter.each do |rule|

field = rule['field']
value = Ruote.lookup(hash, field)

if rule['remove']

Ruote.unset(hash, rule['field'])

elsif d = rule['default'] and value.nil?

Ruote.set(hash, rule['field'], Rufus::Json.dup(d))
end
end

hash
end
end

80 changes: 80 additions & 0 deletions test/unit/ut_22_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#
# testing ruote
#
# Sun Jan 30 21:08:14 JST 2011
#

require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')

require_json
require 'rufus/json'
require 'ruote/util/filter'


class UtFilterTest < Test::Unit::TestCase

def test_remove

assert_equal(
{},
Ruote.filter(
[
{ 'field' => 'x', 'remove' => true }
],
{ 'x' => 'y' }))

assert_equal(
{ 'x' => {} },
Ruote.filter(
[
{ 'field' => 'x.y', 'remove' => true }
],
{ 'x' => { 'y' => 'z' } }))
end

def test_default

assert_equal(
{ 'x' => 1 },
Ruote.filter(
[
{ 'field' => 'x', 'default' => 1 }
],
{}))

assert_equal(
{ 'x' => 2 },
Ruote.filter(
[
{ 'field' => 'x', 'default' => 1 }
],
{ 'x' => 2 }))

assert_equal(
{ 'x' => { 'y' => 1 } },
Ruote.filter(
[
{ 'field' => 'x.y', 'default' => 1 }
],
{ 'x' => {} }))

assert_equal(
{ 'x' => { 'y' => 2 } },
Ruote.filter(
[
{ 'field' => 'x.y', 'default' => 1 }
],
{ 'x' => { 'y' => 2 } }))

assert_equal(
{ 'x' => { 'y' => 1 } },
Ruote.filter(
[
{ 'field' => 'x', 'default' => {} },
{ 'field' => 'x.y', 'default' => 1 }
],
{}))
end
end

0 comments on commit afc772c

Please sign in to comment.