On May 10, 2008, at 10:21 AM, Sean O'Halpin wrote:
> Always a pleasure reading your code. One question: why are you
> aliasing instance_eval?
>
> Regards,
> Sean
>



hmmm.  well this:


class Specd
  alias_method '__eval__', 'instance_eval'

  instance_methods.each{|m| undef_method m unless m[%r/__/]}

  def Specd.build config
    c = Class.new
    new(c).__eval__(config)
    Object.send :const_set, c.title, c
    Object.send :const_get, c.title
  end

...


basically says

  - keep a handle on instance_eval

  - blow away all public methods

  - build Specd objects by creating a class (which has only a few  
instance methods like 'attribute' and 'constraint') and evaluating the  
config definition in there.

so i needed the alias to be able to do the instance eval.


the point of blowing away all the methods in spec'd is so i can  
intercept any DSL-ish methods and apply them to the class i'm  
building.  using this sort of DSL wrapper allows me to build up a  
class with a dsl without littering the class itself with useless DSL  
crap.  for instance

   class Model

      has_many :foos

    end

relies on Model having a has_many method.  this is sometimes not  
desirable as it may require, for instance, inheriting from some  
abstract base type.  with the 'dsl as wrapper approach' one can do

   Model = DSL.build do

      has_many :foos

   end

and Model can be a totally 'normal' class - all the special DSL-y  
goodness on how to build up stuff is contained in the DSL object,  
which has a @model instance and all methods stripped except the dsl  
ones.


this code works very similarly

   http://codeforpeople.com/lib/ruby/configuration/configuration-0.0.5/lib/configuration.rb

it's more complex for sure but the usage should make it clear enough

   http://codeforpeople.com/lib/ruby/configuration/configuration-0.0.5/README


cheers.



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