Class: Assette::TemplateSet

Inherits:
Object
  • Object
show all
Defined in:
lib/assette/template_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*location) ⇒ TemplateSet

Returns a new instance of TemplateSet.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/assette/template_set.rb', line 18

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

Instance Attribute Details

#templatesObject (readonly)

Returns the value of attribute templates.



16
17
18
# File 'lib/assette/template_set.rb', line 16

def templates
  @templates
end

Instance Method Details

#compile(opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/assette/template_set.rb', line 51

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

#storage_variableObject



79
80
81
82
83
84
85
86
# File 'lib/assette/template_set.rb', line 79

def storage_variable
  format = Assette.config.template_format
  if m = format.match(/^([\w\.]+)\[/)
    vars = m[1].split('.')
  else
    []
  end
end