Class: Mjml::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/mjml/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path) ⇒ Cache

Returns a new instance of Cache.



7
8
9
# File 'lib/mjml/cache.rb', line 7

def initialize(template_path)
  @template_path = template_path
end

Instance Attribute Details

#template_pathObject (readonly)

Returns the value of attribute template_path.



5
6
7
# File 'lib/mjml/cache.rb', line 5

def template_path
  @template_path
end

Instance Method Details

#cache { ... } ⇒ String

Yields:

  • -> String

Returns:

  • (String)


13
14
15
16
17
18
19
20
21
22
23
24
# 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