Parent

Class/Module Index [+]

Quicksearch

FeedTools::DatabaseFeedCache

The default caching mechanism for the FeedTools module

Public Class Methods

config_path() click to toggle source

Returns the path to the database.yml config file that FeedTools loaded.

# File lib/feed_tools/database_feed_cache.rb, line 91
def DatabaseFeedCache.config_path
  if !defined?(@config_path) || @config_path.blank?
    @config_path = nil
  end
  return @config_path
end
connected?() click to toggle source

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
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() click to toggle source

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
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.expand_path(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?() click to toggle source

False if there is an error of any kind

# File lib/feed_tools/database_feed_cache.rb, line 112
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?() click to toggle source

True if the appropriate database table already exists

# File lib/feed_tools/database_feed_cache.rb, line 126
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.