guillaume.garcera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-04 22:33 UTC
Meta programming problem
Hi,
i''ve wrote the following code module TeamScore
class Filter
ALLOWED_RESOURCES = [:game]
# convert <game:4 title> into markdown link to game re
# use game title as link name
# "Google":http://google.com.
#
def self.filter(str)
str.gsub(/<([a-z]+):(\d+)>/) do |match|
create_link_for($1.to_sym,$2, match)
end
end
def self.create_link_for(resource_type, id, match)
raise "Macro #{match} not allowed" unless
ALLOWED_RESOURCES.include?(resource_type.to_sym)
case resource_type
when :game
model = Game
when :article
model = Article
end
object = model.send("find", id)
"\"#{object.name}\":www.teamscore.org/#{object.class.name.pluralize.downcase}/#{object.id}"
end
end
end
How i can dry case when block.
My idea is to send a find message to model coresponding the
resource_type argument but resource_type is a string (previously
extracted from regexp) and I can''t use ressource_type.send(....)
or send("#{ressource_type.classify}.find", id) the 2 stament raise an
error (NoMethodError: undefined method `Game'' for
TeamScore::Filter:Class)
If you have any tips or idea
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 12/5/06, guillaume.garcera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <guillaume.garcera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > i''ve wrote the following code module TeamScore > class Filter > ALLOWED_RESOURCES = [:game] > > # convert <game:4 title> into markdown link to game re > # use game title as link name > # "Google":http://google.com. > # > def self.filter(str) > str.gsub(/<([a-z]+):(\d+)>/) do |match| > create_link_for($1.to_sym,$2, match) > end > end > > def self.create_link_for(resource_type, id, match) > raise "Macro #{match} not allowed" unless > ALLOWED_RESOURCES.include?(resource_type.to_sym) > case resource_type > when :game > model = Game > when :article > model = Article > end > object = model.send("find", id) > > "\"#{object.name}\":www.teamscore.org/#{object.class.name.pluralize.downcase}/#{object.id}" > end > end > end > > How i can dry case when block.Remove it altogether ;) see below.> My idea is to send a find message to model coresponding the > resource_type argument but resource_type is a string (previously > extracted from regexp) and I can''t use ressource_type.send(....) > or send("#{ressource_type.classify}.find", id) the 2 stament raise an > error (NoMethodError: undefined method `Game'' for > TeamScore::Filter:Class)You don''t need the send here. Send without an object tries to execute send on the current scope, which would be TeamScore::Filter. In this case, you are trying to execute self.Game.find(id), which is of course not valid (unless you happened to have a constant called Game defined on the class...). Try resource_type.classify.find(id). resource_type.classify will simply return the class (Game or Article), which you can happily call any methods on. Cheers, Max> > If you have any tips or idea > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
guillaume.garcera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-05 08:03 UTC
Re: Meta programming problem
Max Muermann a écrit :> Remove it altogether ;) see below. > > > My idea is to send a find message to model coresponding the > > resource_type argument but resource_type is a string (previously > > extracted from regexp) and I can''t use ressource_type.send(....) > > or send("#{ressource_type.classify}.find", id) the 2 stament raise an > > error (NoMethodError: undefined method `Game'' for > > TeamScore::Filter:Class) > > You don''t need the send here. Send without an object tries to execute > send on the current scope, which would be TeamScore::Filter. In this > case, you are trying to execute self.Game.find(id), which is of course > not valid (unless you happened to have a constant called Game defined > on the class...). > > Try resource_type.classify.find(id). > > resource_type.classify will simply return the class (Game or Article), > which you can happily call any methods on. > > Cheers, > MaxThanks, I''ve try it but this doesn''t work. I''ve got an LocalJumpError or something like this. I think it try to invoke find on a String not on the Game or Article. The classify method just retunr a string in my mind. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guillaume :> > You don't need the send here. Send without an object tries to execute > > send on the current scope, which would be TeamScore::Filter. In this > > case, you are trying to execute self.Game.find(id), which is of course > > not valid (unless you happened to have a constant called Game defined > > on the class...). > > > > Try resource_type.classify.find(id). > > > > resource_type.classify will simply return the class (Game or Article), > > which you can happily call any methods on. > > Thanks, > > I've try it but this doesn't work. > I've got an LocalJumpError or something like this. I think it try to > invoke find on a String not on the Game or Article. The classify method > just retunr a string in my mind.Try : resource_type.classify.constantize.find(id) -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
guillaume.garcera-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-05 19:02 UTC
Re: Meta programming problem
This work great. Thank a lot. Jean-François a écrit :> Try : > > resource_type.classify.constantize.find(id) > > -- Jean-François. > > > -- > À la renverse.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---