librelist archives

« back to archive

method_missing idea

method_missing idea

From:
Alan deLevie
Date:
2009-12-25 @ 00:15
This is my first message to the mailing list. Friendly looks damn promising.

I had a feature idea:

Supposed I have the model:
class Item
  include Friendly::Document
  attribute :name, String
end

I save a record of Item like so:
>> i = Item.new
=> #<Item:0x7fda11119c50>
>> i.name = "hello"
=> "hello"
>> i.foo = "bar"
NoMethodError: undefined method `foo=' for #<Item:0x7fda11119c50
@name="hello">
        from (irb):7

Why not on method_missing, dynamically add the 'foo' attribute onto Item and
save the record?

Thanks,
Alan

Re: [friendly] method_missing idea

From:
James Golick
Date:
2009-12-25 @ 01:08
Hi Alan,

There are a couple of reasons I'm not doing this.

First of all, I'm not crazy about method_missing. It usually ends up being
more trouble to debug than its worth. One of Friendly's design goals is to
avoid method_missing as much as possible.

The second reason is type-casting. JSON only has a few native types. When
you want to store anything else in your JSON, you have to have some type
conversion in place somewhere. To accomplish that, your attributes need to
be associated with a type, so that Friendly knows how to convert them (see
lib/friendly/attribute.rb).

-J.

On Thu, Dec 24, 2009 at 6:15 PM, Alan deLevie <adelevie@gmail.com> wrote:

> This is my first message to the mailing list. Friendly looks damn
> promising.
>
> I had a feature idea:
>
> Supposed I have the model:
> class Item
>   include Friendly::Document
>   attribute :name, String
> end
>
> I save a record of Item like so:
> >> i = Item.new
> => #<Item:0x7fda11119c50>
> >> i.name = "hello"
> => "hello"
> >> i.foo = "bar"
> NoMethodError: undefined method `foo=' for #<Item:0x7fda11119c50
> @name="hello">
>         from (irb):7
>
> Why not on method_missing, dynamically add the 'foo' attribute onto Item
> and save the record?
>
> Thanks,
> Alan
>