ninjs 0.13.8 → 0.14.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.
Files changed (37) hide show
  1. data/VERSION +1 -1
  2. data/bin/ninjs +2 -1
  3. data/lib/ninjs/command.rb +1 -1
  4. data/lib/ninjs/generator.rb +14 -10
  5. data/lib/ninjs/project.rb +7 -4
  6. data/ninjs.gemspec +20 -7
  7. data/repository/ninjs/core/application.js +24 -43
  8. data/repository/ninjs/core/dom.js +139 -0
  9. data/repository/ninjs/core/existence.js +121 -277
  10. data/repository/ninjs/core/extend.js +35 -77
  11. data/repository/ninjs/core/module.js +101 -116
  12. data/repository/ninjs/core/nin.js +7 -4
  13. data/repository/ninjs/tests/application.test.js +24 -0
  14. data/repository/ninjs/tests/array.utilities.test.js +55 -0
  15. data/repository/ninjs/tests/existence.test.js +64 -0
  16. data/repository/ninjs/tests/extension.test.js +38 -0
  17. data/repository/ninjs/tests/index.html +11 -5
  18. data/repository/ninjs/tests/module.test.js +75 -0
  19. data/repository/ninjs/tests/qspec.js +26 -0
  20. data/repository/ninjs/tests/{ninjs.utilities.test.js → string.utilities.test.js} +14 -66
  21. data/spec/cli_spec.rb +159 -0
  22. data/spec/command_spec.rb +226 -2
  23. data/spec/fixtures/compressed.myapp.js +40 -20
  24. data/spec/fixtures/myapp.initial.js +383 -488
  25. data/spec/fixtures/myapp.js +379 -484
  26. data/spec/fixtures/mymodule.alias.module.js +10 -0
  27. data/spec/fixtures/mymodule.dependencies.module.js +13 -0
  28. data/spec/fixtures/mymodule.elements.js +5 -0
  29. data/spec/fixtures/mymodule.model.js +3 -0
  30. data/spec/fixtures/mymodule.module.js +10 -0
  31. data/spec/fixtures/nin.js +428 -0
  32. data/spec/generator_spec.rb +58 -10
  33. data/spec/project_spec.rb +11 -7
  34. metadata +59 -9
  35. data/repository/ninjs/tests/ninjs.test.js +0 -188
  36. data/repository/ninjs/tests/qunit/qunit.css +0 -197
  37. data/repository/ninjs/tests/qunit/qunit.js +0 -1415
