class Talktome::Message

Attributes

data[RW]
metadata[RW]
path[RW]
template_content[RW]

Public Class Methods

new(path, options = {}) click to toggle source
# File lib/talktome/message.rb, line 4
def initialize(path, options = {})
  @path = path
  @options = options
  compile
end

Public Instance Methods

dup() { |d| ... } click to toggle source
Calls superclass method
# File lib/talktome/message.rb, line 26
def dup
  d = super
  yield(d) if block_given?
  d
end
extension() click to toggle source
# File lib/talktome/message.rb, line 32
def extension
  path.ext.to_s.gsub(/^\./,"")
end
instantiate(tpldata) click to toggle source
# File lib/talktome/message.rb, line 15
def instantiate(tpldata)
  self.dup do |m|
    m.metadata = {}
    self.metadata.each_pair do |k, v|
      m.metadata[k] = i(v, tpldata)
    end
    m.template_content = i(m.template_content, tpldata.merge(metadata: m.metadata))
    m.data = tpldata
  end
end
to_html() click to toggle source
# File lib/talktome/message.rb, line 40
def to_html
  case extension
  when 'md'
    template_it(Talktome.redcarpet.render(self.template_content), :html)
  else
    template_it(self.template_content, :html)
  end
end
to_text() click to toggle source
# File lib/talktome/message.rb, line 36
def to_text
  template_it(self.template_content, :text)
end

Private Instance Methods

compile() click to toggle source
# File lib/talktome/message.rb, line 79
def compile
  raw = path.read.force_encoding(Encoding::UTF_8)
  if raw =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    @metadata, @template_content = YAML::load($1), $'
  else
    @metadata, @template_content = {}, raw
  end
end
i(tpl, tpldata) click to toggle source
# File lib/talktome/message.rb, line 72
def i(tpl, tpldata)
  Template.new({
    template_file: self.path,
    template_path: self.path.parent
  }).render(tpl, tpldata)
end
template_it(src, ctype) click to toggle source
# File lib/talktome/message.rb, line 64
def template_it(src, ctype)
  if @options[:templater]
    @options[:templater].call(self, src, ctype)
  else
    src
  end
end