sprockets-amd 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTE3NzBjNWQ2YTFlOGM1OTcwNzJjOTU0ZGVmZTJkMjg5OGFkYWUzNg==
4
+ YjUwZGFiYjJiMDA4N2ZhMDU5OWIxOGRkOTk0ZWRlZTRhMWZjNTA0OA==
5
5
  data.tar.gz: !binary |-
6
- NjIxYmVjMzViMGY2ZTc2MmRmOWI0ZTRmNWVkMjMzYzM3YjliMzc1Mg==
6
+ MjgwMDE4YTUzMDdjZjhhN2I2MTRjY2Q3NTMwZDM0YzNlMDgyYjBiMw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OWE5MjFkOGJkZTBlNjc3YmM3NTEyN2FkNzBkMDUxMGZhNTQ5NTM5MWQ3NjNi
10
- ZjhmZDA5ZmJhZThhYjc5MjAyMzViOTBiYzFlZTg1MzJiMTE4ODhmOTFkNjBj
11
- ODBiY2QxYzE2YzI4YjFkOTA0YjhjNDVhZjRlMmRiY2QwN2YzZTc=
9
+ ZTgzN2MwMzNmN2VhOGQ4MmE5MmY2ZWZkZjhmNTc4ODM2MTYzMGNmMTJlMzk2
10
+ YzhmODY2NDFiYmYxYmJmZGFiYjhjNzE2ZDNkMzFhZmJkNTFiZjRiMGMxN2Nj
11
+ MjI2OTY1ZmQ5NGU2ZjUyZjE2NjE2ZDIzYmUyMjIyYjViODg4OWU=
12
12
  data.tar.gz: !binary |-
13
- OTY1YjJhNzU5ZTEwNGU3YTU2NzljNDhlOWM0OTJkZDk2MWM2ZWU1NmY0Nzk0
14
- M2MxZTMxZjE5MDEwMTIxMmViYTczNTk2MGM5NGViOTRjOGFlNmJjYTYwMTc4
15
- MWRlZTg0NjgzZTlmYmM3ZjRmYTI4OWRiMjMyOTc0NjFlZmM0N2U=
13
+ NmEwY2YwNDQwMjA2Zjc2YWJiNzIyNDdmODExZDNmZTYyZmU1MWY5YzI5NGUw
14
+ NmY3ZjM4YmEyZjkzNzEzYjllYzBlMzNjN2NhMDk2MzE4NDE4MmQ2M2QxMmNi
15
+ YmQzNzM5M2QyNTU4YTE2YmFiMThmZjNjZjU2Yjk2Y2QxMmE3ODg=
@@ -0,0 +1,31 @@
1
+ require 'tilt'
2
+
3
+ module SprocketsAmd
4
+
5
+ class AMDTemplate < Tilt::Template
6
+ class << self;
7
+ attr_accessor :config
8
+ end
9
+
10
+ self.config = {}
11
+
12
+ self.default_mime_type = 'application/javascript'
13
+
14
+
15
+ def self.engine_initialized?
16
+ defined? ::AMDTemplateEngine
17
+ end
18
+
19
+ def initialize_engine
20
+ require_template_library 'sprockets-amd/template_engine'
21
+ end
22
+
23
+ def prepare; end
24
+
25
+ def evaluate(scope, locals, &block)
26
+ @output ||= AMDTemplateEngine.new(data, self.class.config).render
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,77 @@
1
+ class AMDTemplateEngine
2
+
3
+ def initialize(code, options = {})
4
+ @prefixes = options[:prefixes]
5
+ @code = code
6
+ @require_statement = options[:require_statement] || "AMD\.require"
7
+ @module_statement = options[:module_statement] || "AMD\.module"
8
+ end
9
+
10
+ def extract_module_name
11
+ regexp = /#{@module_statement}\.([\w_\.-]+)/
12
+ @moodule_name ||= with_prefix(@code.match(regexp)[1]).first
13
+ end
14
+
15
+ def extract_dependencies
16
+ regexp = /#{@require_statement}\.([\w_\.-]+)/
17
+ @deps ||= with_prefix @code.scan(regexp).flatten.uniq
18
+ end
19
+
20
+ def get_quote_dependencies
21
+ extract_dependencies.map { |el| "'#{el}'" }
22
+ end
23
+
24
+ def with_prefix(matches)
25
+ matches = [matches] unless matches.is_a?(Array)
26
+ matches.map do |el|
27
+ arr = el.split(".")
28
+ if get_registered_prefixes.include?(arr[0])
29
+ arr.slice(0, 2).join(".")
30
+ else
31
+ arr.slice(0)
32
+ end
33
+ end
34
+ end
35
+
36
+ def get_registered_prefixes
37
+ @prefixes || []
38
+ end
39
+
40
+ def module_name_without_prefix
41
+ without_prefix [extract_module_name]
42
+ end
43
+
44
+ def dependencies_without_prefix
45
+ without_prefix extract_dependencies
46
+ end
47
+
48
+ def without_prefix(thing)
49
+ prefixes = get_registered_prefixes
50
+ thing.map do |el|
51
+ prefixes.each { |pr| el = el.gsub("#{pr}.", '') }
52
+ el
53
+ end
54
+ end
55
+
56
+ def combined_prefixes
57
+ syntax = [@require_statement, @module_statement]
58
+ prefs = get_registered_prefixes.inject([]) do |sum, pref|
59
+ sum + syntax.map { |el| "#{el}.#{pref}" }
60
+ end
61
+ prefs + syntax
62
+ end
63
+
64
+ def strip_code
65
+ @code = "var #{module_name_without_prefix[0]} = {};\n#{@code}"
66
+ combined_prefixes.each do |pref|
67
+ @code = @code.gsub "#{pref}.", ''
68
+ end
69
+ @code += "\nreturn #{module_name_without_prefix[0]};"
70
+ @code
71
+ end
72
+
73
+ def render
74
+ "define('#{extract_module_name}', [#{get_quote_dependencies.join(', ')}], function(#{dependencies_without_prefix.join(", ")}) {\n#{strip_code}});"
75
+ end
76
+
77
+ end
@@ -0,0 +1,3 @@
1
+ module SprocketsAmd
2
+ VERSION = "0.0.2"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-amd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vyacheslav Shebanov
@@ -31,6 +31,9 @@ extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
33
  - lib/sprockets-amd.rb
34
+ - lib/sprockets-amd/engine.rb
35
+ - lib/sprockets-amd/version.rb
36
+ - lib/sprockets-amd/template_engine.rb
34
37
  homepage: https://github.com/Termina1/sprockets-amd
35
38
  licenses: []
36
39
  metadata: {}