jsdm 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/jsdm.rb +106 -23
  3. metadata +4 -4
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ spec = Gem::Specification.new do |s|
16
16
  s.has_rdoc = true
17
17
  s.rubyforge_project = 'jsdm'
18
18
  s.homepage = 'http://www.github.com/retiman/jsdm'
19
- s.version = '0.4.3'
19
+ s.version = '0.4.4'
20
20
  s.summary = 'Javascript dependency manager'
21
21
  s.description = <<-EOF
22
22
  Use #require statements to declare dependencies in JavaScript and let JSDM
@@ -3,7 +3,7 @@ require 'jsdm/dependency_resolver'
3
3
  require 'jsdm/errors'
4
4
  require 'jsdm/preprocessor'
5
5
 
6
- # JSDM allows you to add #require statements to JavaScript and then manages
6
+ # JSDM allows you to add \#require statements to JavaScript and then manages
7
7
  # the dependencies for you. You may place the JavaScript files in many
8
8
  # different directories, and then add those directories to your load path.
9
9
  #
@@ -28,22 +28,82 @@ class JSDM
28
28
 
29
29
  # Options: randomize sort load_path extension comment_pattern require_pattern
30
30
  #
31
- # Examples:
31
+ # Creates an instance of a JSDM class which will let you retrieve dependency
32
+ # information about your source files. Use the specific instance methods to
33
+ # retrieve this information.
34
+ #
35
+ # You may give JSDM a set of <tt>load_path</tt>s to search; in which case,
36
+ # the first file in the load path that matches a require directive will be
37
+ # the file JSDM considers to be the dependency.
38
+ #
39
+ # For example, suppose your load path consists of /foo and /bar. Both
40
+ # directories contain baz.js. If bif.js declares baz.js as a dependency,
41
+ # then during the dependency resolution step, /foo/baz.js will be considered
42
+ # a dependency, and /bar/baz.js will not be.
43
+ #
44
+ # However, if you ask for <tt>jsdm.sources</tt>, all sources, including
45
+ # /bar/baz.js will be returned.
46
+ #
47
+ # JavaScript source files should place \#require directives inside comments
48
+ # at the top of the source file, so that JSDM can parse and process them.
49
+ # You may use globs in your \#require directives.
50
+ #
51
+ # Here is an example of what one of your JavaScript files might look like:
52
+ #
53
+ # // #require a.js
54
+ # // #require b.js
55
+ # // #require c/d.js
56
+ # // #require e/*.js
57
+ # // #require f/**/*.js
58
+ #
59
+ # It is advised to not use globs in your \#require directives if you
60
+ # use multiple load paths, as \#require *.js will automatically
61
+ # always resolve to all .js files in the first directory of your load path.
62
+ #
63
+ # For example:
64
+ #
65
+ # * Suppose directory /foo contains a.js, b.js, c.js
66
+ # * Suppose directory /bar contains d.js
67
+ # * Suppose a.js has a // \#require *.js comment
68
+ #
69
+ # Note that in the following example, /bar/d.js is not a dependency of a.js
70
+ #
71
+ # jsdm = JSDM.new :load_path => %w(/foo /bar)
72
+ # pp jsdm.sources_for '/foo/a.js' # ['/foo/a.js', '/foo/b.js', '/foo/c.js']
73
+ # pp jsdm.sources # ['/foo/a.js', '/foo/b.js', '/foo/c.js', '/bar/d.js']
32
74
  #
33
- # # Looks for both .js and .jsm extensions in the current directory
34
- # JSDM.new :extension => '{js,jsm}'
75
+ # Here are some more examples:
35
76
  #
36
- # # Looks for JavaScript files in two different directories
37
- # JSDM.new :load_path => %w(some/path some/other/path)
77
+ # # Implicitly finds JavaScript files in the current directory, and prints
78
+ # # them:
79
+ # jsdm = JSDM.new
80
+ # jsdm.sources.each { |source| puts source }
81
+ #
82
+ # # Finds both .js and .jsm files in the current directory, and prints
83
+ # # them:
84
+ # jsdm = JSDM.new :extension => '{js,jsm}'
85
+ # jsdm.sources.each { |source| puts source }
86
+ #
87
+ # # Finds JavaScript files in two different directories, and prints file
88
+ # # from both directories:
89
+ #
90
+ # jsdm = JSDM.new :load_path => %w(some/path some/other/path)
91
+ # jsdm.sources.each { |source| puts source }
38
92
  #
39
93
  # # Sorts the JavaScript files beforehand; ensures a unique ordering of
