Sha256: 86e62284ee65df608602ff2f71390239c815b27a238124a93563c6fc2b77973a

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

module Assette
  
  class TemplateSet
    attr_reader :templates
    
    def initialize *location
      if location.size == 1
        location = location.pop
      end
      
      if location.is_a?(Array)
        location = 'all' if location.include?(:all) || location.include?('all')
      else
        location = location.to_s
      end
      
      path = Assette.config.templates_path
      
      if location == 'all'
        dirs = [File.join(path,'*')]
      elsif location.is_a?(Array)
        dirs = location.collect do |l|
          File.join(path,l)
        end
      else
        dirs = [File.join(path,location)]
      end
      
      @paths = []
      dirs.each do |dir|
        @paths += Dir[File.join(dir,'*')]
      end
      
      @templates = @paths.collect do |p|
        Template.open(p)
      end
    end
    
    def insert_preloader
      p = Assette.config.template_preloader
      if p && pp = Assette.config.find_file_from_relative_path(p)
        Assette::File.open(pp).all_code
      end
    end
    
    def compile
      coffee = Array.new
      
      vars = storage_variable
      
      used = []
      vars.each do |var|
        used << var
        
        if used.size == 1
          str = "window[#{var.to_json}] ||= {}"
        else
          str = used.join('.')
          str << " ||= {}"
        end
        
        coffee << str
      end
      
      templates.each do |template|
        coffee << template.compile
      end
      
      t = Assette::Reader::Coffee.compile_str(coffee.join("\n"))
      pre = insert_preloader
      pre ? [pre,t].join("\n") : t
    end
    
    def storage_variable
      format = Assette.config.template_format
      if m = format.match(/^([\w\.]+)\[/)
        vars = m[1].split('.')
      else
        []
      end
    end
    
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
assette-0.0.5 lib/assette/template_set.rb
assette-0.0.4 lib/assette/template_set.rb
assette-0.0.3 lib/assette/template_set.rb
assette-0.0.2 lib/assette/template_set.rb
assette-0.0.1 lib/assette/template_set.rb