I am using the following idiom to validate form data that is not 
stored in a table:
    class FormData < ActiveRecord::BaseWithoutTable
      column :date_column, :date
      column :int_column, :integer
      validates_date :date_column
      validates_numericality_of :int_column
    end
    unless request.get?
      @report = FormData.new(params[:report])
      @report.save
    else
      @report = FormData.new
    end
This works great (BaseWithoutTable and ValidatesDate are non-standard 
packages), but it contains lots of boilerplate code that I''d like to 
factor out. I would like to do something like this instead:
    form_validate do
      column :date_column, :date
      column :int_column, :integer
      validates_date :date_column
      validates_numericality_of :int_column
    end
but I''m unsure how I could do this. Something like:
  def form_validate(init = {}, &block)
    instance_eval <<-CODE
      class Eval < ActiveRecord::BaseWithoutTable
        #{block}
      end
      unless request.get?
        @report = Eval.new(params[:report])
        @report.save
      else
        @report = Eval.new(#{init})
      end
    CODE
  end
doesn''t work. Do I need to use method_missing to parse the lines from 
the block or is there a better way?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jan-29  12:40 UTC
Re: DRYing code through meta programming
Hi -- On Mon, 29 Jan 2007, svenax wrote:> > I am using the following idiom to validate form data that is not > stored in a table: > > class FormData < ActiveRecord::BaseWithoutTable > column :date_column, :date > column :int_column, :integer > validates_date :date_column > validates_numericality_of :int_column > end > > unless request.get? > @report = FormData.new(params[:report]) > @report.save > else > @report = FormData.new > end > > This works great (BaseWithoutTable and ValidatesDate are non-standard > packages), but it contains lots of boilerplate code that I''d like to > factor out. I would like to do something like this instead: > > form_validate do > column :date_column, :date > column :int_column, :integer > validates_date :date_column > validates_numericality_of :int_column > endI probably just haven''t had enough coffee, but I''m not seeing how this differs significantly from what you''ve got above, in terms of amount of code. Or is the savings inside the two add-on packages? David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try some more coffee ;)
Well, it would eliminate
    unless request.get?
      @report = FormData.new(params[:report])
      @report.save
    else
      @report = FormData.new
    end
from each time I use this idiom.
Maybe I really want to ask the general question: How do I treat a code 
block like a string to be used in some XXX_eval function? I can''t do 
block.to_s can I? No, that would be too simple ...
On Jan 29, 1:40 pm, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
wrote:> Hi --
>
>
>
> On Mon, 29 Jan 2007, svenax wrote:
>
> > I am using the following idiom to validate form data that is not
> > stored in a table:
>
> >    class FormData < ActiveRecord::BaseWithoutTable
> >      column :date_column, :date
> >      column :int_column, :integer
> >      validates_date :date_column
> >      validates_numericality_of :int_column
> >    end
>
> >    unless request.get?
> >      @report = FormData.new(params[:report])
> >      @report.save
> >    else
> >      @report = FormData.new
> >    end
>
> > This works great (BaseWithoutTable and ValidatesDate are non-standard
> > packages), but it contains lots of boilerplate code that I''d
like to
> > factor out. I would like to do something like this instead:
>
> >    form_validate do
> >      column :date_column, :date
> >      column :int_column, :integer
> >      validates_date :date_column
> >      validates_numericality_of :int_column
> >    endI probably just haven''t had enough coffee, but
I''m not seeing how this
> differs significantly from what you''ve got above, in terms of
amount
> of code.  Or is the savings inside the two add-on packages?
>
> David
>
> --
> Q. What is THE Ruby book for Rails developers?
> A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
>     (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
> A. Ruby Power and Light, LLC (http://www.rubypal.com)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
svenax wrote:> Maybe I really want to ask the general question: How do I treat a code > block like a string to be used in some XXX_eval function? I can''t do > block.to_s can I?I thought most evals could take blocks as arguments, or as do-end blocks. Do you mean you need to do string surgery to the block first? I would try the opposite of "meta" programming, first. :-) -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jan 29, 6:20 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> svenax wrote: > > Maybe I really want to ask the general question: How do I treat a code > > block like a string to be used in some XXX_eval function? I can''t do > > block.to_s can I?I thought most evals could take blocks as arguments, or as do-end > blocks. Do you mean you need to do string surgery to the block first? > I would try the opposite of "meta" programming, first. :-)That is exactly what I mean. Opposite? You mean hyperprogramming? /sven axelsson --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
svenax wrote:> That is exactly what I mean. Opposite? You mean hyperprogramming?No, uh, like... programmging. if statements. +. You know... -- Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Jan 30, 12:00 am, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> svenax wrote: > > That is exactly what I mean. Opposite? You mean hyperprogramming? > No, uh, like... programmging. if statements. +. You know...Yeah, so? I''ve alrady done that, so I thought it was time to try something new. So the question still stands: Is it possible to stringify a code block in order to perform some string surgery on it before handing it off to an eval function? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
svenax wrote:> So the question still stands: Is it possible to > stringify a code block in order to perform > some string surgery on it before handing it off to > an eval function?Absolutely. What have you tried? What problem(s) did you have? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---