require 'pickle' module FWToolkit module Test class FrankCache @@klass_to_objects = {} @@id_to_object = {} def self.add_object(object) object.id = object.object_id # add to id to object hash @@id_to_object[object.object_id.to_s] = object # add to klass to object hash @@klass_to_objects[object.class.name] = [] unless @@klass_to_objects[object.class.name] @@klass_to_objects[object.class.name] << object end def self.all_objects(klass) @@klass_to_objects[klass.name] ? @@klass_to_objects[klass.name] : [] end def self.find(id) @@id_to_object[id.to_s] end def self.find_first(klass, conditions) self.find_all(klass, conditions).first end def self.find_all(klass, conditions) objects = [] FWToolkit::Test::FrankCache.all_objects(klass).each do |o| match = true conditions.keys.each do |k| if not eval("o.#{k}").eql? conditions[k] match = false break end end objects << o if match end objects end end class FrankModel attr_accessor :id, :core_data_entity module PickleAdapter include Pickle::Adapter::Base # Gets a list of the available models for this adapter def self.model_classes klasses = ::FWToolkit::Test::FrankModel.__send__(:descendants) end # get a list of column names for a given class def self.column_names(klass) klass.new.methods end # Get an instance by id of the model def self.get_model(klass, id) FWToolkit::Test::FrankCache.find(id) end # Find the first instance matching conditions def self.find_first_model(klass, conditions) FWToolkit::Test::FrankCache.find_first(klass, conditions) end # Find all models matching conditions def self.find_all_models(klass, conditions) FWToolkit::Test::FrankCache.find_all(klass, conditions) end # Create a model using attributes def self.create_model(klass, attributes) puts "create_model" end end def self.object_in_cache(id) FWToolkit::Test::FrankCache.find(id) end def self.all_objects FWToolkit::Test::FrankCache.all_objects(self) end def save! FWToolkit::Test::FrankCache.add_object(self) end def self.find(id) FWToolkit::Test::FrankCache.find(id) end def self.find_first(conditions={}) FWToolkit::Test::FrankCache.find_first(self, conditions) end def self.find_all(conditions={}) FWToolkit::Test::FrankCache.find_all(self, conditions) end end end end