@@ -1,77 +1,35 @@
1
- /* File: extend.js */
2
- //= require "existence"
3
-
4
- if (is_undefined(Function.prototype['method'])) {
5
- /*
6
- Function: method
7
- Method to add a method to an object (ie. String.method('my_method', my_func); // 'hello'.my_method())
8
-
9
- Parameters:
10
- name - name of the method
11
- func - function definition
12
-
13
- Returns:
14
- this (chainable)
15
-
16
- > String.method('custom_method', function() {
17
- > // define custom_method
18
- > });
19
- >
20
- > "hello".custom_method();
21
- */
22
- Function.prototype.method = function(name, func) {
23
- if (is_undefined(name)) {
24
- throw new SyntaxError("Object.method(name, func): name is undefined");
25
- }
26
-
27
- if (is_undefined(func)) {
28
- throw new SyntaxError("Object.method(name, func): func is undefined");
29
- }
30
-
31
- if (is_undefined(this.prototype[name])) {
32
- this.prototype[name] = func;
33
- return this;
34
- }
35
- };
36
- }
37
-
38
- if (is_undefined(unless)) {
39
- /*
40
- Function: unless
41
- Function to better express negative conditions (ie. if (!something))
42
-
43
- Parameters:
44
- expression - expression to be tested
45
- callback - function to be executed unless expression is true (see how that works)
46
- fallback - function to be executed if the expression is false (optional)
47
-
48
- Returns:
49
- undefined
50
-
51
- > unless(test_expression === 'some condition',
52
- > function() {
53
- > alert('we do something');
54
- > },
55
- > function() {
56
- > alert('we can do something if it meets the condition too');
57
- > }
58
- > );
59
- */
60
- var unless = function(expression, callback, fallback) {
61
- if (is_undefined(expression)) {
62
- throw new SyntaxError("unless(expression, callback[, fallback]): expression is undefined");
63
- }
64
-
65
- if (is_undefined(callback)) {
66
- throw new SyntaxError("unless(expression, callback[, fallback]): callback is undefined");
67
- }
68
-
69
- // This kind of expression is exactly why we NEED unless
70
- if (!expression) {
71
- callback.call(this);
72
- }
73
- else if (is_defined(fallback)) {
74
- fallback.call(this);
75
- }
76
- };
77
- }
1
+ if (is_undefined(Function.prototype['method'])) {
2
+ Function.prototype.method = function(name, func) {
3
+ if (is_undefined(name)) {
4
+ throw new SyntaxError("Object.method(name, func): name is undefined");
5
+ }
6
+
7
+ if (is_undefined(func)) {
8
+ throw new SyntaxError("Object.method(name, func): func is undefined");
9
+ }
10
+
11
+ if (is_undefined(this.prototype[name])) {
12
+ this.prototype[name] = func;
13
+ return this;
14
+ }
15
+ };
16
+ }
17
+
18
+ if (is_undefined(window.unless)) {
19
+ window.unless = function(expression, callback, fallback) {
20
+ if (is_undefined(expression)) {
21
+ throw new SyntaxError("unless(expression, callback[, fallback]): expression is undefined");
22
+ }
23
+
24
+ if (is_undefined(callback)) {
25
+ throw new SyntaxError("unless(expression, callback[, fallback]): callback is undefined");
26
+ }
27
+
28
+ if (!expression) {
29
+ callback.call(this);
30
+ }
31
+ else if (is_defined(fallback)) {
32
+ fallback.call(this);
33
+ }
34
+ };
35
+ }
@@ -1,135 +1,120 @@
1
- var NinjsModule = function(name) {
2
- this.dom = {};
3
- this.data = {};
4
- this.name = name;
5
- this.run_tests = false;
6
- this.tests = [];
7
- };
1
+ window.NinjsModule = function(name) {
2
+ this.dom = new NinjsDOM(this);
3
+ this.browser = browser;
4
+ this.data = {};
5
+ this.name = name;
6
+ this.run_tests = false;
7
+ this.tests = [];
8
+ };
8
9
 
9
- NinjsModule.method('actions', function() {});
10
+ NinjsModule.method('actions', function() {});
10
11
 
