On Jul 24, 2007, at 2:50 PM, Michael Koziarski wrote:

>
> Sorry for replying to myself, but the wireless here is a little too
> spotty to reply to all the individual threads.
>
> Providing a method to allow static defaults such as:
>
> set_default_values :foo=>'bar'
>
> Is just reimplementing the pre-existing defaults code that we extract
> from the columns, so I'm not sure that's the right way to go.
>
> For dynamic defaults, such as 'the default value for the categories on
> a blog, is taken from the account's default values' I've not seen a
> case that can't be catered for by overriding the initialize method.
>
> Requiring users to call super first is perfectly acceptable to me.  To
> expect to use methods from a super class without first initializing
> that super class seems very strange to me.
>
> Of course, there may well be particular cases where it's genuinely
> confusing that something doesn't work, or that the error messages are
> misleading,  but we can fix those on a case by case basis.
>
> Every feature that we add, becomes something we need to support on an
> ongoing basis, for the foreseeable future.  So we have to have a
> reject by default, or we'll end up with another 'components'.

okay, this isn't a patch but it's what i'm using internally and a  
suggestion for an impl:


class ActiveRecord::Base
   alias_method '__initialize__', 'initialize'

   def initialize options = nil, &block
     returning( __initialize__(options, &block) ) do
       options ||= {}
       options.to_options!
       defaults = self.class.defaults || self.defaults || Hash.new
       (defaults.keys - options.keys).each do |key|
         value = defaults[key]
         case value
           when Proc
             value = instance_eval &value
           when Symbol
             value = send value
         end
         send "#{ key }=", value
       end
     end
   end

   def self.defaults *argv
     @defaults = argv.shift.to_hash if argv.first
     return @defaults if defined? @defaults
   end

   def defaults *argv
     @defaults = argv.shift.to_hash if argv.first
     return @defaults if defined? @defaults
   end
end


while this might seem overly complicated at first it makes all this  
behave correctly

class C < ActiveRecord::Base
   defaults :foo => 42
end
p C.new.foo            #=> 42
p C.new('foo' => 42.0) #=> 42.0


class C < ActiveRecord::Base
   defaults :foo => :bar

   def bar() 42 end
end
p C.new.foo           #=> 42


class C < ActiveRecord::Base
   defaults :foo => lambda{ 42 }
end
p C.new.foo           #=> 42

class C < ActiveRecord::Base
   defaults :foo => lambda{ bar }
   def bar() 42 end
end
p C.new.foo           #=> 42


also note that the defaults are set *after* the 'normal'  
initialization had been executed - allowed things like

class C
   default :foobar => lambda{ foo + bar }
end


kind regards.


a @ http://drawohara.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to