Objectmix
Tags Register Mark Forums Read

metaprogramming to generate list of class-specific attributes : RUBY

This is a discussion on metaprogramming to generate list of class-specific attributes within the RUBY forums in Programming Languages category; Rubyists: Here's a test case illustrates the problem. Then we'll have a candidate fix and some questions. class Kozmik def self.acts_as_frog(species) @@frog_list << species end @@frog_list = [] cattr_accessor :frog_list end class Toad < Kozmik acts_as_frog :bufo acts_as_frog :kermit end class BullFrog < Kozmik acts_as_frog :tree acts_as_frog :riparian end def test_fun_with_metaclasses assert_equal [:tree, :riparian], BullFrog.frog_list assert_equal [:bufo, :kermit], Toad.frog_list end Suppose we need an 'acts_as_' system to generate some methods for our target classes. And suppose one method is a list of all the arguments passed to each acts_as_() in each specific class. The above test fails because @@ occupies ...


Object Mix > Programming Languages > RUBY > metaprogramming to generate list of class-specific attributes

Reply

 

LinkBack Thread Tools
  #1  
Old 09-14-2007, 03:05 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default metaprogramming to generate list of class-specific attributes

Rubyists:

Here's a test case illustrates the problem. Then we'll have a candidate fix
and some questions.

class Kozmik
def self.acts_as_frog(species)
@@frog_list << species
end
@@frog_list = []
cattr_accessor :frog_list
end

class Toad < Kozmik
acts_as_frog :bufo
acts_as_frog :kermit
end

class BullFrog < Kozmik
acts_as_frog :tree
acts_as_frog :riparian
end

def test_fun_with_metaclasses
assert_equal [:tree, :riparian], BullFrog.frog_list
assert_equal [:bufo, :kermit], Toad.frog_list
end

Suppose we need an 'acts_as_' system to generate some methods for our target
classes. And suppose one method is a list of all the arguments passed to
each acts_as_() in each specific class.

The above test fails because @@ occupies the entire inheritance chain. So
both frog_list() class-methods return [:tree, :riparian, :bufo, :kermit].
The fix seems to be to reach into the meta-class for each derived class:

class Kozmik
def self.acts_as_frog(species)
@frog_list ||= []
@frog_list << species
self.class.send :attr_accessor, :frog_list
end
end

Is that as clean and lean as it could be? Is there some way to do this with
even fewer lines?

Next question: Couldn't class Kozmik be a module instead?

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
  #2  
Old 09-14-2007, 04:21 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: metaprogramming to generate list of class-specific attributes

Phlip wrote:
...
> class Kozmik
> def self.acts_as_frog(species)
> @frog_list ||= []
> @frog_list << species
> self.class.send :attr_accessor, :frog_list
> end
> end
>
> Is that as clean and lean as it could be? Is there some way to do this with
> even fewer lines?
>
> Next question: Couldn't class Kozmik be a module instead?


Sure...

module Kozmik
def acts_as_frog(species)
@frog_list ||= []
@frog_list << species
self.class.send :attr_accessor, :frog_list
end
end

class Toad; extend Kozmik
...
end

Or is that cheating somehow?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  #3  
Old 09-14-2007, 04:27 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: metaprogramming to generate list of class-specific attributes

Joel VanderWerf wrote:

> class Toad; extend Kozmik


extend... different from include, right?

--
Phlip

  #4  
Old 09-14-2007, 04:38 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: metaprogramming to generate list of class-specific attributes

Phlip wrote:
> Joel VanderWerf wrote:
>
>> class Toad; extend Kozmik

>
> extend... different from include, right?


If you need it to be #include, you can always call #extend from within
#append_features as in ruby-talk:35979.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  #5  
Old 09-14-2007, 04:45 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: metaprogramming to generate list of class-specific attributes


On Sep 14, 2007, at 5:00 PM, Phlip wrote:

> Rubyists:

Here is one answering
> class Kozmik
> def self.acts_as_frog(species)
> @frog_list ||= []
> @frog_list << species
> self.class.send :attr_accessor, :frog_list
> end
> end
>
> Is that as clean and lean as it could be? Is there some way to do
> this with
> even fewer lines?


I am not really sure if this works, but couldn't you do:

class Kozmik
def self.inherited
module_eval do
@frog_list = []
end
end

def self.acts_as_frog(species)
@frog_list << species
end
end

Also, does module_eval only work in a class method? Why can't I do
def something; module_eval do puts 5 end; end ?

Ari
-------------------------------------------|
Nietzsche is my copilot



Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Generate XML with attributes from a cobol copybook usenet cobol 1 11-17-2007 01:07 PM
Adding attributes stored in a list to a class dynamically. usenet Python 7 09-03-2007 10:03 AM
How does VS05's Unit Test wizard generate a list of projects/namespace/class/methods to test? usenet CSharp 0 08-23-2007 03:43 PM
class attributes & data attributes usenet Python 2 06-20-2007 10:12 AM
over-riding DIV #id attributes with .class attributes!? usenet Adobe Tools 8 02-13-2007 12:23 PM


All times are GMT -5. The time now is 12:19 PM.

Managed by Infnx Pvt Ltd.