<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">I got three private replies, which were
all useful, so I'll summarize here for reference:<br>
<div class="moz-cite-prefix"><br>
On 14/03/13 08:40, Jared Fraser wrote:<br>
</div>
<blockquote
cite="mid:CADV5iK49S08J1wUrbcGgdgA5E3pp61AVruUveTZcQfRvm1ysFw@mail.gmail.com"
type="cite">If they're all active record based, you can do
ActiveRecord::Base.subclasses assuming they're all loaded (like
in production)
<div><br>
</div>
<div>Jared<span></span><br>
</div>
</blockquote>
I'm not using ActiveRecord, but that subclasses method sure would
be useful! I forgot to mention I use Sinatra, not Rails, and no
classes seem to respond to that method, unfortunately.<br>
<br>
<br>
<div class="moz-cite-prefix">On 14/03/13 08:50, George Drummond
wrote:<br>
</div>
<blockquote
cite="mid:CAKDS2DO3gDOtZxMVF8qpzJj-9sq332+TX-Cs2CcwdG45LQkmaQ@mail.gmail.com"
type="cite">
<div dir="ltr"><br>
<div class="gmail_extra">
<div class="gmail_quote">On Thu, Mar 14, 2013 at 12:38 AM,
Ronny Ager-Wick <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:ronny@ager-wick.com" target="_blank">ronny@ager-wick.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">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.<br>
I want to loop through the defined classes to see which
ons of them responds to certain methods.<br>
The classes I'm trying to find are Ohm Models that have
a certain attribute defined.<br>
If I had a collection of classes, I'd probably do
something like this:<br>
<br>
classes = #get list of all classes<br>
classes.each { |c|<br>
# assume it's an Ohm::Model if it responds to :first<br>
# maybe there's a better way of filtering out only the
Ohm models<br>
</blockquote>
<div> </div>
<div style=""># Will work if they are all Ohm::Model
objects, wont however work if you have something that
inherits from this class like Car < Ohm::Model</div>
<div style="">if c.is_a? Ohm::Model <br>
</div>
</div>
</div>
</div>
</blockquote>
Alas, that's exactly what I have.<br>
<br>
<blockquote
cite="mid:CAKDS2DO3gDOtZxMVF8qpzJj-9sq332+TX-Cs2CcwdG45LQkmaQ@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div style=""><br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
if c.respond_to?(:first) &&
c.first.respond_to?(:updated_at)<br>
# convert the timestamp of all records in this
model.<br>
end<br>
end<br>
<br>
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.<br>
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.<br>
</blockquote>
<div><br>
</div>
<div style="">A better approach may be to fork the gem
this is in and rewrite the methods that return a UTC
date and have a conditional to return the unix
timestamp?</div>
</div>
</div>
</div>
</blockquote>
Now that is a really good idea! I'll see what I can do with that.
I was thinking of running my conversion before upgrading Ohm and
Ohm-contrib, because after upgrade, I can't even access any of the
records:<br>
$ irb -r ./init.rb <br>
irb(main):001:0> Person[1]<br>
NoMethodError: undefined method `2011-02-25 21:52:26 UTC=' for
:Person<br>
from
/home/user/app/current/vendor/ohm-1.2.0/lib/ohm.rb:1380:in `block
in update_attributes'<br>
from
/home/user/app/current/vendor/ohm-1.2.0/lib/ohm.rb:1380:in `each'<br>
from
/home/user/app/current/vendor/ohm-1.2.0/lib/ohm.rb:1380:in
`update_attributes'<br>
from
/home/user/app/current/vendor/ohm-1.2.0/lib/ohm.rb:1142:in `load!'<br>
from /home/user/app/current/vendor/ohm-1.2.0/lib/ohm.rb:746:in
`[]'<br>
from
/home/user/app/current/vendor/ohm-contrib-1.2/lib/ohm/slug.rb:9:in
`[]'<br>
from (irb):1<br>
from /usr/bin/irb:12:in `<main>'<br>
irb(main):002:0> <br>
It would be a good idea to patch the new version to actually
understand old data! Or in worst case, patch the old version to be
able to write new data.<br>
<br>
<br>
<div class="moz-cite-prefix">On 14/03/13 08:56, Andrew McDonough
wrote:<br>
</div>
<blockquote
cite="mid:CAGEPW5gDEHuJOKAoVSz+WCFz2Gas88EDG2VTrVQY0KByHi0+4A@mail.gmail.com"
type="cite">Assuming all the classes you want to find inherit
from Ohm::Model, you could just do:
<div><br>
</div>
<div style="">Ohm::Model.subclasses</div>
<div style=""><br>
</div>
<div style="">Andrew</div>
</blockquote>
Yes they do, but I don't have that handy subclasses method.
However after consulting with my knowledgeable friend Google, I
found one (
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<a
href="https://groups.google.com/forum/?fromgroups=#%21topic/comp.lang.ruby/DoEsfO9EapY">https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.ruby/DoEsfO9EapY</a>)
that I improved a little to remove the class (Ohm::Model in my
case) itself:<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<a href="https://gist.github.com/agerwick/5158730">https://gist.github.com/agerwick/5158730</a><br>
<br>
So now I can simply do:<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<pre class="line-pre" style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 12px; line-height: 16px; font-family: Consolas, 'Liberation Mono', Courier, monospace; white-space: pre; word-wrap: break-word; width: 847px; margin: 0px; padding: 0px; color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><div class="line" id="file-subclasses-rb-LC1"><span class="k" style="font-weight: bold;">class</span> <span class="nc" style="color: rgb(68, 85, 136); font-weight: bold;">Class</span></div><div class="line" id="file-subclasses-rb-LC2"> <span class="k" style="font-weight: bold;">def</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">subclasses</span></div><div class="line" id="file-subclasses-rb-LC3"> <span class="n" style="color: rgb(5
1, 51, 51);">class_hash</span> <span class="o" style="font-weight: bold;">=</span> <span class="p">{}</span></div><div class="line" id="file-subclasses-rb-LC4"> <span class="no" style="color: teal;">ObjectSpace</span><span class="o" style="font-weight: bold;">.</span><span class="n" style="color: rgb(51, 51, 51);">each_object</span> <span class="k" style="font-weight: bold;">do</span> <span class="o" style="font-weight: bold;">|</span><span class="n" style="color: rgb(51, 51, 51);">obj</span><span class="o" style="font-weight: bold;">|</span></div><div class="line" id="file-subclasses-rb-LC5"> <span class="k" style="font-weight: bold;">if</span> <span class="no" style="color: teal;">Class</span> <span class="o" style="font-weight: bold;">==</span> <span class="n" style="color: rgb(51, 51, 51);">obj</span><span class="o" style="font-weight: bold;">.</span><span class="n" style="color: rgb(51, 51, 51);">class</span></div><div class="line" id="file-subclasses-rb-LC6"> <spa
n class="c1" style="color: rgb(153, 153, 136); font-style: italic;"># remove first item from ancestors, which is the class itself - it's wrong to say a class is a subclass of itself</span></div><div class="line" id="file-subclasses-rb-LC7"> <span class="k" style="font-weight: bold;">if</span> <span class="n" style="color: rgb(51, 51, 51);">obj</span><span class="o" style="font-weight: bold;">.</span><span class="n" style="color: rgb(51, 51, 51);">ancestors</span><span class="o" style="font-weight: bold;">[</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="o" style="font-weight: bold;">.</span><span class="n" style="color: rgb(51, 51, 51);">.</span><span class="o" style="font-weight: bold;">-</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="o" style="font-weight: bold;">].</span><span class="n" style="color: rgb(51, 51, 51);">include?</span> <span class="nb" style="color: rgb(0, 134, 179);">self</span></div><div class="line"
id="file-subclasses-rb-LC8"> <span class="n" style="color: rgb(51, 51, 51);">class_hash</span><span class="o" style="font-weight: bold;">[</span><span class="n" style="color: rgb(51, 51, 51);">obj</span><span class="o" style="font-weight: bold;">]</span> <span class="o" style="font-weight: bold;">=</span> <span class="kp" style="font-weight: bold;">true</span></div><div class="line" id="file-subclasses-rb-LC9"> <span class="k" style="font-weight: bold;">end</span></div><div class="line" id="file-subclasses-rb-LC10"> <span class="k" style="font-weight: bold;">end</span></div><div class="line" id="file-subclasses-rb-LC11"> <span class="k" style="font-weight: bold;">end</span></div><div class="line" id="file-subclasses-rb-LC12"> <span class="n" style="color: rgb(51, 51, 51);">class_hash</span><span class="o" style="font-weight: bold;">.</span><span class="n" style="color: rgb(51, 51, 51);">keys</span></div><div class="line" id="file-subclasses-rb-LC13"> <span
class="k" style="font-weight: bold;">end</span></div><div class="line" id="file-subclasses-rb-LC14"><span class="k" style="font-weight: bold;">end</span></div></pre>
<tt><br>
Ohm::Model.subclasses.each do |c|</tt><tt><br>
</tt><tt> if c.new.respond_to?(:created_at)</tt><tt><br>
</tt><tt> # deal with it</tt><tt><br>
</tt><tt> end</tt><tt><br>
</tt><tt> </tt><tt>c.new.respond_to?(:updated_at)</tt><tt><br>
</tt><tt> # deal with it</tt><tt><br>
</tt><tt> end</tt><tt><br>
</tt><tt>end</tt><br>
<br>
Thanks for the great response everybody!<br>
Ronny.<br>
</div>
</body>
</html>