ninjs 0.13.1 → 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/Gemfile +1 -3
  2. data/Gemfile.lock +1 -67
  3. data/README.md +348 -0
  4. data/Rakefile +2 -4
  5. data/VERSION +1 -1
  6. data/bin/ninjs +196 -39
  7. data/lib/ninjs/command.rb +23 -64
  8. data/lib/ninjs/configuration.rb +16 -38
  9. data/lib/ninjs/generator.rb +23 -12
  10. data/lib/ninjs/project.rb +7 -6
  11. data/ninjs.gemspec +9 -33
  12. data/repository/ninjs/core/module.js +32 -105
  13. data/repository/ninjs/docs/Data/ClassHierarchy.nd +0 -0
  14. data/repository/ninjs/docs/Data/ConfigFileInfo.nd +0 -0
  15. data/repository/ninjs/docs/Data/IndexInfo.nd +0 -0
  16. data/repository/ninjs/docs/Data/SymbolTable.nd +0 -0
  17. data/repository/ninjs/extensions/ninjs.jquery.js +67 -0
  18. metadata +74 -100
  19. data/README.textile +0 -287
  20. data/tmp/metric_fu/output/churn.html +0 -692
  21. data/tmp/metric_fu/output/flay.html +0 -566
  22. data/tmp/metric_fu/output/flay.js +0 -11
  23. data/tmp/metric_fu/output/flog.html +0 -2392
  24. data/tmp/metric_fu/output/flog.js +0 -12
  25. data/tmp/metric_fu/output/hotspots.html +0 -3607
  26. data/tmp/metric_fu/output/index.html +0 -572
  27. data/tmp/metric_fu/output/lib_ninjs.rb.html +0 -49
  28. data/tmp/metric_fu/output/lib_ninjs_command.rb.html +0 -137
  29. data/tmp/metric_fu/output/lib_ninjs_configuration.rb.html +0 -107
  30. data/tmp/metric_fu/output/lib_ninjs_project.rb.html +0 -236
  31. data/tmp/metric_fu/output/rcov.html +0 -566
  32. data/tmp/metric_fu/output/rcov.js +0 -11
  33. data/tmp/metric_fu/output/reek.html +0 -1294
  34. data/tmp/metric_fu/output/reek.js +0 -20
  35. data/tmp/metric_fu/output/roodi.html +0 -599
  36. data/tmp/metric_fu/output/roodi.js +0 -11
  37. data/tmp/metric_fu/report.yml +0 -1548
data/Gemfile CHANGED
@@ -2,14 +2,12 @@ source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
- gem "rubikon", ">= 0"
6
5
  gem "fssm", ">= 0"
7
6
  gem "jsmin", ">= 0"
8
- gem "sprockets", ">= 0"
7
+ gem "sprockets", "1.0.2"
9
8
  # Add dependencies to develop your gem here.
10
9
  # Include everything needed to run rake, tests, features, etc.
11
10
  group :development do
12
- gem "metric_fu", ">= 0"
13
11
  gem "shoulda", ">= 0"
14
12
  gem "bundler", "~> 1.0.0"
15
13
  gem "jeweler", "~> 1.5.2"
@@ -1,71 +1,16 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- Saikuro (1.1.0)
5
- abstract (1.0.0)
6
- activesupport (3.0.5)
7
- arrayfields (4.7.4)
8
- chronic (0.3.0)
9
- churn (0.0.13)
10
- chronic (>= 0.2.3)
11
- hirb
12
- json_pure
13
- main
14
- ruby_parser (~> 2.0.4)
15
- sexp_processor (~> 3.0.3)
16
- colored (1.2)
17
4
  diff-lcs (1.1.2)
18
- erubis (2.6.6)
19
- abstract (>= 1.0.0)
20
- fattr (2.2.0)
21
- flay (1.4.2)
22
- ruby_parser (~> 2.0)
23
- sexp_processor (~> 3.0)
24
- flog (2.5.1)
25
- ruby_parser (~> 2.0)
26
- sexp_processor (~> 3.0)
27
5
  fssm (0.2.2)
28
6
  git (1.2.5)
29
- haml (3.0.25)
30
- hirb (0.4.0)
31
- i18n (0.5.0)
32
7
  jeweler (1.5.2)
33
8
  bundler (~> 1.0.0)
34
9
  git (>= 1.2.5)
35
10
  rake
36
11
  jsmin (1.0.1)
