Class: Mime::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/mime-components/engine/engine.rb,
lib/mime-components/engine/render.rb,
lib/mime-components/engine/globals.rb,
lib/mime-components/extensions/erb.rb,
lib/mime-components/components/components.rb

Overview

The Engine is the primary class responsible for rendering Mime documents. Each engine contains a component registry as well as a set of built-in components.

Instance Method Summary collapse

Constructor Details

#initializeEngine

Initialize a new instance of the Mime templating engine.



10
11
12
13
14
15
# File 'lib/mime-components/engine/engine.rb', line 10

def initialize
   @global_vars = {}
   @registered_components = {}
   @component_regex = %r{(<[MmCc]:((?:[A-z0-9\:\-]+)|\#).*?/?>)}
   Mime.register_defaults(self)
end

Instance Method Details

#[](name) ⇒ Object

Get a global template variable.

Parameters:

  • name (String)

    name of variable to get value of

Returns:

  • (Object)

    value



20
21
22
# File 'lib/mime-components/engine/globals.rb', line 20

def [](name)
   @global_vars[name]
end

#[]=(name, value) ⇒ Object

Set a global template variable.

Parameters:

  • name (String)

    name of variable to set

  • value (Object)

    new value of variable (nil to unset)

Returns:

  • (Object)

    new value



13
14
15
# File 'lib/mime-components/engine/globals.rb', line 13

def []=(name, value)
   @global_vars[name] = value
end

#erb(template, vars) ⇒ String

Render an ERB template with Erubi, then post-process with this engine.

Parameters:

  • template (Erubi::Engine)

    rendered ERB template

  • vars (Hash)

    map of variables to send to template

Returns:

  • (String)

    rendered template



37
38
39
40
# File 'lib/mime-components/extensions/erb.rb', line 37

def erb(template, vars)
   template_output = instance_eval(template.src)
   render(template_output, vars)
end

#keysArray[Object]

Get a list of set global variables.

Returns:

  • (Array[Object])

    set keys



26
27
28
# File 'lib/mime-components/engine/globals.rb', line 26

def keys
   @global_vars.keys
end

#register_component(component) ⇒ Object

Register a new component for this engine.

Parameters:

  • component (Component)

    component to register



42
43
44
# File 'lib/mime-components/components/components.rb', line 42

def register_component(component)
   @registered_components[component.name.downcase] = component
end

#render(template, vars) ⇒ Object

Render all components in a ‘template’.

Parameters:

  • template (String)

    template as string

  • vars (Hash)

    variables to render with



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mime-components/engine/render.rb', line 10

def render(template, vars)
   @global_vars.each_key do |k|
      vars[k] = @global_vars[k]
   end
   # we locate, parse and replace tags to render the template. none of these
   # things work with frozen literals, so we'll make a copy to work on and return
   output = template.dup
   # find tags
   matches = output.scan(@component_regex)
   matches.each do |n|
      # render tag
      tag, component_name = n
      tag_out = if component_name == '#'
                   ''
                else
                   render_component(component_name, tag, vars)
                end
      # replace tag in output with its rendered value
      output.gsub!(tag, tag_out)
   end

   # freeze and return output
   output.freeze
   output
end

#render_component(name, xml, vars) ⇒ Object

Render a component based on its parsed XML tag.

Parameters:

  • name (String)

    component name

  • xml (String)

    raw component XML

  • vars (Hash)

    variables to pass to component



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mime-components/components/components.rb', line 50

def render_component(name, xml, vars)
   # get component, raising an error if it doesn't exist
   if (component = @registered_components[name.downcase]).nil?
      raise "#{xml}: component '#{name}' was not registered for this engine"
   end

   # normalize attributes
   xml = Nokogiri::XML.parse(xml).root
   attributes = {}
   xml.attributes.map do |k, v|
      attributes[k.to_s.strip.downcase.to_sym] = v
   end
   # render subcomponents of output
   render(component.render(attributes, vars).to_s, vars)
end