module DynamicParser::RegexCache

Public Class Methods

match(regex, txt, &block) click to toggle source
# File lib/dynamic_parser/regex_cache.rb, line 5
def self.match(regex, txt, &block)
  raise 'you must specify a block' unless block_given?
  retrieved_data = fetch("#{digest(regex.to_s)}/#{digest(txt)}") do
    matched = txt.match(regex)
    block.call(matched)
  end
end

Protected Class Methods

digest(s) click to toggle source
# File lib/dynamic_parser/regex_cache.rb, line 26
def self.digest(s)
  Digest::MD5.hexdigest(s)
end
fetch(key, expiration=300, &block) click to toggle source
# File lib/dynamic_parser/regex_cache.rb, line 14
def self.fetch(key, expiration=300, &block)
  raise 'you must specify a block' unless block_given?
  if defined?(Rails)
    Rails.cache.fetch(key, :expires_in => expiration) do
      block.call
    end
  else
    puts "[DynamicParser] No known driver available for caching! Using memory."
    @cache ||= block.call
  end
end