Hello
I''m trying to DRY my code and I think It could be done even better. The
following 2 functions are "before_save" callbacks that practically do
the
same thing, but on a different set of attributes.
Since the code is highly similar, I was wondering if someone could help me
to refactor the code?
def normalize_budget
result = normalize_generic(self.budget)
self.budget_min = result[:min]
self.budget_max = result[:max]
self.budget = result[:display]
end
def normalize_size
result = normalize_generic(self.size)
self.size_min = result[:min]
self.size_max = result[:max]
self.size = result[:display]
end
Kind regards,
Axinte
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Aug-27 13:47 UTC
Re: How to refactor the following 2 function (4 lines each)
def normalize(property)
result = normalize_generic(self.send(property))
self.send("#{property}_min=", result[:min])
self.send("#{property}_max=", result[:max])
self.send("#{property}=", result[:display])
end
object.normalize(:size)
object.normalize(:budget)
-
Maurício Linhares
http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr
On Thu, Aug 27, 2009 at 10:40 AM, Axinte
Logo<alogo-2zXVbNbNaFTHNWWW6QW1Ag@public.gmane.org>
wrote:> Hello
>
> I''m trying to DRY my code and I think It could be done even
better. The
> following 2 functions are "before_save" callbacks that
practically do the
> same thing, but on a different set of attributes.
>
> Since the code is highly similar, I was wondering if someone could help me
> to refactor the code?
>
>
> def normalize_budget
> result = normalize_generic(self.budget)
> self.budget_min = result[:min]
> self.budget_max = result[:max]
> self.budget = result[:display]
> end
>
> def normalize_size
> result = normalize_generic(self.size)
> self.size_min = result[:min]
> self.size_max = result[:max]
> self.size = result[:display]
> end
>
>
> Kind regards,
> Axinte
>
> >
>
Gianluca Tessarolo
2009-Aug-27 14:02 UTC
Re: How to refactor the following 2 function (4 lines each)
Hi,
try this:
def normalize
update_result("budget")
update_result("size")
end
def update_result(field_name)
result = normalize_generic(self.send(field_name))
eval("self.#{field_name}_min = result[:min]")
eval("self.#{field_name}_max = result[:max]")
eval("self.#{field_name} = result[:display]")
end> Hello
>
> I''m trying to DRY my code and I think It could be done even
better.
> The following 2 functions are "before_save" callbacks that
practically
> do the same thing, but on a different set of attributes.
>
> Since the code is highly similar, I was wondering if someone could
> help me to refactor the code?
>
>
> def normalize_budget
> result = normalize_generic(self.budget)
> self.budget_min = result[:min]
> self.budget_max = result[:max]
> self.budget = result[:display]
> end
>
> def normalize_size
> result = normalize_generic(self.size)
> self.size_min = result[:min]
> self.size_max = result[:max]
> self.size = result[:display]
> end
>
>
> Kind regards,
> Axinte
>
> >
Axinte Logo
2009-Aug-27 14:35 UTC
Re: How to refactor the following 2 function (4 lines each)
Thank you both for your feedback. Based on your suggestions, I''ve
finally
decided to implement it in the following way:
%w[size budget].each do |property|
define_method("normalize_#{property}_range") do |*args|
result = normalize_generic_range(self.send(property))
self.send("#{property}_min=", result[:min])
self.send("#{property}_max=", result[:max])
self.send("#{property}=", result[:display])
end
end
this code defines normalize_size_range and normalize_budget_range and
updates both sets of attributes accordingly.
kind regards,
Axinte
2009/8/27 Gianluca Tessarolo
<tessarolo.gianluca-6ZZdBs7hMehZp5Udy/Obhg@public.gmane.org>
>
> Hi,
>
> try this:
>
> def normalize
> update_result("budget")
> update_result("size")
> end
>
> def update_result(field_name)
> result = normalize_generic(self.send(field_name))
> eval("self.#{field_name}_min = result[:min]")
> eval("self.#{field_name}_max = result[:max]")
> eval("self.#{field_name} = result[:display]")
> end
> > Hello
> >
> > I''m trying to DRY my code and I think It could be done even
better.
> > The following 2 functions are "before_save" callbacks that
practically
> > do the same thing, but on a different set of attributes.
> >
> > Since the code is highly similar, I was wondering if someone could
> > help me to refactor the code?
> >
> >
> > def normalize_budget
> > result = normalize_generic(self.budget)
> > self.budget_min = result[:min]
> > self.budget_max = result[:max]
> > self.budget = result[:display]
> > end
> >
> > def normalize_size
> > result = normalize_generic(self.size)
> > self.size_min = result[:min]
> > self.size_max = result[:max]
> > self.size = result[:display]
> > end
> >
> >
> > Kind regards,
> > Axinte
> >
> > >
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Aug-27 14:40 UTC
Re: How to refactor the following 2 function (4 lines each)
Here''s a better solution:
%w[size budget].each do |property|
class_eval %Q!
def normalize_#{property}_range
result = normalize_generic_range( self.#{property} )
self.#{property}_min = result[:min]
self.#{property}_max = result[:max]
self.#{property} = result[:display]
end
!
end
-
Maurício Linhares
http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr
On Thu, Aug 27, 2009 at 11:35 AM, Axinte
Logo<alogo-2zXVbNbNaFTHNWWW6QW1Ag@public.gmane.org>
wrote:> Thank you both for your feedback. Based on your suggestions, I''ve
finally
> decided to implement it in the following way:
>
>
> %w[size budget].each do |property|
> define_method("normalize_#{property}_range") do |*args|
> result = normalize_generic_range(self.send(property))
> self.send("#{property}_min=", result[:min])
> self.send("#{property}_max=", result[:max])
> self.send("#{property}=", result[:display])
> end
> end
>
>
> this code defines normalize_size_range and normalize_budget_range and
> updates both sets of attributes accordingly.
>
> kind regards,
> Axinte
Axinte Logo
2009-Aug-27 20:24 UTC
Re: How to refactor the following 2 function (4 lines each)
Mauricio, Thank you again for your help. Is there any difference between your solution and mine? Kind regards, Axinte 2009/8/27 Maurício Linhares <linhares.mauricio-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > Here''s a better solution: > > - > Maurício Linhares > http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr > > On Thu, Aug 27, 2009 at 11:35 AM, Axinte Logo<alogo-2zXVbNbNaFTHNWWW6QW1Ag@public.gmane.org> wrote: > > Thank you both for your feedback. Based on your suggestions, I''ve finally > > decided to implement it in the following way: > > > > > > %w[size budget].each do |property| > > define_method("normalize_#{property}_range") do |*args| > > result = normalize_generic_range(self.send(property)) > > self.send("#{property}_min=", result[:min]) > > self.send("#{property}_max=", result[:max]) > > self.send("#{property}=", result[:display]) > > end > > end > > > > > > this code defines normalize_size_range and normalize_budget_range and > > updates both sets of attributes accordingly. > > > > kind regards, > > Axinte > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Aug-27 20:27 UTC
Re: How to refactor the following 2 function (4 lines each)
Yes, you''re defining and method and using send at every call, my solution will generate a method with direct calls instead of sending them. - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Thu, Aug 27, 2009 at 5:24 PM, Axinte Logo<alogo-2zXVbNbNaFTHNWWW6QW1Ag@public.gmane.org> wrote:> Mauricio, > Thank you again for your help. Is there any difference between your solution > and mine? > Kind regards, > Axinte