Ripple & Dynamic Default Values

Mat Ellis mat at tecnh.com
Thu Feb 10 08:29:08 EST 2011


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.






More information about the riak-users mailing list