Class | FeedTools::DatabaseFeedCache |
In: |
lib/feed_tools/database_feed_cache.rb
|
Parent: | ActiveRecord::Base |
The default caching mechanism for the FeedTools module
Returns true if a connection to the database has been established and the required table structure is in place.
# File lib/feed_tools/database_feed_cache.rb, line 100 100: def DatabaseFeedCache.connected? 101: begin 102: ActiveRecord::Base.connection 103: return false if ActiveRecord::Base.configurations.nil? 104: return false unless DatabaseFeedCache.table_exists? 105: rescue => error 106: return false 107: end 108: return true 109: end
If ActiveRecord is not already connected, attempts to find a configuration file and use it to open a connection for ActiveRecord. This method is probably unnecessary for anything but testing and debugging purposes. In a Rails environment, the connection will already have been established and this method will simply do nothing.
This method should not raise any exceptions because it‘s designed to be run only when the module is first loaded. If it fails, the user should get an exception when they try to perform some action that makes use of the caching functionality, and not until.
# File lib/feed_tools/database_feed_cache.rb, line 47 47: def DatabaseFeedCache.initialize_cache 48: # Establish a connection if we don't already have one 49: begin 50: ActiveRecord::Base.default_timezone = :utc 51: ActiveRecord::Base.connection 52: rescue 53: end 54: if !ActiveRecord::Base.connected? 55: begin 56: possible_config_files = [ 57: "./config/database.yml", 58: "./database.yml", 59: "../config/database.yml", 60: "../database.yml", 61: "../../config/database.yml", 62: "../../database.yml", 63: "../../../config/database.yml", 64: "../../../database.yml" 65: ] 66: database_config_file = nil 67: for file in possible_config_files 68: if File.exists?(File.expand_path(file)) 69: database_config_file = file 70: @config_path = database_config_file 71: break 72: end 73: end 74: database_config_hash = File.open(database_config_file) do |file| 75: config_hash = YAML::load(file) 76: unless config_hash[FEED_TOOLS_ENV].nil? 77: config_hash = config_hash[FEED_TOOLS_ENV] 78: end 79: config_hash 80: end 81: ActiveRecord::Base.configurations = database_config_hash 82: ActiveRecord::Base.establish_connection(database_config_hash) 83: ActiveRecord::Base.connection 84: rescue 85: end 86: end 87: return nil 88: end
False if there is an error of any kind
# File lib/feed_tools/database_feed_cache.rb, line 112 112: def DatabaseFeedCache.set_up_correctly? 113: begin 114: ActiveRecord::Base.connection 115: if !ActiveRecord::Base.configurations.nil? && 116: !DatabaseFeedCache.table_exists? 117: return false 118: end 119: rescue Exception 120: return false 121: end 122: return true 123: end
True if the appropriate database table already exists
# File lib/feed_tools/database_feed_cache.rb, line 126 126: def DatabaseFeedCache.table_exists? 127: begin 128: ActiveRecord::Base.connection.select_one("select id, href, title, " + 129: "link, feed_data, feed_data_type, http_headers, last_retrieved " + 130: "from #{self.table_name()}") 131: rescue ActiveRecord::StatementInvalid 132: return false 133: rescue 134: return false 135: end 136: return true 137: end