class Mjml::Cache
Attributes
Public Class Methods
Source
# File lib/mjml/cache.rb, line 7 def initialize(template_path) @template_path = template_path end
Public Instance Methods
Source
# File lib/mjml/cache.rb, line 13 def cache(&block) return yield if !Mjml.cache_mjml && block cached_path = cached_file_path if File.exist?(cached_path) File.read(cached_path) else html_content = yield if block File.write(cached_path, html_content) html_content end end
@yield [] -> String @return [String]
Private Instance Methods
Source
# File lib/mjml/cache.rb, line 39 def cache_directory dir = File.join(Dir.pwd, 'tmp', 'mjml_cache') FileUtils.mkdir_p(dir) unless Dir.exist?(dir) dir end
Source
# File lib/mjml/cache.rb, line 28 def cached_file_path File.join(cache_directory, "#{fingerprint}.html") end
Source
# File lib/mjml/cache.rb, line 32 def fingerprint full_path = File.join(Dir.pwd, 'app', 'views', "#{template_path}.mjml") raise "Template file not found: #{full_path}" unless File.exist?(full_path) Digest::SHA256.hexdigest(File.read(full_path)) end