jsrequire 0.0.5 → 0.1.0

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.
data/README.mkd ADDED
@@ -0,0 +1,74 @@
1
+ jsrequire
2
+ =========
3
+
4
+
5
+ A small lib to add a require functionality to JavaScript files.
6
+
7
+
8
+ #### Features
9
+
10
+ * Dependencies are solved from one or multiple files
11
+ * Multiple loadpaths for js file lookup
12
+ * Simple assets collector for stylesheets
13
+ * Support for namespaces like ExtJS requires it
14
+
15
+
16
+ A JavaScript file
17
+ -----------------
18
+
19
+ On top of a file add something like:
20
+
21
+ /* require foo/bar */
22
+ /* css baz */
23
+
24
+ This requires the js file foo/bar.js and the stylesheet baz.css. Dependencies are resolved in right order of require statments over all files.
25
+
26
+
27
+ Controller
28
+ ----------
29
+
30
+ #### resolve js dependencies
31
+
32
+ path_to_js = "/absolute/path/to/javascripts"
33
+
34
+ jsrequire = JsRequire.new
35
+ dependencies = jsrequire.resolve_dependencies(File.join(path_to_js, "foo.js"))
36
+
37
+ #### rewrite js paths for web usage
38
+
39
+ @javascripts = JsRequire::web_path_helper(dependencies[:javascripts],
40
+ path_to_js => "/public/javascripts"
41
+ })
42
+
43
+ @stylesheets = dependencies[:stylesheets]
44
+
45
+
46
+ A view file
47
+ -----------
48
+
49
+ e.g. in a Haml view template
50
+
51
+ - for css in @stylesheets
52
+ %link{:rel => "stylesheet", :href => css, :type => "text/css"}
53
+
54
+ - for js in @javascripts
55
+ %script{:type => "text/javascript", :src => js}
56
+
57
+
58
+ Note on Patches/Pull Requests
59
+ -----------------------------
60
+
61
+ * Fork the project.
62
+ * Make your feature addition or bug fix.
63
+ * Add tests for it. This is important so I don't break it in a
64
+ future version unintentionally.
65
+ * Commit, do not mess with rakefile, version, or history.
66
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
67
+ * Send me a pull request. Bonus points for topic branches.
68
+
69
+
70
+ Copyright
71
+ ---------
72
+
73
+ Copyright (c) 2010 aekym. See LICENSE for details.
74
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
data/jsrequire.gemspec CHANGED
@@ -5,22 +5,22 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jsrequire}
8
- s.version = "0.0.5"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["aekym"]
12
- s.date = %q{2010-02-05}
12
+ s.date = %q{2010-05-07}
13
13
  s.description = %q{Organizes requirements of assets in JavaScript files, resolved dependencies of js files and helps include depending css files.}
14
14
  s.email = %q{me@aekym.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.mkd"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
22
  "LICENSE",
23
- "README.rdoc",
23
+ "README.mkd",
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "jsrequire.gemspec",
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "test/fixtures/javascripts/a.js",
30
30
  "test/fixtures/javascripts/c.js",
31
31
  "test/fixtures/javascripts/file.with.dot.js",
32
+ "test/fixtures/javascripts/hook.js",
32
33
  "test/fixtures/javascripts/namespace/a.js",
33
34
  "test/fixtures/javascripts/namespace/json_reader.js",
34
35
  "test/fixtures/javascripts/namespace/norequire.js",
@@ -41,15 +42,17 @@ Gem::Specification.new do |s|
41
42
  "test/fixtures/javascripts/requirecss.js",
42
43
  "test/fixtures/stylesheets/style.css",
43
44
  "test/helper.rb",
45
+ "test/test_hooks.rb",
44
46
  "test/test_jsrequire.rb"
45
47
  ]
