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