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?
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 ...
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.
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?
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?