I do appreciate that your trying to keep the code as dry as possible, but I
personally prefer to keep the property declaration part (and association
part) of my DataMapper class definitions as declarative as possible.
For example:
# ===========
# cleaner
opts = {:auto_validation => false, :size => 50}
property :firstname, String, opts
property :lastname, String, opts
opts = {:auto_validation => false, :precision => 10, :scale => 4}
property :minimum, BigDecimal, opts.merge({:nullable => false})
property :maximum, BigDecimal, opts
# ===========
# more typing at first, but easier to maintain, more declarative, and more
glance-able
# (Adam's preference)
property :firstname, String, :auto_validation => false, :size => 50
property :lastname, String, :auto_validation => false, :size => 50
property :minimum, BigDecimal, :auto_validation => false, :precision => 10,
:scale => 4, :nullable => false
property :maximum, BigDecimal, :auto_validation => false, :precision => 10,
:scale => 4
((if the formating or line-endings got messed up, check out
http://gist.github.com/47524 ))
I personally prefer the latter because it's "stupid-proof" and makes
everything as obvious and accessible as possible. The former may have been
less typing up-front, but you can't really glance-over it and know what's
going on with each property....you have to glance up to the 'opts' variable
to get the list of property options rather than them being inline with the
property.
But really it's all up to the person writing the code and their preferences.
As michael points out, adding the default_options block method you proposed
can be a plugin lets people opt (har) into using it if they want to. When
there's enough demand to move default_options into -core, talk with dkubb
and he'll make it happen.
===
~Adam
On Wed, Jan 14, 2009 at 9:18 AM, heda <[email protected]> wrote:
>
> too messy when you have sets of options
>
> opts = {:auto_validation => false}
> opts_string = opts.merge({:size => 50})
> opts_decimal = opts.merge({:precision => 10, :scale => 4})
>
> property :firstname, String, opts_string
> property :lastname, String, opts_string
>
> property :minimum, BigDecimal, opts_decimal.merge({:nullable =>
> false})
> property :maximum, BigDecimal, opts_decimal
>
> =====
>
> much cleaner and easier to read with something like
>
> with_default_options {:auto_validation => false} do
>
> with_default_options {:size => 50} do
> property :firstname, String
> property :lastname, String
> end
>
> with_default_options {:precision => 10, :scale => 4} do
> property :minimum, BigDecimal, :nullable => false
> property :lastname, BigDecimal
> end
>
> end
>
> On Jan 14, 3:04 pm, "Adam French" <[email protected]> wrote:
> > Avoid needless complexity
> > opts = {:size => (10..100), :scale =>2, :nullable => false}
> > property :maximum, BigDecimal, opts
> > property :minimum, BigDecimal, opts
> >
> > ===
> > ~Adam
> >
> > On Wed, Jan 14, 2009 at 9:01 AM, Michael Klishin <
> >
> > [email protected]> wrote:
> >
> > > On 14.01.2009, at 17:13, heda wrote:
> >
> > > > I'd imagine (though Michael can verify this) that the block should
> > > > look something like this
> >
> > > > with_default_options {:precision => 8, :scale => 2, :nullable =>
> > > > false} do
> > > > property :minimum, BigDecimal
> > > > property :maximum, BigDecimal
> > > > end
> >
> > > > with_default_options {:size => (10..100), :nullable => false} do
> > > > property :firstname, String
> > > > property :lastname, String
> > > > end
> >
> > > Correct
> >
> > > MK
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---