46
48
  s.homepage = %q{http://github.com/aekym/jsrequire}
47
49
  s.rdoc_options = ["--charset=UTF-8"]
48
50
  s.require_paths = ["lib"]
49
- s.rubygems_version = %q{1.3.5}
51
+ s.rubygems_version = %q{1.3.6}
50
52
  s.summary = %q{Organizes requirements of assets in JavaScript files}
51
53
  s.test_files = [
52
54
  "test/helper.rb",
55
+ "test/test_hooks.rb",
53
56
  "test/test_jsrequire.rb"
54
57
  ]
55
58
 
data/lib/jsrequire.rb CHANGED
@@ -11,6 +11,13 @@ class JsRequire
11
11
 
12
12
  loadpaths = [loadpaths] unless loadpaths.is_a?(Array)
13
13
  @additional_loadpaths = JsRequire::normalize_filepaths(loadpaths.compact)
14
+
15
+ @preprocessors = Hash.new { |h,k| h[k] = [] }
16
+ end
17
+
18
+
19
+ def on(action = nil, &block)
20
+ @preprocessors[action] << block
14
21
  end
15
22
 
16
23
 
@@ -135,6 +142,17 @@ class JsRequire
135
142
  raise FileNotFoundInLoadpath, "File '#{filename}' not found in loadpaths '#{loadpaths.join("', '")}'."
136
143
  end
137
144
 
145
+ def exec_preprocessor(action, parameter)
146
+ trigger = Proc.new do |cb|
147
+ res = cb.call(action, parameter)
148
+ action, parameter = res if res.is_a?(Array) && res.size == 2
149
+ end
150
+
151
+ @preprocessors[action].each(&trigger)
152
+ @preprocessors[nil].each(&trigger)
153
+
154
+ [action, parameter]
155
+ end
138
156
 
139
157
  def extract_dependencies(filename)
140
158
  is_require = true
@@ -142,11 +160,19 @@ class JsRequire
142
160
 
143
161
  File.open(filename, "r") do |f|
144
162
  begin
145
- case line = f.gets
146
- when /^\s*\/\*\s*require\s*(\S+)\s*\*\/\s*$/
147
- js << "#{$1}.js"
148
- when /^\s*\/\*\s*css\s*(\S+)\s*\*\/\s*$/
149
- @stylesheets[filename] << $1 + ".css"
163
+ line = f.gets
164
+ if line =~ /^\s*\/\*\s*(\w+)(.+)\*\/\s*$/
165
+ action = $1.strip
166
+ parameter = $2.strip
167
+
168
+ # fire callbacks
169
+ #
170
+ action, parameter = exec_preprocessor(action, parameter)
171
+
172
+ case action
173
+ when "require" then js << "#{parameter}.js"
174
+ when "css" then @stylesheets[filename] << parameter + ".css"
175
+ end
150
176
  else
151
177
  is_require = false
152
178
  end
@@ -0,0 +1,2 @@
1
+ /* view hello world */
2
+
data/test/helper.rb CHANGED
@@ -5,6 +5,12 @@ require 'shoulda'
5
5
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
7
 
8
+ begin
9
+ require 'redgreen'
10
+ rescue Exception
11
+ end
12
+
13
+
8
14
  require 'jsrequire'
9
15
 
10
16
  class Test::Unit::TestCase
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/helper.rb'
2
+
3
+ class TestHooks < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @fixtures_dir = File.join(File.dirname(__FILE__), "fixtures")
7
+ end
8
+
9
+ def require(file, &block)
10
+ @jsrequire = JsRequire.new
11
+
12
+ yield
13
+
14
+ data = @jsrequire.resolve_dependencies(File.join(@fixtures_dir + "/javascripts", file))
15
+ end
16
+
17
+
18
+ context "hooks" do
19
+
20
+ should "register hook successfully" do
21
+ @jsrequire = JsRequire.new
22
+ @jsrequire.on { |action, parameter| }
23
+ end
24
+
25
+ should "execute general hook" do
26
+ called = false
27
+
28
+ require("hook.js") do
29
+ @jsrequire.on do |action, parameter|
30
+ assert_equal "view", action
31
+ assert_equal "hello world", parameter
32
+ called = true
33
+ end
34
+ end
35
+
36
+ assert called, "Callback was not called"
37
+ end
38
+
39
+ should "execute specific hook" do
40
+ called = false
41
+ require("hook.js") do
42
+ @jsrequire.on("view") do |action, parameter|
43
+ assert_equal "view", action
44
+ assert_equal "hello world", parameter
45
+ called = true
46
+ end
47
+ end
48
+
49
+ assert called, "Callback was not called"
50
+ end
51
+
52
+ should "call specific hook before general" do
53
+ specific_called = false
54
+ general_called = false
55
+
56
+ require("hook.js") do
57
+ @jsrequire.on do |action, parameter|
58
+ assert specific_called, "Specific was not called before"
59
+ general_called = true
60
+ end
61
+ @jsrequire.on("view") do |action, parameter|
62
+ assert !general_called, "General was called before"
63
+ specific_called = true
64
+ end
65
+ end
66
+
67
+ assert general_called && specific_called, "Callbacks were not called"
68
+ end
69
+
70
+ should "rewrite line to require" do
71
+ called = false
72
+ deps = require("hook.js") do
73
+ @jsrequire.on("view") do |action, params|
74
+ called = true
75
+ ["require", "norequire"]
76
+ end
77
+ end
78
+
79
+ assert called, "Callback not fired"
80
+ assert_equal 2, deps[:javascripts].size
81
+ assert_match /norequire\.js$/, deps[:javascripts].first
82
+ end
83
+
84
+ should "rewrite line to require" do
85
+ called = false
86
+ deps = require("hook.js") do
87
+ @jsrequire.on("view") do |action, params|
88
+ called = true
89
+ ["require", "norequire"]
90
+ end
91
+ end
92
+
93
+ assert called, "Callback not fired"
94
+ assert_equal 2, deps[:javascripts].size
95
+ assert_match /norequire\.js$/, deps[:javascripts].first
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsrequire
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - aekym
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-05 00:00:00 +01:00
17
+ date: 2010-05-07 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -21,12 +26,12 @@ extensions: []
21
26
 
22
27
  extra_rdoc_files:
23
28
  - LICENSE
24
- - README.rdoc
29
+ - README.mkd
25
30
  files:
26
31
  - .document
27
32
  - .gitignore
28
33
  - LICENSE
29
- - README.rdoc
34
+ - README.mkd
30
35
  - Rakefile
31
36
  - VERSION
32
37
  - jsrequire.gemspec
@@ -35,6 +40,7 @@ files:
35
40
  - test/fixtures/javascripts/a.js
36
41
  - test/fixtures/javascripts/c.js
37
42
  - test/fixtures/javascripts/file.with.dot.js
43
+ - test/fixtures/javascripts/hook.js
38
44
  - test/fixtures/javascripts/namespace/a.js
39
45
  - test/fixtures/javascripts/namespace/json_reader.js
40
46
  - test/fixtures/javascripts/namespace/norequire.js
@@ -47,6 +53,7 @@ files:
47
53
  - test/fixtures/javascripts/requirecss.js
48
54
  - test/fixtures/stylesheets/style.css
49
55
  - test/helper.rb
56
+ - test/test_hooks.rb
50
57
  - test/test_jsrequire.rb
51
58
  has_rdoc: true
52
59
  homepage: http://github.com/aekym/jsrequire
@@ -61,21 +68,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
68
  requirements:
62
69
  - - ">="
63
70
  - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
64
73
  version: "0"
65
- version:
66
74
  required_rubygems_version: !ruby/object:Gem::Requirement
67
75
  requirements:
68
76
  - - ">="
69
77
  - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
70
80
  version: "0"
71
- version:
72
81
  requirements: []
73
82
 
74
83
  rubyforge_project:
75
- rubygems_version: 1.3.5
84
+ rubygems_version: 1.3.6
76
85
  signing_key:
77
86
  specification_version: 3
78
87
  summary: Organizes requirements of assets in JavaScript files
79
88
  test_files:
80
89
  - test/helper.rb
90
+ - test/test_hooks.rb
81
91
  - test/test_jsrequire.rb
data/README.rdoc DELETED
@@ -1,50 +0,0 @@
1
- = jsrequire
2
-
3
- A small lib to add a require functionality to JavaScript files. On top of a file add something like:
4
-
5
- /* require foo/bar */
6
-
7
- This includes the file foo/bar.js into the file. Dependencies are resolved in right order of require statments over all files.
8
-
9
- ## controller
10
-
11
- # resolve js dependencies
12
-
13
- path_to_js = "/absolute/path/to/javascripts"
14
-
15
- jsrequire = JsRequire.new
16
- dependencies = jsrequire.resolve_dependencies(File.join(path_to_js, "foo.js"))
17
-
18
-
19
- # rewrite js paths for web usage
20
-
21
- @javascripts = JsRequire::web_path_helper(dependencies[:javascripts],
22
- path_to_js => "/public/javascripts"
23
- })
24
-
25
- @stylesheets = dependencies[:stylesheets]
26
-
27
-
28
- ## haml view
29
-
30
- - for css in @stylesheets
31
- %link{:rel => "stylesheet", :href => css, :type => "text/css"}
32
-
33
- - for js in @javascripts
34
- %script{:type => "text/javascript", :src => js}
35
-
36
-
37
- == Note on Patches/Pull Requests
38
-
39
- * Fork the project.
40
- * Make your feature addition or bug fix.
41
- * Add tests for it. This is important so I don't break it in a
42
- future version unintentionally.
43
- * Commit, do not mess with rakefile, version, or history.
44
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
45
- * Send me a pull request. Bonus points for topic branches.
46
-
47
- == Copyright
48
-
49
- Copyright (c) 2010 aekym. See LICENSE for details.
50
-