require "sdb_dal/repository.rb" require "sdb_dal/memory_repository.rb" module SdbDal class RepositoryFactory def self.clear @repository=nil $sdb_dal_config=nil $repository=nil end def self.instance=(repo) @repository=repo end def self.instance(options={}) if options and options.has_key?(:repository) return options[:repository] end if @repository return @repository end sdb_dal_config=find_config_section if sdb_dal_config if sdb_dal_config.has_key?("repository_class") repo_class=eval sdb_dal_config["repository_class"] else repo_class=SdbDal::Repository end domain_prefix= sdb_dal_config["sdb_domain_prefix"] || "" clob_bucket= sdb_dal_config["s3_clob_bucket"] aws_key_id = sdb_dal_config["aws_key_id"] aws_secret_key = sdb_dal_config["aws_secret_key"] memcache_servers= sdb_dal_config['memcache_servers'] memcache_servers = memcache_servers.split(",") if memcache_servers append_table_to_domain=sdb_dal_config['append_table_to_domain'] options={} if sdb_dal_config.has_key?('fail_fast') options[:fail_fast]=sdb_dal_config['fail_fast'] end @repository= repo_class.new(domain_prefix,clob_bucket,aws_key_id,aws_secret_key,memcache_servers,nil,append_table_to_domain,options) end end def self.find_config_section return $sdb_dal_config if $sdb_dal_config config_file_path=nil config_section=nil #when using rails use config/database.yml if const_defined?(:RAILS_ROOT) and ENV.has_key?('RAILS_ENV') config_file_path = File.join("#{RAILS_ROOT}","config","database.yml") config_section =ENV['RAILS_ENV']+"_sdb_dal" else #when not using rails use try database.yml then try config/database.yml if File.exists?("database.yml") config_file_path = "database.yml" elsif File.exists?(File.join("config", "database.yml")) config_file_path = File.join("config", "database.yml") end config_section =(ENV['SDB_DAL_ENV'] || "production")+"_sdb_dal" end if config_file_path and config_section config_file = YAML.load(File.open( config_file_path)) $sdb_dal_config = config_file[config_section] end return $sdb_dal_config end def self.set_repository(repository) @repository=repository end end def self.qualified_const_get(str) path = str.to_s.split('::') from_root = path[0].empty? if from_root from_root = [] path = path[1..-1] else start_ns = ((Class === self)||(Module === self)) ? self : self.class from_root = start_ns.to_s.split('::') end until from_root.empty? begin return (from_root+path).inject(Object) { |ns,name| ns.const_get(name) } rescue NameError from_root.delete_at(-1) end end path.inject(Object) { |ns,name| ns.const_get(name) } end end