Class: Malt::Format::AbstractTemplate

Inherits:
Abstract show all
Defined in:
lib/malt/formats/abstract_template.rb

Overview

An AbstractTemplate is a subclass of Abstract. It is used as the base class for general purpose template formats which can be used to render any other type of format.

Direct Known Subclasses

Erb, Liquid, Mustache, Ruby, String, Tenjin

Instance Attribute Summary

Attributes inherited from Abstract

#options

Instance Method Summary collapse

Methods inherited from Abstract

#default, #engine, engine, extensions, #extensions, #file, file_extension, #file_read, #file_type, #parse_type_from_data, #refile, register, #render_into, #rendering_parameters, #scope_vs_data, #subtype, #text, #to_default, #to_s, #type, #with, #with!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &yld) ⇒ Object

ERB templates can be any type.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/malt/formats/abstract_template.rb', line 32

def method_missing(sym, *args, &yld)
  if Malt::Format.registry.key?(sym)  # TODO: Malt.machine.format?
    return render_into(sym, *args, &yld) #.to_s
  elsif md = /^to_/.match(sym.to_s)
    type = md.post_match.to_sym
    if Malt::Format.registry.key?(type)  # TODO: Malt.machine.format?
      return to(type, *args, &yld)
    end
  end
  super(sym, *args, &yld)
end

Instance Method Details

#render(*type_and_data, &content) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/malt/formats/abstract_template.rb', line 21

def render(*type_and_data, &content)
  type, data = parse_type_from_data(*type_and_data)
  #opts = options.merge(:to=>type, :text=>text, :file=>file, :data=>data)
  render_into(type, *data, &content)

  #render_engine.render(opts, &yld)
  #opts = options.merge(:format=>type, :text=>text, :file=>file, :data=>data, :engine=>engine)
  #Malt.render(opts, &yld)
end

#to(type, *data, &yld) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/malt/formats/abstract_template.rb', line 12

def to(type, *data, &yld)
  new_class   = Malt::Format.registry[type.to_sym]  # TODO: Malt.machine.format?
  new_text    = render_into(type, *data, &yld)
  new_file    = refile(type)
  new_options = options.merge(:text=>new_text, :file=>new_file, :type=>type)
  new_class.new(new_options)
end