[LRUG] Looping through classes

Srushti Ambekallu srshti at gmail.com
Wed Mar 13 22:45:34 PDT 2013


Collapsing that down you could do:

	subclasses = ObjectSpace.each_object do |klass|
		if klass.is_a?(Class) && klass.ancestors.include?(Ohm::Model) && klass.instance_methods.include?(:created_at) && klass.instance_methods.include?(:updated_at)
			# do the conversion
		end
	end

Or you could put that into a method and parameterise "Ohm::Model" so that it can be reused for other classes too.

Thanks,
Srushti
http://c42.in

On 14-Mar-2013, at 8:10 AM, Ronny Ager-Wick wrote:

> Thanks, Jon,
> this is practically what I asked for - a list of all of the objects I need, and a few more. For some reason I got an error on the last line. https://gist.github.com/JonRowe/5158141#comment-797997 Not to worry though, with all the information I got, I'm well on my way to a solution.
> Ronny.
> 
> 
> On 14/03/13 09:42, Jon Rowe wrote:
>> You can get the constants of Object or some other top level object, map them into the actual constants (cause Ruby returns them as symbols),and then filter them…
>> 
>> Something along the lines of this… https://gist.github.com/JonRowe/5158141
>> 
>> Jon Rowe
>> -----------------------------
>> mail at jonrowe.co.uk
>> jonrowe.co.uk
>> 
>> On Thursday, 14 March 2013 at 11:38, Ronny Ager-Wick wrote:
>> 
>>> I've been trying to find a way to do this, but I may be searching for the
>>> wrong terms or something because I get nowhere, even with the help of my
>>> friend Google.
>>> I want to loop through the defined classes to see which ons of them responds
>>> to certain methods.
>>> The classes I'm trying to find are Ohm Models that have a certain attribute
>>> defined.
>>> If I had a collection of classes, I'd probably do something like this:
>>> 
>>> classes = #get list of all classes
>>> classes.each { |c|
>>> # assume it's an Ohm::Model if it responds to :first
>>> # maybe there's a better way of filtering out only the Ohm models
>>> if c.respond_to?(:first) && c.first.respond_to?(:updated_at)
>>> # convert the timestamp of all records in this model.
>>> end
>>> end
>>> 
>>> The end objective is to convert the timestamp from the old way they used to
>>> store it (Ohm-contrib 0.*) which looks like this "2011-02-25 21:54:06 UTC",
>>> and the new way, which are UNIX timestamps.
>>> I could always just make a list of the models manually and loop through them,
>>> but I want to make a reusable script that others can benefit from as well.
>>> Ronny.
>>> _______________________________________________
>>> Chat mailing list
>>> Chat at lists.lrug.org
>>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>> 
> 
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20130314/676b7bab/attachment-0003.html>


More information about the Chat mailing list