I''m totally new to Rails so if there are any tutorials that you could point me to to help answer this question, that''d be great. I want to create an mp3 object which will be initialized in 2 ways. When enumerating the mp3s from the hard drive, if an mp3 is not in the database already, it will created and committed to the database, otherwise, it''ll be retirieved. The mp3 object has some info that has to be checked for consistency if it comes from the db, or that has to be calculated if it''s not in the db yet (ie. tag info). The scenario when it''s not already in the db is that we create an Mp3Info object for the file, extract some of the fields, and get some filesystem info, which ends up being the entirety of what gets stored in the db. So I _could_ do something like this: file = ... mp3info = Mp3Info.new(file) mp3 = Mp3.new(:title => get_title(mp3info), :size => File.size(file), ...) but what I''d rather do is something like mp3 = Mp3.new(file). So I guess what I''m probably looking to do is to overload the constructor, right? How do I do this? Right now I already have an Mp3 class which has no idea about databases or active record. It''d be nice if I could use that class rather have to rewrite it completely (not that that would be so hard). Lowell
On Jun 27, 2005, at 7:53 PM, Lowell Kirsh wrote:> I''m totally new to Rails so if there are any tutorials that you could > point me to to help answer this question, that''d be great. > > I want to create an mp3 object which will be initialized in 2 ways. > When enumerating the mp3s from the hard drive, if an mp3 is not in the > database already, it will created and committed to the database, > otherwise, it''ll be retirieved.What are you storing in the database? The mp3 tag info only, or the tag info plus the actual mp3 binary data? Also, when you say "it''ll be retrieved" what do you mean? Under what circumstances would you want to store an mp3 file, but if it already exists, retrieve it?> The mp3 object has some info that has > to be checked for consistency if it comes from the db, or that has to > be calculated if it''s not in the db yet (ie. tag info). The scenario > when it''s not already in the db is that we create an Mp3Info object > for the file, extract some of the fields, and get some filesystem > info, which ends up being the entirety of what gets stored in the db. > So I _could_ do something like this: > > file = ... > mp3info = Mp3Info.new(file) > mp3 = Mp3.new(:title => get_title(mp3info), :size => File.size > (file), ...) >This looks like a strange mix of procedural and object oriented programming. Would it be possible, perhaps, to use some getters within the Mp3Info class to get the information (such as title and file size) from your Mp3Info object? E.g. I''m thinking: class Mp3Info def title # ... return the title of the mp3 here end def size # ... return the size here end end So that you could then instantiate your ActiveRecord object like this: mp3info = Mp3Info.new(file) mp3 = Mp3.new(:title => mp3info.title, :size => mp3info.size, ...)> but what I''d rather do is something like mp3 = Mp3.new(file). >One possibility would be to convert your Mp3Info class into a module. If you can do that, then creating your ActiveRecord::Base - derived Mp3 class would be very easy since you could mix the methods from your module into your new class: require ''mp3_info'' class Mp3 < ActiveRecord::Base include Mp3Info end> So I guess what I''m probably looking to do is to overload the > constructor, right? How do I do this?To overload a constructor (and call the parent class''s constructor): def initialize(file) # handle the file and set instance variables super end You might want to read up on the pickaxe book[1] or Why''s Poignant Guide[2] for further Ruby-related knowledge :) Duane Johnson (canadaduane) [1] http://www.whytheluckystiff.net/ruby/pickaxe/ [2] http://poignantguide.net/ruby/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks Duane. What I want to do is store in the database the filename and some of the metadata about the mp3 (tags, size, modified date...). I guess I was unclear, but your answer still hit the spot. I guess rather than lumping everything together in the Mp3 class, It''d be better to use a mixin for the Mp3Info class. I''ll get to reading the pickaxe book soon and learn more about mixins. Lowell On 6/27/05, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Jun 27, 2005, at 7:53 PM, Lowell Kirsh wrote: > > I''m totally new to Rails so if there are any tutorials that you could > point me to to help answer this question, that''d be great. > > I want to create an mp3 object which will be initialized in 2 ways. > When enumerating the mp3s from the hard drive, if an mp3 is not in the > database already, it will created and committed to the database, > otherwise, it''ll be retirieved. > > What are you storing in the database? The mp3 tag info only, or the tag > info plus the actual mp3 binary data? > > Also, when you say "it''ll be retrieved" what do you mean? Under what > circumstances would you want to store an mp3 file, but if it already exists, > retrieve it? > > The mp3 object has some info that has > to be checked for consistency if it comes from the db, or that has to > be calculated if it''s not in the db yet (ie. tag info). The scenario > when it''s not already in the db is that we create an Mp3Info object > for the file, extract some of the fields, and get some filesystem > info, which ends up being the entirety of what gets stored in the db. > So I _could_ do something like this: > > file = ... > mp3info = Mp3Info.new(file) > mp3 = Mp3.new(:title => get_title(mp3info), :size => File.size(file), ...) > > > This looks like a strange mix of procedural and object oriented programming. > Would it be possible, perhaps, to use some getters within the Mp3Info class > to get the information (such as title and file size) from your Mp3Info > object? > > E.g. I''m thinking: > > class Mp3Info > def title > # ... return the title of the mp3 here > end > > def size > # ... return the size here > end > end > > So that you could then instantiate your ActiveRecord object like this: > > mp3info = Mp3Info.new(file) > mp3 = Mp3.new(:title => mp3info.title, :size => mp3info.size, ...) > > > but what I''d rather do is something like mp3 = Mp3.new(file). > > One possibility would be to convert your Mp3Info class into a module. If > you can do that, then creating your ActiveRecord::Base -derived Mp3 class > would be very easy since you could mix the methods from your module into > your new class: > > require ''mp3_info'' > class Mp3 < ActiveRecord::Base > include Mp3Info > end > > > So I guess what I''m probably looking to do is to overload the > constructor, right? How do I do this? > > > To overload a constructor (and call the parent class''s constructor): > > def initialize(file) > # handle the file and set instance variables > super > end > > You might want to read up on the pickaxe book[1] or Why''s Poignant Guide[2] > for further Ruby-related knowledge :) > > Duane Johnson > (canadaduane) > > [1] http://www.whytheluckystiff.net/ruby/pickaxe/ > [2] http://poignantguide.net/ruby/ >