Class: Jax::Packager::SprocketsTemplate

Inherits:
Sprockets::SourceFile
  • Object
show all
Defined in:
lib/jax/packager/sprockets_template.rb

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ SprocketsTemplate

Returns a new instance of SprocketsTemplate.



2
3
4
5
# File 'lib/jax/packager/sprockets_template.rb', line 2

def initialize(environment)
  pathname = environment.pathname_from(File.join(Jax.root, "template.js"))
  super(environment, pathname)
end

Instance Method Details

#add_file(template, jsfi) ⇒ Object



29
30
31
32
33
# File 'lib/jax/packager/sprockets_template.rb', line 29

def add_file(template, jsfi)
  relative_path = jsfi.sub(/^#{Regexp::escape Jax.root.to_s}[\/\\]?/, '')
  template << "//= require <#{relative_path}>"
  added_files << jsfi
end

#added_filesObject



39
40
41
# File 'lib/jax/packager/sprockets_template.rb', line 39

def added_files
  @added_files ||= []
end

#already_added?(jsfi) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/jax/packager/sprockets_template.rb', line 35

def already_added?(jsfi)
  added_files.include?(jsfi)
end

#asset_pathsObject



43
44
45
46
47
48
49
50
51
# File 'lib/jax/packager/sprockets_template.rb', line 43

def asset_paths
  Jax.application.asset_paths.collect do |path|
    if File.directory?(path)
      "//= provide \"#{File.join(path, "").gsub(/^#{Regexp::escape Jax.root.to_s}\/?/, '')}\""
    end
  end.reject do |result|
    result.nil?
  end
end

#source_linesObject



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

def source_lines
  # basically the same as super but with a string instead of a file
  # TODO would templating be a worthy addition to Sprockets itself?
  @lines ||= begin
    lines = []
    comments = []
    
    template.each_with_index do |line, lineno|
      lines << line = Sprockets::SourceLine.new(self, line, lineno+1)
      if line.begins_pdoc_comment? || comments.any?
        comments << line
      end
      
      if line.ends_multiline_comment?
        if line.ends_pdoc_comment?
          comments.each { |l| l.comment! }
        end
        comments.clear
      end
    end

    lines
  end
end

#templateObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jax/packager/sprockets_template.rb', line 7

def template
  added_files.clear
  @template ||= begin
    template = [
      'Jax.environment = Jax.PRODUCTION;',
      '',
      asset_paths,
      ''
    ].flatten
    
    Jax.application.javascript_sources.each { |jsfi| try_to_add_file(template, jsfi) }

    template
  end
end

#try_to_add_file(template, jsfi) ⇒ Object



23
24
25
26
27
# File 'lib/jax/packager/sprockets_template.rb', line 23

def try_to_add_file(template, jsfi)
  if File.file?(jsfi) && !already_added?(jsfi)
    add_file(template, jsfi)
  end
end