Hi Rick, hope I got it right:

class AutomobileManufacturer < ActiveRecord::Base
   has_many :automobiles

   def favorite_automobiles
     AutomobileCollection.from_association_collection automobiles
   end
   def all_automobiles
     favorite_automobiles | remote_automobiles
   end
   def remote_automobiles
     AutomobileCollection.from_webservice
   end

end

class AutomobileCollection

   def self.from_association_collection(collection)
     collection = collection.map { |a| AutomobileItem.from_database a }
     new collection
   end
   def self.from_webservice
     # load collection... or take it as argument
     collection = collection.map { |a| AutomobileItem.from_webservice  
a }
     new collection
   end

   # generate a hash to simplify intersect
   def initialize(collection)
     @collection_hash = collection.inject({}) { |hash, item|
       hash.merge item.identifier => item
     }
     @collection = collection
   end

   # this could possibly be done with ruby stdlib 'Delegate'
   def each(&block)
     @collection.each(&block)
   end

   # intersect with another automobile collections
   def |(other)
     new_keys = other.automobiles_hash.keys - @collection_hash.keys
     new_collection = @collection +  
other.automobiles_hash.values_at(*new_keys)

     self.class.new new_collection
   end

   class AutomobileItem

     # two seperate methods because I think the interface for
     # webservice automobile and database automobile wont match.

     def self.from_webservice(automobile)
       instance = new automobile.manufacturer # ...
       instance
     end
     def self.from_database(automobile)
       instance = new automobile.manufacturer # ...
       def instance.favor
         # nop ...
       end
       instance
     end

     def initialize(manufacturer, *further_attributes)
       @attributes_hash = {
         # ...
       }
     end
     # generate an unique but compareable identifier
     def identifier
       "#{ manufacturer } ..."
     end
     # save the record, or maybe not when boxed automobile was loaded  
from db...
     def favor
       Automobile.create @attributes_hash
       # nop
       def self.favor; end
     end
   end

end

To make a subclass of ActiveRecord::Base to something not subclassing  
it requires some voodoo magic. If you really interested in this kind  
of stuff you should gte yourself involved with the rubiunius project. ;)
Otherwise box them in something that would provide the same interface  
for SOAPed objects as for ORMed objects.

This code is fully untested, but I hope you get the idea.

Regards
Florian

Am 02.01.2009 um 20:46 schrieb Rick:

>
> I understand I can just define a class without extending
> ActiveReord::Base, but what if I want to use the model object
> sometimes without having the data persist. For example...
>
> Let's say I have an "AutoManufacturer" object it might have
> name=Mercedes,etc. But AutoManufacturer could contain a collection of
> "Automobile" objects, (class=CLS, model="CLS350, numberOfDoors=4").
> Maybe your db isn't the one storing ALL the Automobile objects, you
> might only want to store 'favorite automobiles' but you still want to
> collect all the Automobile information (class, model, etc) when you
> record a favorite.
>
> So now what if I go to my DB and get my "AutoManufacturer" objects and
> for each AutoManufacturer I query some webservice to return a list of
> Automobiles and I want to then display on a page the AutoManufacturers
> and a collection of the Automobile objects I returned from my
> webservice call. If I have "Automobile" defined as an ActiveRecord
> type when I do something like....
>
> automobiles = getCarsFromWebservice()
> automobiles.each do |a|
>    car = Automobile.new();
>    car.class= a.class
>    car.type= a.type
>    autoManufacturer.autos << car
> end
>
> every new car created above and added to the autoManufacturer actually
> creates a new car in the db, In this case all I want is fully
> populated list of autos in each autoManufacturer object for use to
> display on the front end, but I don't really want the automobile added
> to the db at this time. (Maybe after they select some, I'll then query
> a webservice and populate a real Automobile that I do want to persist
> in my db.)
>
> Currently I'm having to do something really lame. I'm creating
> basically identical objects - one a model object and one a simple
> pojo. It doesn't seem very dry to do it this way though. What's a
> better approach?
>
> -- 
> Rick
>
> >


--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to