Sha256: a25a21b3ef51af95d5bfcf990fc0da620816716f9bafd87c027924d370081393

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

class Stormy::Template

  attr_reader :details
  attr_accessor :content

  def initialize(app,key,details)
    @app = app
    @details = details
    @key = key
    @body = details["body"]

    @engine = resolve_engine(self.class.extract_extension(@key))
  end

  def render(&block)
    if @engine
      @engine.render(@body,resolve_bindings,&block)
    else
      nil
    end
  end

  protected

  @@engines = {
    "md" => "Markdown",
    "haml" => "Haml",
    "txt"  => "Text",
    "html" => "Html",
    "erb" => "Erb",
    "scss" => "Sass"
  }

  @@supported_extensions = @@engines.keys

  def self.rendered_extension?(extension)
    @@supported_extensions.include?(extension)
  end
  
  def self.extract_segment(path)
    extension = extract_extension(path)
    if rendered_extension?(extension)
      File.basename(path,"." + extension)
    else 
      File.basename(path)
    end
  end

  def self.extract_extension(key)
    return "txt" unless key.include?(".")
    key.split(".")[-1].to_s.downcase
  end

  def resolve_engine(extension)
    engine = @@engines[extension]
    if !engine.nil?
      "Stormy::Engines::#{engine}".constantize.new(@app)
    end
  end

  def resolve_bindings
    { 
      meta: @details
    }.merge(@content || {})
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stormy-0.0.1 lib/stormy/template.rb