Class: AMDTemplateEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets-amd/template_engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(code, options = {}) ⇒ AMDTemplateEngine

Returns a new instance of AMDTemplateEngine.



3
4
5
6
7
8
# File 'lib/sprockets-amd/template_engine.rb', line 3

def initialize(code, options = {})
  @prefixes = options[:prefixes]
  @code = code
  @require_statement = options[:require_statement] || "AMD\.require"
  @module_statement = options[:module_statement] || "AMD\.module"
end

Instance Method Details

#combined_prefixesObject



56
57
58
59
60
61
62
# File 'lib/sprockets-amd/template_engine.rb', line 56

def combined_prefixes
  syntax = [@require_statement, @module_statement]
  prefs = get_registered_prefixes.inject([]) do |sum, pref|
    sum + syntax.map { |el| "#{el}.#{pref}" }
  end
  prefs + syntax
end

#dependencies_without_prefixObject



44
45
46
# File 'lib/sprockets-amd/template_engine.rb', line 44

def dependencies_without_prefix
  without_prefix extract_dependencies
end

#extract_dependenciesObject



15
16
17
18
# File 'lib/sprockets-amd/template_engine.rb', line 15

def extract_dependencies
  regexp = /#{@require_statement}\.([\w_\.-]+)/
  @deps ||= with_prefix @code.scan(regexp).flatten.uniq
end

#extract_module_nameObject



10
11
12
13
# File 'lib/sprockets-amd/template_engine.rb', line 10

def extract_module_name
  regexp = /#{@module_statement}\.([\w_\.-]+)/
  @moodule_name ||= with_prefix(@code.match(regexp)[1]).first
end

#get_quote_dependenciesObject



20
21
22
# File 'lib/sprockets-amd/template_engine.rb', line 20

def get_quote_dependencies
  extract_dependencies.map { |el| "'#{el}'" }
end

#get_registered_prefixesObject



36
37
38
# File 'lib/sprockets-amd/template_engine.rb', line 36

def get_registered_prefixes
  @prefixes || []
end

#module_name_without_prefixObject



40
41
42
# File 'lib/sprockets-amd/template_engine.rb', line 40

def module_name_without_prefix
  without_prefix [extract_module_name]
end

#renderObject



73
74
75
# File 'lib/sprockets-amd/template_engine.rb', line 73

def render
  "define('#{extract_module_name}', [#{get_quote_dependencies.join(', ')}], function(#{dependencies_without_prefix.join(", ")}) {\n#{strip_code}});"
end

#strip_codeObject



64
65
66
67
68
69
70
71
# File 'lib/sprockets-amd/template_engine.rb', line 64

def strip_code
  @code = "var #{module_name_without_prefix[0]} = {};\n#{@code}"
  combined_prefixes.each do |pref|
    @code = @code.gsub "#{pref}.", ''
  end
  @code += "\nreturn #{module_name_without_prefix[0]};"
  @code
end

#with_prefix(matches) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sprockets-amd/template_engine.rb', line 24

def with_prefix(matches)
  matches = [matches] unless matches.is_a?(Array)
  matches.map do |el| 
    arr = el.split(".") 
    if get_registered_prefixes.include?(arr[0])
      arr.slice(0, 2).join(".")
    else
      arr.slice(0)
    end
  end
end

#without_prefix(thing) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/sprockets-amd/template_engine.rb', line 48

def without_prefix(thing)
  prefixes = get_registered_prefixes
  thing.map do |el|
    prefixes.each { |pr| el = el.gsub("#{pr}.", '') }
    el
  end
end