[Facets] deeply "filter" an Hash

21 views
Skip to first unread message

Guido De Rosa

unread,
Aug 5, 2011, 5:59:09 AM8/5/11
to facets-u...@rubyforge.org
Hi!

Given some nested data in form of a Hash:

h = {
:a => 1,
:b => {
:x => 10,
:y => 20
},
:c=>3
}

I would like to "authorize" only certain part of such data to pass in,
in a granular way.

authorize = {
:a => true,
:b => {
:x => true
}
}

I would like to get this result:

filtered = {
:a => 1,
:b => {
:x => 10
}
}

There's a way to get this in facets? Thanks!

Guido
_______________________________________________
facets-universal mailing list
facets-u...@rubyforge.org
http://rubyforge.org/mailman/listinfo/facets-universal

Trans

unread,
Aug 11, 2011, 9:37:25 PM8/11/11
to facets-u...@rubyforge.org

On Aug 5, 5:59 am, Guido De Rosa <guidoder...@gmail.com> wrote:
> Hi!
>
> Given some nested data in form of a Hash:
>
>     h = {
>       :a => 1,
>       :b => {
>         :x => 10,
>         :y => 20
>       },
>       :c=>3
>     }
>
> I would like to "authorize" only certain part of such data to pass in,
> in a granular way.
>
>     authorize = {
>       :a => true,
>       :b => {
>         :x => true
>       }
>     }
>
> I would like to get this result:
>
>     filtered = {
>       :a => 1,
>       :b => {
>         :x => 10
>       }
>     }
>
> There's a way  to get this in facets? Thanks!

The first layer is easy enough:

h & [:a, :b]

You can do the second with:

h[:b] & [:x]

But I get the feeling you want something more general. As far as I can
recall at the moment, there is no such filtering method. Maybe you
would like to create one?

Reply all
Reply to author
Forward
0 new messages