You're on the right track. It might be best to make a built-in function that
can handle more than one possibility, for example:
var Ripple = {
mapByFields: function(value, keyData, fields){
if(!value.not_found){
var object = Riak.mapValuesJson(value)[0];
for(field in fields) {
if(object[field] != fields[field])
return [];
}
return [object];
}
}
};
Then instead of passing the source in your query, use map("Ripple.mapByFields",
:arg => {"email" => "[email protected]"}, :keep => true). However, this won't
necessarily give you a User object, but instead a list of JSON objects that
you'll have to coerce into your document class.
Sean Cribbs <[email protected]>
Developer Advocate
Basho Technologies, Inc.
http://basho.com/
On Jun 1, 2010, at 5:26 AM, Samuel MORELLO wrote:
> Hi,
>
> Using Ripple, I wonder what is the best way to implement a simple
> find_by_email function.
> Since i don't want to use the email as a key, is there another way
> that using a mapreduce function ?
>
> Many thanks,
>
> Samuel (newbie)
>
>
> require 'ripple'
> require 'digest/d5'
>
> class User
> include Ripple::Document
>
> property :email, String, :presence => true
> property :hash_password, String, :presence => true
>
> def self.find_by_email(email)
> map = "
> function(v) {
> if (v.values) {
> var val = Riak.mapValuesJson(v)[0];
> return (val.email === '#{email}' ) ? v.key : null;
> } else return null;
> }
> "
> Riak::MapReduce.new(bucket.client).add(bucket).map(map,
> :keep => true).run
> end
>
> def password=(pwd)
> self.hash_password = Digest::MD5.hexdigest(pwd)
> end
> def password
> self.hash_password
> end
>
> end
>
> _______________________________________________
> riak-users mailing list
> [email protected]
> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
_______________________________________________
riak-users mailing list
[email protected]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com