class DryHamlHandlebars::Runner

Public Class Methods

new(template, rendered_haml = nil, view_type, view_name, partial_name, relative_view_path, rabl_path, template_path, compiled_template_path) click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 184
def initialize(template, rendered_haml = nil, view_type, view_name, partial_name, relative_view_path, rabl_path, template_path, compiled_template_path)
  @template                 = template
  @rendered_haml            = rendered_haml
  @view_type                = view_type
  @view_name                = view_name
  @partial_name             = partial_name
  @relative_view_path       = relative_view_path
  @rabl_path                = rabl_path
  @template_path            = template_path
  @compiled_template_path   = compiled_template_path
end

Public Instance Methods

compile_hbs() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 271
    def compile_hbs
      <<-RUBY
        compiled_hbs = HandlebarsAssets::Handlebars.precompile( rendered_haml )
      RUBY
    end
gen_partial_loader() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 300
    def gen_partial_loader
      <<-'RUBY'
        hbs_loader = "(function() {
          this.Handlebars.registerPartial('#{partial_name}', Handlebars.template(#{compiled_hbs}));
        }).call(this)"
      RUBY
    end
gen_template_loader() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 290
    def gen_template_loader
      <<-'RUBY'
        hbs_loader = "(function() {
          this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
          this.HandlebarsTemplates['#{template_names.last}'] = Handlebars.template(#{compiled_hbs});
          return HandlebarsTemplates['#{template_names.last}'];
        }).call(this)"
      RUBY
    end
get_hbs_context() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 315
    def get_hbs_context
      <<-RUBY
        hbs_context = HandlebarsAssets::Handlebars.send(:context)
      RUBY
    end
load_template() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 327
    def load_template
      <<-RUBY
        File.open('#{@compiled_template_path}') do |file|
          hbs_context.eval(file.read, '#{@view_name}.js')
        end
      RUBY
    end
name_partial() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 284
    def name_partial
      <<-RUBY
        partial_name  = '#{@partial_name}'
      RUBY
    end
name_template() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 277
    def name_template
      <<-RUBY
        template_names ||= []
        template_names << '#{File.join(@relative_view_path, @view_name).to_s}'
      RUBY
    end
render_content_for(name, path) click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 380
    def render_content_for(name, path)
      
      haml_handler  = ActionView::Template.handler_for_extension :haml
      haml_source   = File.read(path)
      haml_template = ActionView::Template.new(haml_source, path, haml_handler, {})
      haml_call     = haml_handler.call haml_template

      <<-RUBY
        @view_flow.set( :#{name}, eval(%q( #{haml_call} )).html_safe )
      RUBY
      
    end
render_handlebars_partial_command() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 374
    def render_handlebars_partial_command
      <<-RUBY
        '{{> #{@partial_name}}}'.html_safe
      RUBY
    end
render_rabl() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 335
    def render_rabl
      
      if File.exist? @rabl_path
    
        rabl_handler  = ActionView::Template.handler_for_extension :rabl
        rabl_source   = File.read(@rabl_path)
        rabl_template = ActionView::Template.new(rabl_source, @rabl_path, rabl_handler, {:locals => @template.locals})
        rabl_call     = rabl_handler.call rabl_template

        <<-RUBY
          rabl_call_str = %q( #{rabl_call} )
          rendered_rabl = eval(rabl_call_str).html_safe
        RUBY
        
      else
        
        <<-RUBY
          rendered_rabl ||= '{}'.html_safe
        RUBY
        
      end
      
    end
render_template() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 367
    def render_template
      <<-'RUBY'
        current_template_name = template_names.pop
        hbs_context.eval( "HandlebarsTemplates['#{current_template_name}'](#{rendered_rabl})" )
      RUBY
    end
run() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 196
def run
  
  content_cache = DryHamlHandlebars.content_cache
  out = []

  if @rendered_haml
  
    out << @rendered_haml
    out << compile_hbs
    
    case @view_type
    when :template
      
      out << name_template
      out << gen_template_loader
      
    when :partial
      
      out << name_partial
      out << gen_partial_loader
      
    end

    out << write_asset_files
    out << get_hbs_context
    out << load_template
              
  else #if we don't have any rendered haml (we're probably in production)
    
    out << get_hbs_context
    out << name_template
    
  end

  out << set_locale if defined? SimplesIdeias::I18n

  case @view_type
  when :template
    
    out << render_rabl
    out << set_gon_variable
    
    if content_cache.store.present? #do we need to render some content for another view?

      content_cache.store.each do |item|
        
        name = item.name
        path = item.path
        content_cache.remove_item(item)
        
        #NOTE: this call will overwrite all eval'd variables set below, except for template_names
        #we store this in a stack and pop the last name when we get to render_template()
        #it doesn't matter about the other variables as we're finished with them by this stage
        #and it keeps the eval code simpler if we just reuse them
        
        out << render_content_for(name, path)
        
      end
      content_cache.clear #just to be sure
      
    end
    
    out << render_template
    
  when :partial
    
    out << render_handlebars_partial_command
    
  end
  
  out.join("\n")


end
set_gon_variable() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 359
    def set_gon_variable
      <<-'RUBY'
        Gon::Request.id  = request.object_id
        Gon::Request.env = request.env
        Gon.view_data  ||= JSON.parse(rendered_rabl)
      RUBY
    end
set_locale() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 321
    def set_locale
      <<-RUBY
        hbs_context.eval("I18n.locale = '#{I18n.locale.to_s}'")
      RUBY
    end
write_asset_files() click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 308
    def write_asset_files
      <<-RUBY
        File.open('#{@template_path}',          'w+') {|f| f.write(rendered_haml) }
        File.open('#{@compiled_template_path}', 'w+') {|f| f.write(hbs_loader) }
      RUBY
    end