Module: Leftbrained::HasWebFallback::IncludedClassMethods

Defined in:
lib/leftbrained/has_web_fallback.rb

Instance Method Summary collapse

Instance Method Details

#cache_deadline_for_web_fallback(method_name) ⇒ Object



56
57
58
# File 'lib/leftbrained/has_web_fallback.rb', line 56

def cache_deadline_for_web_fallback(method_name)
  Time.now - cache_period_for_method_with_web_fallback(method_name)
end

#cache_period_for_method_with_web_fallback(method_name) ⇒ Object



85
86
87
# File 'lib/leftbrained/has_web_fallback.rb', line 85

def cache_period_for_method_with_web_fallback(method_name)
  methods_with_web_fallback[method_name.to_sym][:keep_for]
end

#clear_web_fallback_cache(method_name) ⇒ Object



66
67
68
69
70
# File 'lib/leftbrained/has_web_fallback.rb', line 66

def clear_web_fallback_cache(method_name)
  web_fallback_caches_to_be_cleared(method_name).each do |file|
    File.unlink(file)
  end
end

#clear_web_fallback_cachesObject



60
61
62
63
64
# File 'lib/leftbrained/has_web_fallback.rb', line 60

def clear_web_fallback_caches
  methods_with_web_fallback.keys.each do |meth_name|
    clear_web_fallback_cache meth_name
  end
end

#default_url_for_web_fallback(method_name) ⇒ Object



97
98
99
# File 'lib/leftbrained/has_web_fallback.rb', line 97

def default_url_for_web_fallback(method_name)
  "/system/web_fallback/#{self.to_s.underscore.gsub("_mailer", '')}/#{method_name}"
end

#has_web_fallback?(method_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/leftbrained/has_web_fallback.rb', line 43

def has_web_fallback?(method_name=nil)
  return true if method_name.nil?
  methods_with_web_fallback.include? method_name.to_sym
end

#methods_with_web_fallbackObject



52
53
54
# File 'lib/leftbrained/has_web_fallback.rb', line 52

def methods_with_web_fallback
  read_inheritable_attribute(:methods_with_web_fallback)
end

#path_for_web_fallback(method_name) ⇒ Object



101
102
103
# File 'lib/leftbrained/has_web_fallback.rb', line 101

def path_for_web_fallback(method_name)
  File.join(Leftbrained::HasWebFallback.has_web_fallback_cache_path, url_for_web_fallback(method_name))
end

#url_for_web_fallback(method_name) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/leftbrained/has_web_fallback.rb', line 89

def url_for_web_fallback(method_name)
  if methods_with_web_fallback[method_name.to_sym]
    methods_with_web_fallback[method_name.to_sym][:cache_path] || default_url_for_web_fallback(method_name)
  else
    default_url_for_web_fallback(method_name)
  end
end

#web_fallback_caches_to_be_cleared(method_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/leftbrained/has_web_fallback.rb', line 72

def web_fallback_caches_to_be_cleared(method_name)
  return [] unless has_web_fallback? method_name
  cut_off_time = cache_deadline_for_web_fallback(method_name)
  path = path_for_web_fallback(method_name)
  deletable = []
  if File.exists? path
    Find.find(path) do |path|
      deletable << path if (File.mtime(path) <= cut_off_time) && File.file?(path)
    end
  end
  deletable
end

#web_fallback_uuid_for(method_name) ⇒ Object



48
49
50
# File 'lib/leftbrained/has_web_fallback.rb', line 48

def web_fallback_uuid_for(method_name)
  UUIDTools::UUID.random_create.to_s<<".html"
end