37
- json_pure (1.5.1)
38
- main (4.4.0)
39
- arrayfields (>= 4.7.4)
40
- fattr (>= 2.1.0)
41
- metric_fu (2.1.1)
42
- Saikuro (>= 1.1.0)
43
- activesupport (>= 2.0.0)
44
- chronic (~> 0.3.0)
45
- churn (>= 0.0.7)
46
- flay (>= 1.2.1)
47
- flog (>= 2.3.0)
48
- rails_best_practices (>= 0.6.4)
49
- rcov (>= 0.8.3.3)
50
- reek (>= 1.2.6)
51
- roodi (>= 2.1.0)
52
- syntax
53
- rails_best_practices (0.7.1)
54
- activesupport
55
- colored (~> 1.2)
56
- erubis (~> 2.6.6)
57
- haml (~> 3.0.18)
58
- i18n
59
- ruby-progressbar (~> 0.0.9)
60
- ruby_parser (~> 2.0.4)
61
12
  rake (0.8.7)
62
13
  rcov (0.9.9)
63
- reek (1.2.8)
64
- ruby2ruby (~> 1.2)
65
- ruby_parser (~> 2.0)
66
- sexp_processor (~> 3.0)
67
- roodi (2.1.0)
68
- ruby_parser
69
14
  rspec (2.4.0)
70
15
  rspec-core (~> 2.4.0)
71
16
  rspec-expectations (~> 2.4.0)
@@ -74,17 +19,8 @@ GEM
74
19
  rspec-expectations (2.4.0)
75
20
  diff-lcs (~> 1.1.2)
76
21
  rspec-mocks (2.4.0)
77
- rubikon (0.5.3)
78
- ruby-progressbar (0.0.9)
79
- ruby2ruby (1.2.5)
80
- ruby_parser (~> 2.0)
81
- sexp_processor (~> 3.0)
82
- ruby_parser (2.0.6)
83
- sexp_processor (~> 3.0)
84
- sexp_processor (3.0.5)
85
22
  shoulda (2.11.3)
86
23
  sprockets (1.0.2)
87
- syntax (1.0.0)
88
24
 
89
25
  PLATFORMS
90
26
  ruby
@@ -94,9 +30,7 @@ DEPENDENCIES
94
30
  fssm
95
31
  jeweler (~> 1.5.2)
96
32
  jsmin
97
- metric_fu
98
33
  rcov
99
34
  rspec
100
- rubikon
101
35
  shoulda