11
- NinjsModule.method('run', function() {
12
- this.call_on_ready(this.execute);
13
- });
14
-
15
- NinjsModule.method('call_on_ready', function(callback) {
16
- var timer;
17
- var module = this;
18
-
19
- function check_ready() {
20
- timer = setInterval(is_ready, 13);
21
- }
12
+ NinjsModule.method('run', function() {
13
+ var mod = this;
14
+ this.dom.ready(function() {
15
+ mod.execute();
16
+ });
17
+ });
22
18
 
23
- function is_ready() {
24
- if (document && document.getElementsByTagName && document.getElementById && document.body) {
25
- clearInterval(timer);
26
- timer = null;
27
- callback.call(module);
19
+ NinjsModule.method('execute', function() {
20
+ if (this.run_tests) {
21
+ this._run_tests();
28
22
  }
29
- }
30
23
 
31
- check_ready();
32
- });
24
+ this.actions();
25
+ });
33
26
 
34
- NinjsModule.method('execute', function() {
35
- if (this.run_tests) {
36
- this._run_tests();
37
- }
38
-
39
- this.actions();
40
- });
27
+ NinjsModule.method('elements', function(elements) {
28
+ if (is_undefined(elements)) {
29
+ if (is_typeof(Object, elements)) {
30
+ throw new SyntaxError("NinjsModule.elements(elements): elements is undefined");
31
+ }
32
+ else if (is_string(elements)) {
33
+ throw new SyntaxError("NinjsModule.elements(name): name is undefined");
34
+ }
35
+ }
41
36
 
42
- NinjsModule.method('elements', function(elements) {
43
- if (is_undefined(elements)) {
44
- if (is_typeof(Object, elements)) {
45
- throw new SyntaxError("NinjsModule.elements(elements): elements is undefined");
46
- }
47
- else if (is_string(elements)) {
48
- throw new SyntaxError("NinjsModule.elements(name): name is undefined");
49
- }
50
- }
51
-
52
- // If first argument is a string, retrieve the element
53
- if (is_string(elements)) {
54
- var name = elements;
55
- return is_defined(this.dom[name]) ? this.dom[name] : undefined;
56
- }
57
- // Set elements
58
- else {
59
- this.call_on_ready(function() {
60
- for(var key in elements) {
61
- if (elements.hasOwnProperty(key)) {
62
- this.dom[key] = elements[key];
37
+ if (is_string(elements)) {
38
+ var name = elements;
39
+ return this.dom.cached_selectors[name];
40
+ }
41
+ else {
42
+ var dom = this.dom;
43
+ dom.ready(function() {
44
+ for(var key in elements) {
45
+ if (elements.hasOwnProperty(key)) {
46
+ dom.cached_selectors[key] = elements[key];
47
+ }
63
48
  }
64
- }
65
- });
66
- }
67
- });
49
+ });
50
+ }
51
+ });
68
52
 
69
- NinjsModule.method('set_data', function(key, value) {
70
- if (is_undefined(key)) {
71
- throw new SyntaxError('NinjsModule.set_data(key, value): key is undefined');
72
- }
73
-
74
- if (is_typeof(String, key) && is_undefined(value)) {
75
- throw new SyntaxError('NinjsModule.set_data(key, value): value is undefined');
76
- }
53
+ NinjsModule.method('set_data', function(key, value) {
54
+ if (is_undefined(key)) {
55
+ throw new SyntaxError('NinjsModule.set_data(key, value): key is undefined');
56
+ }
57
+
58
+ if (is_typeof(String, key) && is_undefined(value)) {
59
+ throw new SyntaxError('NinjsModule.set_data(key, value): value is undefined');
60
+ }
77
61
 
78
- if (is_typeof(String, key)) {
79
- this.data[key] = value;
80
- }
81
- else if (is_typeof(Object, key)) {
82
- var data = key;
83
- for(var property in data) {
84
- this.data[property] = data[property];
62
+ if (is_typeof(String, key)) {
63
+ this.data[key] = value;
64
+ }
65
+ else if (is_typeof(Object, key)) {
66
+ var data = key;
67
+ for(var property in data) {
68
+ this.data[property] = data[property];
69
+ }
85
70
  }
86
- }
87
71
 
88
- return this;
89
- });
72
+ return this;
73
+ });
90
74
 
91
- NinjsModule.method('add_test', function(test_file) {
92
- this.tests.push(test_file);
93
- });
75
+ NinjsModule.method('add_test', function(test_file) {
76
+ this.tests.push(test_file);
77
+ });
94
78
 
95
- NinjsModule.method('_run_tests', function() {
96
- var test_template = [];
97
- test_template.push('<div class="test-results" title="Test Results">');
98
- test_template.push('<h1 id="qunit-header">' + this.name + ' module tests</h1>');
99
- test_template.push('<h2 id="qunit-banner"></h2>');
100
- test_template.push('<h2 id="qunit-userAgent"></h2>');
101
- test_template.push('<ol id="qunit-tests"></ol>');
102
- test_template.push('</div>');
79
+ NinjsModule.method('_run_tests', function() {
80
+ var test_template = [];
81
+ test_template.push('<div class="test-results" title="Test Results">');
82
+ test_template.push('<h1 id="qunit-header">' + this.name + ' module tests</h1>');
83
+ test_template.push('<h2 id="qunit-banner"></h2>');
84
+ test_template.push('<h2 id="qunit-userAgent"></h2>');
85
+ test_template.push('<ol id="qunit-tests"></ol>');
86
+ test_template.push('</div>');
103
87
 
104
- var qunit_dependencies = '<script src="' + _.tests_path +'qunit/qunit.js"></script>';
105
- $.getScript(_.tests_path + 'qunit/qunit.js');
106
- var qunit_styles = '<link rel="stylesheet" href="' + _.tests_path + 'qunit/qunit.css">';
107
- $('body').append(qunit_styles);
108
- $('body').append(test_template.join("\n"));
88
+ var qunit_dependencies = '<script src="' + _.tests_path +'qunit/qunit.js"></script>';
89
+ $.getScript(_.tests_path + 'qunit/qunit.js');
90
+ var qunit_styles = '<link rel="stylesheet" href="' + _.tests_path + 'qunit/qunit.css">';
91
+ $('body').append(qunit_styles);
92
+ $('body').append(test_template.join("\n"));
109
93
 
110
- this.tests.each(function(test) {
111
- $.getScript(_.tests_path + test + '.test.js', function() {
112
- var test_results_dialog = $('.test-results');
113
- var height = $(window).height() - 200;
114
- var width = $(window).width() - 300;
115
- try {
116
- test_results_dialog.dialog({
117
- width: width,
118
- height: height,
119
- autoOpen: false,
120
- buttons: {
121
- "Thanks buddy": function() {
122
- test_results_dialog.dialog('close');
94
+ this.tests.each(function(test) {
95
+ $.getScript(_.tests_path + test + '.test.js', function() {
96
+ var test_results_dialog = $('.test-results');
97
+ var height = $(window).height() - 200;
98
+ var width = $(window).width() - 300;
99
+ try {
100
+ test_results_dialog.dialog({
101
+ width: width,
102
+ height: height,
103
+ autoOpen: false,
104
+ buttons: {
105
+ "Thanks buddy": function() {
106
+ test_results_dialog.dialog('close');
107
+ }
123
108
  }
124
- }
125
- });
126
- var failed = $('.failed');
127
- console.log(failed.html());
128
- test_results_dialog.dialog('open');
129
- }
130
- catch(error) {
131
- alert("Test harness requires jQueryUI");
132
- }
109
+ });
110
+ var failed = $('.failed');
111
+ console.log(failed.html());
112
+ test_results_dialog.dialog('open');
113
+ }
114
+ catch(error) {
115
+ alert("Test harness requires jQueryUI");
116
+ }
117
+ });
133
118
  });
134
119
  });
135
- });
120
+
@@ -1,4 +1,7 @@
1
- /* File: nin.js */
2
- //= require "extend"
3
- //= require "module"
4
- //= require "application"
1
+ (function() {
2
+ //= require "existence"
3
+ //= require "extend"
4
+ //= require "dom"
5
+ //= require "module"
6
+ //= require "application"
7
+ })();
@@ -0,0 +1,24 @@
1
+ var spec = new QSpec("NinjsApplication");
2
+
3
+ spec.before = function() {
4
+ this.app = new NinjsApplication('myapp');
5
+ };
6
+
7
+ spec.after = function() {
8
+ delete this.app;
9
+ };
10
+
11
+ spec.should("create a ninjs application object", function() {
12
+ this.app = new NinjsApplication('myapp');
13
+ ok(is_defined(this.app), 'app is defined');
14
+ ok(is_typeof(NinjsApplication, this.app), 'app is a valid NinjsApplication');
15
+ });
16
+
17
+ spec.should("create a NinjsModule", function() {
18
+ this.app = new NinjsApplication('myapp');
19
+ this.app.add_module('mymod');
20
+
21
+ ok(is_typeof(NinjsModule, this.app.mymod), 'app.mymod is a valid NinjsModule');
22
+ });
23
+
24
+ spec.run_all();
@@ -0,0 +1,55 @@
1
+ var spec = new QSpec("Array Extensions");
2
+
3
+ spec.should("test for emptiness with is_empty and not_empty", function() {
4
+ expect(4);
5
+
6
+ ok([].is_empty(), "[].is_empty() is true");
7
+ equals(['one', 'two', 'three'].is_empty(), false, "['one', 'two', 'three'].is_empty()");
8
+ ok(['one', 'two', 'three'].not_empty(), "['one', 'two', 'three'].not_empty() is false");
9
+ equals([].not_empty(), false, "[].not_empty() is false");
10
+ });
11
+
12
+ spec.should("iterate over each element with each", function() {
13
+ expect(7);
14
+
15
+ var iteration_count = 0;
16
+ var test_array_values = [];
17
+ var test_array_indices = [];
18
+
19
+ ['one', 'two', 'three'].each(function(value, index) {
20
+ iteration_count++;
21
+ test_array_values.push(value);
22
+ test_array_indices.push(index);
23
+ });
24
+
25
+ equals(test_array_values[0], 'one', 'value at index 0 is correct');
26
+ equals(test_array_values[1], 'two', 'value at index 1 is correct');
27
+ equals(test_array_values[2], 'three', 'value at index 2 is correct');
28
+
29
+ equals(test_array_indices[0], 0, 'first index is correct');
30
+ equals(test_array_indices[1], 1, 'second index is correct');
31
+ equals(test_array_indices[2], 2, 'third index is correct');
32
+
33
+ equals(iteration_count, 3, 'made only three iterations');
34
+ });
35
+
36
+ spec.should("test if array contains an element", function() {
37
+ var array = ['one', 'two', 'three'];
38
+ var string = 'hello';
39
+ var object = {
40
+ name: 'some object'
41
+ };
42
+ var number = 45;
43
+ var date = new Date();
44
+
45
+ var test_array = [array, string, object, number, date];
46
+
47
+ ok(test_array.contains(array), 'array.contains(array)');
48
+ ok(test_array.contains(string), 'array.contains(string)');
49
+ ok(test_array.contains(object), 'array.contains(object)');
50
+ ok(test_array.contains(number), 'array.contains(number)');
51
+ ok(test_array.contains(date), 'array.contains(date)');
52
+ equals(test_array.contains('not in there'), false, 'non-existent value is false');
53
+ });
54
+
55
+ spec.run_all();
@@ -0,0 +1,64 @@
1
+ var spec = new QSpec("Existence");
2
+
3
+ spec.should("test for existence with is_defined", function() {
4
+ var nonexistent;
5
+ var existent = 'I think';
6
+ equals(is_defined(existent), true, 'existent variable is_defined');
7
+ equals(is_defined(nonexistent), false, 'non-existent variable does not exist');
8
+ });
9
+
10
+ spec.should("test for non-existence with is_undefined", function() {
11
+ var existent = 'I think';
12
+ var nonexistent;
13
+ equals(is_undefined(nonexistent), true, 'non-existent variable does not exist');
14
+ equals(is_undefined(existent), false, 'existent variable does exist');
15
+ });
16
+
17
+ spec.should("check the type strictly with is_typeof", function() {
18
+ var foo = function(){};
19
+ var bar = {
20
+ name: 'SomeObject',
21
+ method: function() {}
22
+ };
23
+ var SomeClass = function(){};
24
+ var some_instance = new SomeClass();
25
+
26
+ equals(is_typeof(Number, 4), true, 'can check against Number');
27
+ equals(is_typeof(String, 'Hello World'), true, 'can check against String');
28
+ equals(is_typeof(Array, ['one', 'two', 'three']), true, 'can check against Array');
29
+ equals(is_typeof(Function, foo), true, 'can check against Function');
30
+ equals(is_typeof(Object, bar), true, 'can check against Object');
31
+ equals(is_typeof(RegExp, /pattern/), true, 'can check against Regexp');
32
+ equals(is_typeof(SomeClass, some_instance), true, 'can check against custom object');
33
+ });
34
+
35
+ spec.should("check for default types", function() {
36
+ var today = new Date();
37
+ var easy_as = [1,2,3];
38
+ var pattern = new RegExp(/pattern/);
39
+
40
+ ok(is_string('hello'), 'hello is_string');
41
+ ok(is_number(42), '42 is_number');
42
+ ok(is_array(easy_as), 'easy_as is_array');
43
+ ok(is_bool(false), 'false is_bool');
44
+ ok(is_date(today), 'today is_date');
45
+ ok(is_regex(pattern), 'pattern is_regex');
46
+
47
+ equals(is_regex('hello'), false, 'hello fails is_regex');
48
+ equals(is_date(42), false, '42 fails is_date');
49
+ equals(is_bool(easy_as), false, 'easy_as fails is_bool');
50
+ equals(is_array(today), false, 'today fails is_array');
51
+ equals(is_number(true), false, 'true fails is_number');
52
+ equals(is_string(pattern), false, 'pattern fails is_string');
53
+ });
54
+
55
+ spec.should("determine if a string is a number", function() {
56
+ ok(is_numeric(2), '2 is a number');
57
+ ok(is_numeric(-2), '-2 is a number');
58
+ ok(is_numeric(45.6), '45.6 is a number');
59
+ ok(is_numeric(-45.6), '-45.6 is a number');
60
+ equals(is_numeric('45.6'), true, "'45.6 is a number'");
61
+ equals(is_numeric('Hello'), false, 'Hello is not a number');
62
+ });
63
+
64
+ spec.run_all();