| 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 101
101: def DatabaseFeedCache.connected?
102: begin
103: ActiveRecord::Base.connection
104: return false if ActiveRecord::Base.configurations.nil?
105: return false unless DatabaseFeedCache.table_exists?
106: rescue => error
107: return false
108: end
109: return true
110: 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 48
48: def DatabaseFeedCache.initialize_cache
49: # Establish a connection if we don't already have one
50: begin
51: ActiveRecord::Base.default_timezone = :utc
52: ActiveRecord::Base.connection
53: rescue
54: end
55: if !ActiveRecord::Base.connected?
56: begin
57: possible_config_files = [
58: "./config/database.yml",
59: "./database.yml",
60: "../config/database.yml",
61: "../database.yml",
62: "../../config/database.yml",
63: "../../database.yml",
64: "../../../config/database.yml",
65: "../../../database.yml"
66: ]
67: database_config_file = nil
68: for file in possible_config_files
69: if File.exists?(File.expand_path(file))
70: database_config_file = file
71: @config_path = database_config_file
72: break
73: end
74: end
75: database_config_hash = File.open(database_config_file) do |file|
76: config_hash = YAML::load(file)
77: unless config_hash[FEED_TOOLS_ENV].nil?
78: config_hash = config_hash[FEED_TOOLS_ENV]
79: end
80: config_hash
81: end
82: ActiveRecord::Base.configurations = database_config_hash
83: ActiveRecord::Base.establish_connection(database_config_hash)
84: ActiveRecord::Base.connection
85: rescue
86: end
87: end
88: return nil
89: end
False if there is an error of any kind
# File lib/feed_tools/database_feed_cache.rb, line 113
113: def DatabaseFeedCache.set_up_correctly?
114: begin
115: ActiveRecord::Base.connection
116: if !ActiveRecord::Base.configurations.nil? &&
117: !DatabaseFeedCache.table_exists?
118: return false
119: end
120: rescue Exception
121: return false
122: end
123: return true
124: end
True if the appropriate database table already exists
# File lib/feed_tools/database_feed_cache.rb, line 127
127: def DatabaseFeedCache.table_exists?
128: begin
129: ActiveRecord::Base.connection.select_one("select id, href, title, " +
130: "link, feed_data, feed_data_type, http_headers, last_retrieved " +
131: "from #{self.table_name()}")
132: rescue ActiveRecord::StatementInvalid
133: return false
134: rescue
135: return false
136: end
137: return true
138: end