Sha256: 0be701ea6276f66399b8d236996b1eeca5dd3de3c5976f6df93c3d2ac07e0b33

Contents?: true

Size: 1.88 KB

Versions: 17

Compression:

Stored size: 1.88 KB

Contents

module Assette
  #shitty
  def preloader_file
    p = Assette.config.template_preloader
    pp = Assette.config.find_file_from_relative_path(p) if p
    Assette::File.open(pp) if pp
  end
  
  #shitty
  def preloader_code
    pp = preloader_file
    pp.all_code if pp
  end
  
  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 compile(opts={})
      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 = Assette.preloader_code unless opts[:no_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

17 entries across 17 versions & 1 rubygems

Version Path
assette-0.1.8 lib/assette/template_set.rb
assette-0.1.7 lib/assette/template_set.rb
assette-0.1.6 lib/assette/template_set.rb
assette-0.1.5 lib/assette/template_set.rb
assette-0.1.4 lib/assette/template_set.rb
assette-0.1.3 lib/assette/template_set.rb
assette-0.1.2 lib/assette/template_set.rb
assette-0.1.1 lib/assette/template_set.rb
assette-0.1.0 lib/assette/template_set.rb
assette-0.0.13 lib/assette/template_set.rb
assette-0.0.12 lib/assette/template_set.rb
assette-0.0.11 lib/assette/template_set.rb
assette-0.0.10 lib/assette/template_set.rb
assette-0.0.9 lib/assette/template_set.rb
assette-0.0.8 lib/assette/template_set.rb
assette-0.0.7 lib/assette/template_set.rb
assette-0.0.6 lib/assette/template_set.rb