102
- sprockets
36
+ sprockets (= 1.0.2)
@@ -0,0 +1,348 @@
1
+ Readme
2
+ ======
3
+
4
+ About
5
+ -----
6
+
7
+ ninjs is a new way of building JavaScript applications that allows you to write modular, testable, and reusable JavaScript. ninjs is not really a framework. It provides three essential JavaScript development tools in one coherent package.
8
+
9
+ 1. ninjs uses the "Sprockets" (http://getsprockets.org) JavaScript compiler to package and include scripts. This provides a way to manage all 3rd party and custom libraries and require them directly into your scripts.
10
+ 2. ninjs includes a small JavaScript framework, based on the module pattern (http://javascript.crockford.com/private.html) and "JavaScript: The Good Parts" (http://www.amazon.com/gp/product/B0026OR2ZY/ref=s9_bbs_gw_d6_ir01?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0HQ3A0RDW9269GPJRGW8&pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846), which provides a solid foundation for any JavaScript application.
11
+ 3. The "ninjs" command line application eases the pain of repetitive tasks like compiling, scaffolding, and updating your application.
12
+
13
+ ninjs is written in Ruby (http://ruby-lang.org) and packaged as a Ruby gem (http://rubygems.org). However, it is not specific to Ruby or Rails development. While it certainly is usable in Rails or Ruby based projects, it is not designed for any one particular language or framework.
14
+
15
+ Installation
16
+ ------------
17
+
18
+ You will need Ruby (1.8.x or 1.9.x) and RubyGems installed on your system to install ninjs using RubyGems. This is the easiest way of installing and recommended for most users.
19
+
20
+ ```sh
21
+ gem install ninjs
22
+ ```
23
+
24
+ For development, clone the project and add bin/ninjs to your path (or my favorite trick which is to put it in my development folder and make symbolic link in /usr/local/bin or wherever you put your scripts):
25
+
26
+ ```sh
27
+ git clone git://github.com/textnotspeech/ninjs.git
28
+ ln -s ninjs/bin/ninjs /usr/local/bin/ninjs
29
+ ```
30
+
31
+ Using the Command Line Application
32
+ ==================================
33
+
34
+ The ninjs command line application is a simple way to quickly develop and manage your application. With it you can create an application, generate scaffolding, compile, and upgrade your application.
35
+
36
+ Create a ninjs application
37
+ ==========================
38
+
39
+ The first ninjs command you'll need is the "create" command. As it's name implies, this command creates a brand new ninjs application template, creating the necessary files and directories. The create command accepts two arguments. The first argument is the name of your application object which is required. The second argument, which is optional, is a sub directory in which to install the application. For example, from the root of your project, you may wish to put your application in a "js" or "javascripts" directory (creating the directory if it does not exist). Omitting the directory option will create the application in your current working directory. Here is an example of creating a ninjs application in a sub directory named "js" in the current working directory:
40
+
41
+ ```sh
42
+ ninjs create myapplication js
43
+ ```
44
+
45
+ This will create a ninjs application in a directory named "js" in the current working directory. Now we can begin developing our application.
46
+
47
+ Create a ninjs module
48
+ =====================
49
+
50
+ Using the generate command we can create a module scafoold. The generate command takes two arguments, the first is the type of file you'd like to generate (module, elements, or model) and the second is the name of the file/module the scaffold is for. Here's some examples of using the generate command with a module named hello:
51
+
52
+ ```sh
53
+ ninjs generate module hello
54
+ ninjs generate elements hello
55
+ ninjs generate model hello
56
+ ```
57
+
58
+ If you'd like to generate a module with elements and a model all in one go you can pass the -e and -m flags respectively:
59
+
60
+ ```sh
61
+ ninjs generate module hello -em
62
+ ````
63
+
64
+ Although the generate method is very convenient, you may choose to create a module file manually in the /modules directory. By convention the file name ends with a suffix of .module. An example of a module named hello would look like this:
65
+
66
+ ```sh
67
+ /modules/hello.module.js
68
+ ```
69
+
70
+ The basic functionality of a module is to encapsulate specific pieces of logic into a namespace. You may think of a module as a class in the sense that it allows you to namespace properties and behavior within your application. Typically, a module encapsulates the behavior on a specific page. A ninjs module is an extremely lightweight object with a simple API which helps you write clear, concise code. Here's what an empty module scaffold looks like (generated with ninjs generate model hello -em):
71
+
72
+ ```js
73
+ (function() {
74
+ var self = myapplication.add_module('hello');
75
+
76
+ //= require "../models/hello.model"
77
+ //= require "../elements/hello.elements"
78
+
79
+ myapplication.hello.actions = function() {
80
+
81
+ };
82
+
83
+ myapplication.hello.run();
84
+ })();
85
+ ```
86
+
87
+ Notice the module is wrapped in a closure. This allows us to make a private reference to the current module named "self" due to the fact that the add_module method returns the module it creates. This gives us a consistent way to reference the module without having to use the entire namespace. We may also decide to put private variables and functions available to the module but not exposed to the rest of the application.
88
+
89
+ The actions method is the main method of your module. Also known as the composed method pattern, the actions method should simply be a list of other module methods. This makes it easy to scan the actions method to get a sense of what a given module does. It also encourages the "single responsibility principle" (http://en.wikipedia.org/wiki/Single_responsibility_principle).
90
+
91
+ The run method will execute the actions method when the DOM is ready to be manipulated. This is similar to jQuery's $(document).ready() method. If you wish to execute your modules actions as soon as the script is parsed you may call the execute method instead of run:
92
+
93
+ ```js
94
+ myapplication.hello.execute();
95
+ ```
96
+
97
+ This pattern allows you to write in a literate style, making your intentions clear and methods succinct. It's idiomatic to use the module namespace (instead of using self) when defining your module methods to enhance clarity. Another advantage of wrapping your modules in a closure is that you may choose to define an alias for your application object which makes it easier to type while avoiding creation of another global variable. To do this, simply pass in the application object as an argument to the outer function and then name the alias in the argument to the closure like so:
98
+
99
+ ```js
100
+ (function(app) {
101
+
102
+ var self = app.add_module('hello');
103
+
104
+ app.hello.actions = function() {
105
+
106
+ };
107
+
108
+ app.hello.run();
109
+
110
+ })(myapplication);
111
+ ```
112
+
113
+ You can also generate a scaffold with an alias using the -a option of the generate command. The previous example can be generated like so:
114
+
115
+ ```sh
116
+ ninjs generate module hello -a
117
+ ```
118
+
119
+ By default, the alias option will use "app" as the alias. To create a custom alias simply pass the name after the -a option like so:
120
+
121
+ ```sh
122
+ ninjs generate module hello -a myalias
123
+ ```
124
+
125
+ This will generate a file using an alias defined as "myalias". This is simply a convenience and a bit of syntactic sugar, although there are practical benefits to this as well. For example using this pattern, any module can be ported to another application simply by swapping out the application object passed to the closure.
126
+
127
+ Let's take a closer look at the "actions" composed method pattern. Let's define a simple module which demonstrates this:
128
+
129
+ ```js
130
+ (function() {
131
+ var self = myapplication.add_module('hello');
132
+
133
+ myapplication.hello.actions = function() {
134
+ self.set_defaults();
135
+ self.say_hello();
136
+ };
137
+
138
+ myapplication.hello.set_defaults = function() {
139
+ self.greeting = 'Hello';
140
+ self.guest = 'World';
141
+ };
142
+
143
+ myapplication.hello.say_hello = function() {
144
+ alert(self.get_greeting());
145
+ };
146
+
147
+ myapplication.get_greeting = function() {
148
+ return self.greeting + ' ' + self.guest + '!';
149
+ };
150
+
151
+ myapplication.hello.run();
152
+ })();
153
+ ```
154
+
155
+ Although the complexity of this module is contrived, we can easily see what this module does simply by glancing at the actions method. This will make your application logic easier to write, understand, modify, and test
156
+
157
+ Create module elements
158
+ ======================
159
+
160
+ Another common best practice that ninjs encourages is cacheing your element selectors. For example, when using jQuery to select a DOM element, it's typical to assign the result of the selection to a variable in case you need it again. Here's what this looks like in practice:
161
+
162
+ ```js
163
+ // Bad no-caching
164
+ $('#some-element').css({ 'background-color': '#FF0000' });
165
+ $('#some-element').html("I turned red");
166
+
167
+ // Good caching
168
+ var some_element = $('#some-element');
169
+
170
+ some_element.css({ 'background-color': '#FF0000' });
171
+ some_element.html("I turned red");
172
+ ```
173
+
174
+ When we cache our selections, we only have to search the DOM once, improving performance.
175
+
176
+ The only problem with this is that we tend to manipulate a lot of elements and our code can become littered with them. At worst, they're strewn about the file wherever they are used, making it easy to accidentally re-assign them. At best, all selections are cached in one place and easy to see, which prevents us from accidentally caching them twice but it becomes a fair amount of boilerplate that crufts up the file. ninjs goes a step further by putting these cached selectors in their own file in the elements folder. This gives us one place to manage all the cached selectors available to our module.
177
+
178
+ Elements belong to a module and can be added using the elements method. To add elements to the hello module, let's add a hello.elements.js with the generate command:
179
+
180
+ ```sh
181
+ ninjs generate elements hello
182
+ ```
183
+
184
+ This will create a hello.elements.js file inside the elements directory. The elements scaffold looks like this:
185
+
186
+ ```js
187
+ myapplication.hello.elements({
188
+
189
+ });
190
+ ```
191
+
192
+ The elements method facilitates both setting and getting cached elements (violating the single responsibility principle for convenience). When passed an object, the elements method maps the key/value pairs of name/selector to the modules dom object. When passed a string, the elements method pulls the selector via it's name. To set a module's elements, pass an object of key value pairs like so:
193
+
194
+ ```js
195
+ myapplication.hello.elements({
196
+ message_box: $('#message-box')
197
+ });
198
+ ```
199
+
200
+ Now these cached jQuery selectors will be available via the elements command by passing it's name into the method like so:
201
+
202
+ ```js
203
+ myapplication.hello.some_method = function() {
204
+ self.elements('message_box');
205
+ };
206
+ ```
207
+
208
+ This pattern provides a consistent way to access and create elements and also creates an opportunity to add logic to the process (as in the ninjs.jquery.elements.js plugin). That's all there is to creating module elements. Now we require the file in the module using the "Sprockets require directive" (http://getsprockets.org/installation_and_usage#specifying_dependencies_with_the_require_directive) in hello.module.js.
209
+
210
+ ```js
211
+ (function() {
212
+ var self = myapplication.add_module('hello');
213
+
214
+ //= require "../elements/hello.elements"
215
+
216
+ ...
217
+ })();
218
+ ```
219
+
220
+ Be sure to require the elements file after the "add_module" method is called. Now all elements defined in the elements method will be available to the module. Let's take our hello example and instead of alerting the greeting, let's put it in the message_box element (assuming an html page containing this element):
221
+
222
+ ```js
223
+ ...
224
+
225
+ myapplication.hello.say_hello = function() {
226
+ self.elements('message_box').html(self.get_greeting());
227
+ };
228
+
229
+ ...
230
+ ```
231
+
232
+ Again, this pattern keeps the logic very clear and our code very concise. It's easy to read, test, and refactor. With time, you'll develop your own naming conventions and standards. The important thing is to focus on good semantic names that accurately describe the properties and behavior of your application.
233
+
234
+ Most modules will be exactly like the one we just created, only with more methods. However, there is one more piece that helps you achieve greater modularity, which is ninjs models.
235
+
236
+ Create a ninjs model
237
+ ====================
238
+
239
+ ninjs models are simply files in the /models directory that define a data structure for the module. Every module has a data object that stores properties for reuse or configuration.
240
+
241
+ Let's suppose I have a plugin that I want to use in my module. The plugin takes a configuration object, but I want to be able to set a default configuration for my module. I'll use the generate command to create a model:
242
+
243
+ ```sh
244
+ ninjs generate model hello
245
+ ```
246
+
247
+ This will create an empty model scaffold that looks like this:
248
+
249
+ ```js
250
+ myapplication.hello.set_data({
251
+
252
+ });
253
+ ```
254
+
255
+ The set_data method takes either an object or a string as the first argument. When an object is used as the first argument, the key value pairs contained in the object will be copied to the module's "data" object. When a string is used, the first argument is a string representing the name of the property to assign and the second is the value to assign to that property. Using the string method, we could define a configuration object for the plugin like so:
256
+
257
+ ```js
258
+ myapplication.hello.set_data('plugin_config', {
259
+ width: 300,
260
+ height: 250
261
+ });
262
+ ```
263
+
264
+ If we wish to set several properties at once we can use just an object as the first argument:
265
+
266
+ ```js
267
+ myapplication.hello.set_data({
268
+ plugin_config: {
269
+ width: 300,
270
+ height: 250
271
+ },
272
+ another_property: 'some value'
273
+ });
274
+ ```
275
+
276
+ Next we include the model in the module file:
277
+
278
+ ```js
279
+ (function() {
280
+ var self = myapplication.add_module('hello');
281
+
282
+ //= require "../elements/hello.model.js"
283
+ //= require "../elements/hello.elements"
284
+
285
+ ...
286
+ })();
287
+ ```
288
+
289
+ Now whenever I use the plugin in my module, I can mix in the config object like so:
290
+
291
+ ```js
292
+ ...
293
+
294
+ myapplication.hello.setup_plugin = function() {
295
+ self.some_element.some_plugin(self.data.plugin_config);
296
+ self.another_element.some_plugin(self.data.plugin_config);
297
+ };
298
+
299
+ ...
300
+ ```
301
+
302
+ This way, we don't have to keep redefining the same properties each time we use this plugin. If we want to extend the defaults, we can use something like jQuery's extend method:
303
+
304
+ ```js
305
+ ...
306
+
307
+ myapplication.hello.setup_plugin = function() {
308
+ self.some_element.some_plugin(self.data.plugin_config);
309
+ self.another_element.some_plugin($.extend(self.data.plugin_config, {
310
+ height: 300,
311
+ color: #FF0000
312
+ }));
313
+ };
314
+
315
+ ...
316
+ ```
317
+
318
+ The model provides a foundation we can build from, helping use to keep our code DRY.
319
+
320
+ Compiling the application
321
+ =========================
322
+
323
+ Now that we have a complete module including elements and a model, we need to compile these into one coherent file to use in our html. To do so, we have 2 options. Open up a terminal and navigate to the root of your ninjs application. We can compile our application with one of 2 commands. The first choice is the compile command. From the root of your ninjs application type:
324
+
325
+ ```sh
326
+ ninjs compile
327
+ ```
328
+
329
+ This will compile all the files in the modules folder, resolving all dependencies using the Sprockets engine, and finally outputting them into the /application directory with the .module suffix removed. Our hello example module would compile into the application folder as hello.js. Now we can include the hello.js file (along with the myapplication.js) in our html document. Since running compile every time we make a change to any one of our module source files would quickly become a tedious chore, ninjs also has a watch command which will watch your project directory for changes and automatically compile when a file is changed. This speeds up development considerably and frankly makes ninjs usable in a daily development context. To watch a ninjs project simply navigate to the project root and issue the watch command:
330
+
331
+ ```sh
332
+ ninjs watch
333
+ ```
334
+
335
+ That's it! you've created a basic ninjs application! The only step left is to include the compiled script files in our markup:
336
+
337
+ ```html
338
+ <!DOCTYPE html>
339
+ <head>
340
+ <title>ninjs Demo</title>
341
+ </head>
342
+ <body>
343
+ <script src="js/application/myapplication.js"></script>
344
+ <script src="js/application/hello.js"></script>
345
+ </body>
346
+ </html>
347
+ ...
348
+ ```