Ripple & Dynamic Default Values

Mat Ellis mat at tecnh.com
Thu Feb 10 12:28:27 EST 2011


Yes, I got NameError: undefined method `foo' for class `Class' when I tried that.

M.

On Feb 10, 2011, at 6:48 AM, Sean Cribbs wrote:

> Mat,
> 
> Did you try method(:foo) ? Method objects respond to :call.
> 
> Sean Cribbs <sean at basho.com>
> Developer Advocate
> Basho Technologies, Inc.
> http://basho.com/
> 
> On Feb 10, 2011, at 8:29 AM, Mat Ellis wrote:
> 
>> For anyone else trying to do this, I couldn't find any docs on how to do dynamic default values.
>> 
>> Objective: add an auto-generated UUID field to a Click model using Twitter's simple_uuid gem.
>> 
>> Class Click
>> include Ripple::Document
>> include SimpleUUID
>> 
>> property :uuid, String, :default => foo
>> key on :uuid
>> 
>> def foo
>>   UUID.new.to_guid
>> end
>> end
>> 
>> This just gave me "foo" every time:
>> 
>> Changing default to UUID.new.to_guid gave me a nice uuid value but the same one each time.
>> 
>> I had to make the default property lambda { UUID.new.to_guid } for it to work.
>> 
>> Many other gems/plugins have a convention that if your value in these cases is a symbol it treats it as a local function. This would mean changing the default function in the Ripple gem to something like (untested and untried!):
>> 
>> def default
>> default = options[:default]
>> 
>> return nil if default.nil?
>> if default.is_a? :symbol
>>   type_cast(self.respond_to?(:default) ? self.default : nil)
>> else
>>   type_cast(default.respond_to?(:call) ? default.call : default)
>> end
>> end
>> 
>> M.
>> 
>> 
>> 
>> _______________________________________________
>> riak-users mailing list
>> riak-users at lists.basho.com
>> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
> 





More information about the riak-users mailing list