librelist archives

« back to archive

method_missing in my View

method_missing in my View

From:
Fernando Guillen
Date:
2011-02-20 @ 17:42
Hello,

I'm trying to add 'magic' functionality to my Views and I'm trying to
use 'method_missing' to do so.

For example I want to implement this method_missing:

  def method_missing( method_name, *args, &block )
    if method_name.to_s =~ /_to_html$/
      return @my_hash[method_name.gsub('_to_html','')].to_html
    end
    super
  end

(Very simplified example, and written in the air)

In this example I'm trying to add a formated version of all the keys in @my_hash

Everything works well in insolation: If I instantiate a
MyViewMustache.new and call to 'key_to_html' the method_missing is
called.

But in a mustache template the MyViewMustache.method_missing is never called.

I have realized that someone is calling to MyViewMustache.respond_to?
first and if it responses 'false' the method call is never done.

My work around has been to over-write the 'respond_to?' as well as the
'method_missing' but something that was gonna be very simple it's
starting to smell to a mess.

Which is the recommended solution to this kind of 'dynamic' Views?

Thanks and Regards

f.

-- 
Fernando Guillén
Freelance Web Developer
http://www.fernandoguillen.info
http://spainrb.org/fernando-guillen

Re: [mustache] method_missing in my View

From:
Nicolás Sanguinetti
Date:
2011-02-20 @ 18:38
On Sun, Feb 20, 2011 at 3:42 PM, Fernando Guillen
<fguillen.mail@gmail.com> wrote:
> Hello,
>
> I'm trying to add 'magic' functionality to my Views and I'm trying to
> use 'method_missing' to do so.
>
> For example I want to implement this method_missing:
>
>  def method_missing( method_name, *args, &block )
>    if method_name.to_s =~ /_to_html$/
>      return @my_hash[method_name.gsub('_to_html','')].to_html
>    end
>    super
>  end
>
> (Very simplified example, and written in the air)
>
> In this example I'm trying to add a formated version of all the keys in @my_hash
>
> Everything works well in insolation: If I instantiate a
> MyViewMustache.new and call to 'key_to_html' the method_missing is
> called.
>
> But in a mustache template the MyViewMustache.method_missing is never called.
>
> I have realized that someone is calling to MyViewMustache.respond_to?
> first and if it responses 'false' the method call is never done.
>
> My work around has been to over-write the 'respond_to?' as well as the
> 'method_missing' but something that was gonna be very simple it's
> starting to smell to a mess.
>
> Which is the recommended solution to this kind of 'dynamic' Views?

When you override method_missing, ALWAYS override respond_to? As well.

ALWAYS.

It's not a smell :)

If you feel there's too much functionality shared, extract that to a
different method (or in fact, use respond_to? inside of
method_missing). But by redefining method_missing you're effectively
changing the interface of your object, by changing the methods that
can be called. So you have to override respond_to? to reflect that :)

Cheers,
-foca

> Thanks and Regards
>
> f.
>
> --
> Fernando Guillén
> Freelance Web Developer
> http://www.fernandoguillen.info
> http://spainrb.org/fernando-guillen
>

Re: [mustache] method_missing in my View

From:
Fernando Guillen
Date:
2011-02-20 @ 22:20
2011/2/20 Nicolás Sanguinetti <hi@nicolassanguinetti.info>:
> by redefining method_missing you're effectively
> changing the interface of your object, by changing the methods that
> can be called. So you have to override respond_to? to reflect that :)

Absolutely understood, I'll do it this way.

Thanks Nicolás.

f.

-- 
Fernando Guillén
Freelance Web Developer
http://www.fernandoguillen.info
http://spainrb.org/fernando-guillen