40
94
  # # dependencies, even if you've made a mistake in specifying the require
41
- # # statements
42
- # JSDM.new :sort => true
95
+ # # statements:
96
+ # jsdm = JSDM.new :sort => true
97
+ # jsdm.sources.each { |source| puts source }
43
98
  #
44
99
  # # Randomizes the JavaScript files beforehand; hopefully ensures that you
45
- # # will get an error from improperly specifying dependencies
46
- # JSDM.new :randomize => true
100
+ # # will get an error from improperly specifying dependencies:
101
+ # jsdm = JSDM.new :randomize => true
102
+ # jsdm.sources.each { |source| puts source }
103
+ #
104
+ # # Make the directive for requiring files @import instead of #require:
105
+ # JSDM.new :require_pattern => /^\s*\/\/\s*@import\s*/
106
+ # jsdm.sources.each { |source| puts source }
47
107
  def initialize(opts = {})
48
108
  defaults = {
49
109
  :randomize => false,
@@ -64,14 +124,20 @@ class JSDM
64
124
  process
65
125
  end
66
126
 
67
- # Preprocesses, resolves, and sorts the source files. Here's how it does it:
68
- #
69
- # * Gather all the sources in the load path
70
- # * Optionally sort them to ensure that dependencies have been set correctly
71
- # * Create the Preprocessor, DependencyResolver, and DependencyManager
72
- # * Preprocess the source files and resolve the dependencies
73
- # * Add each individual dependency to the DependencyManager
74
- # * Have the DependencyManager perform a topological sort
127
+ # Returns an array of all JavaScript files in your <tt>load_path</tt>, sorted
128
+ # so that dependent files are ordered first. This method is called by the
129
+ # initializer and the sorted array of JavaScript files will be stored in the
130
+ # <tt>sources</tt> variable for the user.
131
+ #
132
+ # If you are only interested in sources from a particular load path, or for
133
+ # a particular source file, see <tt>sources_for</tt> or
134
+ # <tt>dependencies</tt>.
135
+ #
136
+ # Examples:
137
+ #
138
+ # # Prints out sources from all load paths, in the correct order
139
+ # jsdm = JSDM.new :load_path => %w(path1 path2 path3)
140
+ # jsdm.sources.each { |source| puts source }
75
141
  def process
76
142
  self.sources = options[:load_path].map { |path|
77
143
  Dir[File.join(path, '**', "*.#{options[:extension]}")]
@@ -98,23 +164,40 @@ class JSDM
98
164
  sources
99
165
  end
100
166
 
101
- # Returns an associative list of sources mapped to source dependencies
167
+ # Returns an associative list of sources mapped to source dependencies. The
168
+ # order of source dependencies is not guaranteed to be anything meaningful.
169
+ # See <tt>dependencies_for</tt> if you want a list of dependencies in sorted
170
+ # order.
102
171
  def dependencies
103
172
  manager.dependencies
104
173
  end
105
174
 
106
- # Returns the dependencies of a source file, as an array of strings
175
+ # Returns the dependencies, and transitive dependencies, of a source file as
176
+ # an array of strings, with the load path prepended to the file name. The
177
+ # dependencies are guaranteed to be in sorted order, so that dependent files
178
+ # come first.
179
+ #
180
+ # Example(s):
181
+ #
182
+ # # Print a list of dependencies for each source file.
183
+ # jsdm = JSDM.new
184
+ # jsdm.sources.each |source| { pp jsdm.dependencies_for source }
107
185
  def dependencies_for(source)
108
186
  manager.dependencies_for(source)
109
187
  end
110
188
 
111
- # Returns the dependencies of a source file (and the source file itself) as
112
- # an array of strings
189
+ # Returns the dependencies, and transitive dependencies, of a source file,
190
+ # as well as the source file itself, as an array of strings, with the load path
191
+ # prepended to the file name.
192
+ #
193
+ # The dependencies are guaranteed to be in sorted order, so that dependent
194
+ # files come first.
113
195
  def sources_for(source)
114
196
  dependencies_for(source) << source
115
197
  end
116
198
 
117
- # Returns the require statements for a source file
199
+ # Returns the require statements for a source file, as a list of strings.
200
+ # These are the actual \#require directives for the source file.
118
201
  def requires_for(source)
119
202
  requires.select { |r| r.first == source }.first.last
120
203
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsdm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.4.3
9
+ - 4
10
+ version: 0.4.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Min Huang
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-09 00:00:00 -07:00
18
+ date: 2010-06-22 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21