Module: ActionMailer::Markdown::TemplateHandler

Defined in:
lib/action_mailer/markdown/template_handler.rb

Defined Under Namespace

Classes: HTML, Text

Constant Summary collapse

UNDERSCORE =
"_"
OBJECT_ATTRIBUTE_MATCHER =
/%\{([a-z0-9_]+\.[a-z0-9_]+)\}/i.freeze

Class Method Summary collapse

Class Method Details

.expand_variables(template, variables) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/action_mailer/markdown/template_handler.rb', line 15

def self.expand_variables(template, variables)
  template
    .scan(OBJECT_ATTRIBUTE_MATCHER)
    .map(&:first)
    .each_with_object(variables) do |match, buffer|
      target, attribute = match.split(".")
      buffer[match.to_sym] = variables[target.to_sym].public_send(attribute)
    end
end

.extract_variables(context) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/action_mailer/markdown/template_handler.rb', line 25

def self.extract_variables(context)
  context
    .instance_variable_get(:@_assigns)
    .each_with_object({}) do |(name, value), buffer|
      next if name.start_with?(UNDERSCORE)

      buffer[name.to_sym] = value
    end
end

.render(template, context, format) ⇒ Object



9
10
11
12
13
# File 'lib/action_mailer/markdown/template_handler.rb', line 9

def self.render(template, context, format)
  variables = expand_variables(template, extract_variables(context))
  source = template.rstrip % variables
  ActionMailer::Markdown.public_send(format, source)
end