Sha256: 73dd40f30330aa8bcca273289f83dcce71e0ccfbfdb9f47ce7238820cac654b5

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

class Servel::HamlContext
  extend Servel::Instrumentation
  include ActiveSupport::NumberHelper

  LOCK = Mutex.new

  def self.render(template, locals)
    [200, {}, [new.render(template, locals)]]
  end

  def initialize
    @build_path = Pathname.new(__FILE__).dirname.realpath + 'templates'
  end

  def render(template, locals = {})
    haml_engine(template).render(self, locals)
  end

  def partial(name, locals = {})
    render("_#{name}.haml", locals)
  end

  def include(path)
    (@build_path + path).read
  end

  def sort_attrs(sort, current_method)
    data = { sort_method: current_method, sort_direction: "asc" }
    classes = ["sortable"]
    if sort[:method] == current_method
      data[:sort_active] = true
      data[:sort_direction] = sort[:direction]
      classes << "sort-active"
      classes << "sort-#{sort[:direction]}"
    end

    {
      data: data,
      class: classes
    }
  end

  def haml_engine(path)
    LOCK.synchronize do
      @@haml_engine_cache ||= {}
      unless @@haml_engine_cache.key?(path)
        @@haml_engine_cache[path] = Hamlit::Template.new(filename: path) { include(path) }
      end
      @@haml_engine_cache[path]
    end
  end

  instrument :render, :partial, :include
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
servel-0.19.0 lib/servel/haml_context.rb
servel-0.18.0 lib/servel/haml_context.rb
servel-0.17.0 lib/servel/haml_context.rb
servel-0.16.0 lib/servel/haml_context.rb
servel-0.15.0 lib/servel/haml_context.rb
servel-0.13.0 lib/servel/haml_context.rb
servel-0.12.0 lib/servel/haml_context.rb