joosy 0.1.0.RC1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +14 -0
  3. data/Gemfile.lock +159 -0
  4. data/Guardfile +30 -0
  5. data/MIT-LICENSE +21 -0
  6. data/README.rdoc +3 -0
  7. data/Rakefile +14 -0
  8. data/app/assets/javascripts/joosy.js.coffee +7 -0
  9. data/app/assets/javascripts/joosy/core/application.js.coffee +28 -0
  10. data/app/assets/javascripts/joosy/core/form.js.coffee +87 -0
  11. data/app/assets/javascripts/joosy/core/helpers.js.coffee +6 -0
  12. data/app/assets/javascripts/joosy/core/joosy.js.coffee +65 -0
  13. data/app/assets/javascripts/joosy/core/layout.js.coffee +47 -0
  14. data/app/assets/javascripts/joosy/core/modules/container.js.coffee +59 -0
  15. data/app/assets/javascripts/joosy/core/modules/events.js.coffee +35 -0
  16. data/app/assets/javascripts/joosy/core/modules/filters.js.coffee +39 -0
  17. data/app/assets/javascripts/joosy/core/modules/log.js.coffee +15 -0
  18. data/app/assets/javascripts/joosy/core/modules/module.js.coffee +43 -0
  19. data/app/assets/javascripts/joosy/core/modules/renderer.js.coffee +116 -0
  20. data/app/assets/javascripts/joosy/core/modules/time_manager.js.coffee +25 -0
  21. data/app/assets/javascripts/joosy/core/modules/widgets_manager.js.coffee +58 -0
  22. data/app/assets/javascripts/joosy/core/page.js.coffee +156 -0
  23. data/app/assets/javascripts/joosy/core/preloader.js.coffee +5 -0
  24. data/app/assets/javascripts/joosy/core/resource/generic.js.coffee +61 -0
  25. data/app/assets/javascripts/joosy/core/resource/rest.js.coffee +76 -0
  26. data/app/assets/javascripts/joosy/core/resource/rest_collection.js.coffee +48 -0
  27. data/app/assets/javascripts/joosy/core/router.js.coffee +89 -0
  28. data/app/assets/javascripts/joosy/core/templaters/rails_jst.js.coffee +20 -0
  29. data/app/assets/javascripts/joosy/core/widget.js.coffee +41 -0
  30. data/app/assets/javascripts/joosy/preloaders/caching.js.coffee +94 -0
  31. data/app/assets/javascripts/joosy/preloaders/inline.js.coffee +55 -0
  32. data/app/helpers/joosy/sprockets_helper.rb +23 -0
  33. data/app/views/layouts/json_wrapper.json.erb +1 -0
  34. data/joosy.gemspec +25 -0
  35. data/lib/joosy.rb +9 -0
  36. data/lib/joosy/forms.rb +47 -0
  37. data/lib/joosy/rails/engine.rb +17 -0
  38. data/lib/joosy/rails/version.rb +5 -0
  39. data/lib/rails/generators/joosy/application_generator.rb +41 -0
  40. data/lib/rails/generators/joosy/joosy_base.rb +30 -0
  41. data/lib/rails/generators/joosy/layout_generator.rb +32 -0
  42. data/lib/rails/generators/joosy/page_generator.rb +44 -0
  43. data/lib/rails/generators/joosy/preloader_generator.rb +32 -0
  44. data/lib/rails/generators/joosy/resource_generator.rb +29 -0
  45. data/lib/rails/generators/joosy/templates/app.js.coffee +10 -0
  46. data/lib/rails/generators/joosy/templates/app/helpers/application.js.coffee +4 -0
  47. data/lib/rails/generators/joosy/templates/app/layouts/application.js.coffee +2 -0
  48. data/lib/rails/generators/joosy/templates/app/layouts/template.js.coffee +2 -0
  49. data/lib/rails/generators/joosy/templates/app/pages/application.js.coffee +1 -0
  50. data/lib/rails/generators/joosy/templates/app/pages/template.js.coffee +5 -0
  51. data/lib/rails/generators/joosy/templates/app/pages/welcome/index.js.coffee +22 -0
  52. data/lib/rails/generators/joosy/templates/app/resources/template.js.coffee +2 -0
  53. data/lib/rails/generators/joosy/templates/app/routes.js.coffee +8 -0
  54. data/lib/rails/generators/joosy/templates/app/templates/layouts/application.jst.hamlc +2 -0
  55. data/lib/rails/generators/joosy/templates/app/templates/pages/welcome/index.jst.hamlc +7 -0
  56. data/lib/rails/generators/joosy/templates/app/widgets/template.js.coffee +2 -0
  57. data/lib/rails/generators/joosy/templates/app_controller.rb +9 -0
  58. data/lib/rails/generators/joosy/templates/app_preloader.js.coffee.erb +13 -0
  59. data/lib/rails/generators/joosy/templates/preload.html.erb +26 -0
  60. data/lib/rails/generators/joosy/templates/preload.html.haml +19 -0
  61. data/lib/rails/generators/joosy/widget_generator.rb +32 -0
  62. data/spec/javascripts/helpers/spec_helper.js.coffee +44 -0
  63. data/spec/javascripts/joosy/core/application_spec.js.coffee +40 -0
  64. data/spec/javascripts/joosy/core/form_spec.js.coffee +141 -0
  65. data/spec/javascripts/joosy/core/joosy_spec.js.coffee +72 -0
  66. data/spec/javascripts/joosy/core/layout_spec.js.coffee +50 -0
  67. data/spec/javascripts/joosy/core/modules/container_spec.js.coffee +92 -0
  68. data/spec/javascripts/joosy/core/modules/events_spec.js.coffee +53 -0
  69. data/spec/javascripts/joosy/core/modules/filters_spec.js.coffee +71 -0
  70. data/spec/javascripts/joosy/core/modules/log_spec.js.coffee +15 -0
  71. data/spec/javascripts/joosy/core/modules/module_spec.js.coffee +47 -0
  72. data/spec/javascripts/joosy/core/modules/renderer_spec.js.coffee +149 -0
  73. data/spec/javascripts/joosy/core/modules/time_manager_spec.js.coffee +25 -0
  74. data/spec/javascripts/joosy/core/modules/widget_manager_spec.js.coffee +75 -0
  75. data/spec/javascripts/joosy/core/page_spec.js.coffee +175 -0
  76. data/spec/javascripts/joosy/core/resource/generic_spec.js.coffee +35 -0
  77. data/spec/javascripts/joosy/core/resource/rest_collection_spec.js.coffee +65 -0
  78. data/spec/javascripts/joosy/core/resource/rest_spec.js.coffee +108 -0
  79. data/spec/javascripts/joosy/core/router_spec.js.coffee +123 -0
  80. data/spec/javascripts/joosy/core/templaters/rails_jst_spec.js.coffee +25 -0
  81. data/spec/javascripts/joosy/core/widget_spec.js.coffee +51 -0
  82. data/spec/javascripts/joosy/preloaders/caching_spec.js.coffee +36 -0
  83. data/spec/javascripts/joosy/preloaders/inline_spec.js.coffee +16 -0
  84. data/spec/javascripts/support/assets/coolface.jpg +0 -0
  85. data/spec/javascripts/support/assets/okay.jpg +0 -0
  86. data/spec/javascripts/support/assets/test.js +1 -0
  87. data/spec/javascripts/support/jasmine.yml +74 -0
  88. data/spec/javascripts/support/jasmine_config.rb +23 -0
  89. data/spec/javascripts/support/jasmine_runner.rb +32 -0
  90. data/spec/javascripts/support/sinon-1.3.1.js +3469 -0
  91. data/spec/javascripts/support/sinon-ie-1.3.1.js +82 -0
  92. data/vendor/assets/javascripts/base64.js +135 -0
  93. data/vendor/assets/javascripts/inflection.js +656 -0
  94. data/vendor/assets/javascripts/jquery.form.js +980 -0
  95. data/vendor/assets/javascripts/jquery.hashchange.js +390 -0
  96. data/vendor/assets/javascripts/metamorph.js +409 -0
  97. data/vendor/assets/javascripts/sugar.js +6040 -0
  98. metadata +232 -0
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Sinon.JS 1.3.1, 2012/01/04
3
+ *
4
+ * @author Christian Johansen (christian@cjohansen.no)
5
+ *
6
+ * (The BSD License)
7
+ *
8
+ * Copyright (c) 2010-2011, Christian Johansen, christian@cjohansen.no
9
+ * All rights reserved.
10
+ *
11
+ * Redistribution and use in source and binary forms, with or without modification,
12
+ * are permitted provided that the following conditions are met:
13
+ *
14
+ * * Redistributions of source code must retain the above copyright notice,
15
+ * this list of conditions and the following disclaimer.
16
+ * * Redistributions in binary form must reproduce the above copyright notice,
17
+ * this list of conditions and the following disclaimer in the documentation
18
+ * and/or other materials provided with the distribution.
19
+ * * Neither the name of Christian Johansen nor the names of his contributors
20
+ * may be used to endorse or promote products derived from this software
21
+ * without specific prior written permission.
22
+ *
23
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ */
34
+
35
+ "use strict";
36
+ /*global sinon, setTimeout, setInterval, clearTimeout, clearInterval, Date*/
37
+ /**
38
+ * Helps IE run the fake timers. By defining global functions, IE allows
39
+ * them to be overwritten at a later point. If these are not defined like
40
+ * this, overwriting them will result in anything from an exception to browser
41
+ * crash.
42
+ *
43
+ * If you don't require fake timers to work in IE, don't include this file.
44
+ *
45
+ * @author Christian Johansen (christian@cjohansen.no)
46
+ * @license BSD
47
+ *
48
+ * Copyright (c) 2010-2011 Christian Johansen
49
+ */
50
+ function setTimeout() {}
51
+ function clearTimeout() {}
52
+ function setInterval() {}
53
+ function clearInterval() {}
54
+ function Date() {}
55
+
56
+ // Reassign the original functions. Now their writable attribute
57
+ // should be true. Hackish, I know, but it works.
58
+ setTimeout = sinon.timers.setTimeout;
59
+ clearTimeout = sinon.timers.clearTimeout;
60
+ setInterval = sinon.timers.setInterval;
61
+ clearInterval = sinon.timers.clearInterval;
62
+ Date = sinon.timers.Date;
63
+
64
+ /*global sinon*/
65
+ /**
66
+ * Helps IE run the fake XMLHttpRequest. By defining global functions, IE allows
67
+ * them to be overwritten at a later point. If these are not defined like
68
+ * this, overwriting them will result in anything from an exception to browser
69
+ * crash.
70
+ *
71
+ * If you don't require fake XHR to work in IE, don't include this file.
72
+ *
73
+ * @author Christian Johansen (christian@cjohansen.no)
74
+ * @license BSD
75
+ *
76
+ * Copyright (c) 2010-2011 Christian Johansen
77
+ */
78
+ function XMLHttpRequest() {}
79
+
80
+ // Reassign the original function. Now its writable attribute
81
+ // should be true. Hackish, I know, but it works.
82
+ XMLHttpRequest = sinon.xhr.XMLHttpRequest || undefined;
@@ -0,0 +1,135 @@
1
+ var Base64 = {
2
+
3
+ // private property
4
+ _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
5
+
6
+ // public method for encoding
7
+ encode : function (input) {
8
+ var output = "";
9
+ var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
10
+ var i = 0;
11
+
12
+ input = Base64._utf8_encode(input);
13
+
14
+ while (i < input.length) {
15
+
16
+ chr1 = input.charCodeAt(i++);
17
+ chr2 = input.charCodeAt(i++);
18
+ chr3 = input.charCodeAt(i++);
19
+
20
+ enc1 = chr1 >> 2;
21
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
22
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
23
+ enc4 = chr3 & 63;
24
+
25
+ if (isNaN(chr2)) {
26
+ enc3 = enc4 = 64;
27
+ } else if (isNaN(chr3)) {
28
+ enc4 = 64;
29
+ }
30
+
31
+ output = output +
32
+ this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
33
+ this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
34
+
35
+ }
36
+
37
+ return output;
38
+ },
39
+
40
+ // public method for decoding
41
+ decode : function (input) {
42
+ var output = "";
43
+ var chr1, chr2, chr3;
44
+ var enc1, enc2, enc3, enc4;
45
+ var i = 0;
46
+
47
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
48
+
49
+ while (i < input.length) {
50
+
51
+ enc1 = this._keyStr.indexOf(input.charAt(i++));
52
+ enc2 = this._keyStr.indexOf(input.charAt(i++));
53
+ enc3 = this._keyStr.indexOf(input.charAt(i++));
54
+ enc4 = this._keyStr.indexOf(input.charAt(i++));
55
+
56
+ chr1 = (enc1 << 2) | (enc2 >> 4);
57
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
58
+ chr3 = ((enc3 & 3) << 6) | enc4;
59
+
60
+ output = output + String.fromCharCode(chr1);
61
+
62
+ if (enc3 != 64) {
63
+ output = output + String.fromCharCode(chr2);
64
+ }
65
+ if (enc4 != 64) {
66
+ output = output + String.fromCharCode(chr3);
67
+ }
68
+
69
+ }
70
+
71
+ output = Base64._utf8_decode(output);
72
+
73
+ return output;
74
+
75
+ },
76
+
77
+ // private method for UTF-8 encoding
78
+ _utf8_encode : function (string) {
79
+ string = string.replace(/\r\n/g,"\n");
80
+ var utftext = "";
81
+
82
+ for (var n = 0; n < string.length; n++) {
83
+
84
+ var c = string.charCodeAt(n);
85
+
86
+ if (c < 128) {
87
+ utftext += String.fromCharCode(c);
88
+ }
89
+ else if((c > 127) && (c < 2048)) {
90
+ utftext += String.fromCharCode((c >> 6) | 192);
91
+ utftext += String.fromCharCode((c & 63) | 128);
92
+ }
93
+ else {
94
+ utftext += String.fromCharCode((c >> 12) | 224);
95
+ utftext += String.fromCharCode(((c >> 6) & 63) | 128);
96
+ utftext += String.fromCharCode((c & 63) | 128);
97
+ }
98
+
99
+ }
100
+
101
+ return utftext;
102
+ },
103
+
104
+ // private method for UTF-8 decoding
105
+ _utf8_decode : function (utftext) {
106
+ var string = "";
107
+ var i = 0;
108
+ var c = c1 = c2 = 0;
109
+
110
+ while ( i < utftext.length ) {
111
+
112
+ c = utftext.charCodeAt(i);
113
+
114
+ if (c < 128) {
115
+ string += String.fromCharCode(c);
116
+ i++;
117
+ }
118
+ else if((c > 191) && (c < 224)) {
119
+ c2 = utftext.charCodeAt(i+1);
120
+ string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
121
+ i += 2;
122
+ }
123
+ else {
124
+ c2 = utftext.charCodeAt(i+1);
125
+ c3 = utftext.charCodeAt(i+2);
126
+ string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
127
+ i += 3;
128
+ }
129
+
130
+ }
131
+
132
+ return string;
133
+ }
134
+
135
+ }
@@ -0,0 +1,656 @@
1
+ /*
2
+ Copyright (c) 2010 Ryan Schuft (ryan.schuft@gmail.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
23
+ /*
24
+ This code is based in part on the work done in Ruby to support
25
+ infection as part of Ruby on Rails in the ActiveSupport's Inflector
26
+ and Inflections classes. It was initally ported to Javascript by
27
+ Ryan Schuft (ryan.schuft@gmail.com) in 2007.
28
+
29
+ The code is available at http://code.google.com/p/inflection-js/
30
+
31
+ The basic usage is:
32
+ 1. Include this script on your web page.
33
+ 2. Call functions on any String object in Javascript
34
+
35
+ Currently implemented functions:
36
+
37
+ String.pluralize(plural) == String
38
+ renders a singular English language noun into its plural form
39
+ normal results can be overridden by passing in an alternative
40
+
41
+ String.singularize(singular) == String
42
+ renders a plural English language noun into its singular form
43
+ normal results can be overridden by passing in an alterative
44
+
45
+ String.camelize(lowFirstLetter) == String
46
+ renders a lower case underscored word into camel case
47
+ the first letter of the result will be upper case unless you pass true
48
+ also translates "/" into "::" (underscore does the opposite)
49
+
50
+ String.underscore() == String
51
+ renders a camel cased word into words seperated by underscores
52
+ also translates "::" back into "/" (camelize does the opposite)
53
+
54
+ String.humanize(lowFirstLetter) == String
55
+ renders a lower case and underscored word into human readable form
56
+ defaults to making the first letter capitalized unless you pass true
57
+
58
+ String.capitalize() == String
59
+ renders all characters to lower case and then makes the first upper
60
+
61
+ String.dasherize() == String
62
+ renders all underbars and spaces as dashes
63
+
64
+ String.titleize() == String
65
+ renders words into title casing (as for book titles)
66
+
67
+ String.demodulize() == String
68
+ renders class names that are prepended by modules into just the class
69
+
70
+ String.tableize() == String
71
+ renders camel cased singular words into their underscored plural form
72
+
73
+ String.classify() == String
74
+ renders an underscored plural word into its camel cased singular form
75
+
76
+ String.foreign_key(dropIdUbar) == String
77
+ renders a class name (camel cased singular noun) into a foreign key
78
+ defaults to seperating the class from the id with an underbar unless
79
+ you pass true
80
+
81
+ String.ordinalize() == String
82
+ renders all numbers found in the string into their sequence like "22nd"
83
+ */
84
+
85
+ /*
86
+ This sets up a container for some constants in its own namespace
87
+ We use the window (if available) to enable dynamic loading of this script
88
+ Window won't necessarily exist for non-browsers.
89
+ */
90
+ if (window && !window.InflectionJS)
91
+ {
92
+ window.InflectionJS = null;
93
+ }
94
+
95
+ /*
96
+ This sets up some constants for later use
97
+ This should use the window namespace variable if available
98
+ */
99
+ InflectionJS =
100
+ {
101
+ /*
102
+ This is a list of nouns that use the same form for both singular and plural.
103
+ This list should remain entirely in lower case to correctly match Strings.
104
+ */
105
+ uncountable_words: [
106
+ 'equipment', 'information', 'rice', 'money', 'species', 'series',
107
+ 'fish', 'sheep', 'moose', 'deer', 'news'
108
+ ],
109
+
110
+ /*
111
+ These rules translate from the singular form of a noun to its plural form.
112
+ */
113
+ plural_rules: [
114
+ [new RegExp('(m)an$', 'gi'), '$1en'],
115
+ [new RegExp('(pe)rson$', 'gi'), '$1ople'],
116
+ [new RegExp('(child)$', 'gi'), '$1ren'],
117
+ [new RegExp('^(ox)$', 'gi'), '$1en'],
118
+ [new RegExp('(ax|test)is$', 'gi'), '$1es'],
119
+ [new RegExp('(octop|vir)us$', 'gi'), '$1i'],
120
+ [new RegExp('(alias|status)$', 'gi'), '$1es'],
121
+ [new RegExp('(bu)s$', 'gi'), '$1ses'],
122
+ [new RegExp('(buffal|tomat|potat)o$', 'gi'), '$1oes'],
123
+ [new RegExp('([ti])um$', 'gi'), '$1a'],
124
+ [new RegExp('sis$', 'gi'), 'ses'],
125
+ [new RegExp('(?:([^f])fe|([lr])f)$', 'gi'), '$1$2ves'],
126
+ [new RegExp('(hive)$', 'gi'), '$1s'],
127
+ [new RegExp('([^aeiouy]|qu)y$', 'gi'), '$1ies'],
128
+ [new RegExp('(x|ch|ss|sh)$', 'gi'), '$1es'],
129
+ [new RegExp('(matr|vert|ind)ix|ex$', 'gi'), '$1ices'],
130
+ [new RegExp('([m|l])ouse$', 'gi'), '$1ice'],
131
+ [new RegExp('(quiz)$', 'gi'), '$1zes'],
132
+ [new RegExp('s$', 'gi'), 's'],
133
+ [new RegExp('$', 'gi'), 's']
134
+ ],
135
+
136
+ /*
137
+ These rules translate from the plural form of a noun to its singular form.
138
+ */
139
+ singular_rules: [
140
+ [new RegExp('(m)en$', 'gi'), '$1an'],
141
+ [new RegExp('(pe)ople$', 'gi'), '$1rson'],
142
+ [new RegExp('(child)ren$', 'gi'), '$1'],
143
+ [new RegExp('([ti])a$', 'gi'), '$1um'],
144
+ [new RegExp('((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$','gi'), '$1$2sis'],
145
+ [new RegExp('(hive)s$', 'gi'), '$1'],
146
+ [new RegExp('(tive)s$', 'gi'), '$1'],
147
+ [new RegExp('(curve)s$', 'gi'), '$1'],
148
+ [new RegExp('([lr])ves$', 'gi'), '$1f'],
149
+ [new RegExp('([^fo])ves$', 'gi'), '$1fe'],
150
+ [new RegExp('([^aeiouy]|qu)ies$', 'gi'), '$1y'],
151
+ [new RegExp('(s)eries$', 'gi'), '$1eries'],
152
+ [new RegExp('(m)ovies$', 'gi'), '$1ovie'],
153
+ [new RegExp('(x|ch|ss|sh)es$', 'gi'), '$1'],
154
+ [new RegExp('([m|l])ice$', 'gi'), '$1ouse'],
155
+ [new RegExp('(bus)es$', 'gi'), '$1'],
156
+ [new RegExp('(o)es$', 'gi'), '$1'],
157
+ [new RegExp('(shoe)s$', 'gi'), '$1'],
158
+ [new RegExp('(cris|ax|test)es$', 'gi'), '$1is'],
159
+ [new RegExp('(octop|vir)i$', 'gi'), '$1us'],
160
+ [new RegExp('(alias|status)es$', 'gi'), '$1'],
161
+ [new RegExp('^(ox)en', 'gi'), '$1'],
162
+ [new RegExp('(vert|ind)ices$', 'gi'), '$1ex'],
163
+ [new RegExp('(matr)ices$', 'gi'), '$1ix'],
164
+ [new RegExp('(quiz)zes$', 'gi'), '$1'],
165
+ [new RegExp('s$', 'gi'), '']
166
+ ],
167
+
168
+ /*
169
+ This is a list of words that should not be capitalized for title case
170
+ */
171
+ non_titlecased_words: [
172
+ 'and', 'or', 'nor', 'a', 'an', 'the', 'so', 'but', 'to', 'of', 'at',
173
+ 'by', 'from', 'into', 'on', 'onto', 'off', 'out', 'in', 'over',
174
+ 'with', 'for'
175
+ ],
176
+
177
+ /*
178
+ These are regular expressions used for converting between String formats
179
+ */
180
+ id_suffix: new RegExp('(_ids|_id)$', 'g'),
181
+ underbar: new RegExp('_', 'g'),
182
+ space_or_underbar: new RegExp('[\ _]', 'g'),
183
+ uppercase: new RegExp('([A-Z])', 'g'),
184
+ underbar_prefix: new RegExp('^_'),
185
+
186
+ /*
187
+ This is a helper method that applies rules based replacement to a String
188
+ Signature:
189
+ InflectionJS.apply_rules(str, rules, skip, override) == String
190
+ Arguments:
191
+ str - String - String to modify and return based on the passed rules
192
+ rules - Array: [RegExp, String] - Regexp to match paired with String to use for replacement
193
+ skip - Array: [String] - Strings to skip if they match
194
+ override - String (optional) - String to return as though this method succeeded (used to conform to APIs)
195
+ Returns:
196
+ String - passed String modified by passed rules
197
+ Examples:
198
+ InflectionJS.apply_rules("cows", InflectionJs.singular_rules) === 'cow'
199
+ */
200
+ apply_rules: function(str, rules, skip, override)
201
+ {
202
+ if (override)
203
+ {
204
+ str = override;
205
+ }
206
+ else
207
+ {
208
+ var ignore = (skip.indexOf(str.toLowerCase()) > -1);
209
+ if (!ignore)
210
+ {
211
+ for (var x = 0; x < rules.length; x++)
212
+ {
213
+ if (str.match(rules[x][0]))
214
+ {
215
+ str = str.replace(rules[x][0], rules[x][1]);
216
+ break;
217
+ }
218
+ }
219
+ }
220
+ }
221
+ return str;
222
+ }
223
+ };
224
+
225
+ /*
226
+ This lets us detect if an Array contains a given element
227
+ Signature:
228
+ Array.indexOf(item, fromIndex, compareFunc) == Integer
229
+ Arguments:
230
+ item - Object - object to locate in the Array
231
+ fromIndex - Integer (optional) - starts checking from this position in the Array
232
+ compareFunc - Function (optional) - function used to compare Array item vs passed item
233
+ Returns:
234
+ Integer - index position in the Array of the passed item
235
+ Examples:
236
+ ['hi','there'].indexOf("guys") === -1
237
+ ['hi','there'].indexOf("hi") === 0
238
+ */
239
+ if (!Array.prototype.indexOf)
240
+ {
241
+ Array.prototype.indexOf = function(item, fromIndex, compareFunc)
242
+ {
243
+ if (!fromIndex)
244
+ {
245
+ fromIndex = -1;
246
+ }
247
+ var index = -1;
248
+ for (var i = fromIndex; i < this.length; i++)
249
+ {
250
+ if (this[i] === item || compareFunc && compareFunc(this[i], item))
251
+ {
252
+ index = i;
253
+ break;
254
+ }
255
+ }
256
+ return index;
257
+ };
258
+ }
259
+
260
+ /*
261
+ You can override this list for all Strings or just one depending on if you
262
+ set the new values on prototype or on a given String instance.
263
+ */
264
+ if (!String.prototype._uncountable_words)
265
+ {
266
+ String.prototype._uncountable_words = InflectionJS.uncountable_words;
267
+ }
268
+
269
+ /*
270
+ You can override this list for all Strings or just one depending on if you
271
+ set the new values on prototype or on a given String instance.
272
+ */
273
+ if (!String.prototype._plural_rules)
274
+ {
275
+ String.prototype._plural_rules = InflectionJS.plural_rules;
276
+ }
277
+
278
+ /*
279
+ You can override this list for all Strings or just one depending on if you
280
+ set the new values on prototype or on a given String instance.
281
+ */
282
+ if (!String.prototype._singular_rules)
283
+ {
284
+ String.prototype._singular_rules = InflectionJS.singular_rules;
285
+ }
286
+
287
+ /*
288
+ You can override this list for all Strings or just one depending on if you
289
+ set the new values on prototype or on a given String instance.
290
+ */
291
+ if (!String.prototype._non_titlecased_words)
292
+ {
293
+ String.prototype._non_titlecased_words = InflectionJS.non_titlecased_words;
294
+ }
295
+
296
+ /*
297
+ This function adds plurilization support to every String object
298
+ Signature:
299
+ String.pluralize(plural) == String
300
+ Arguments:
301
+ plural - String (optional) - overrides normal output with said String
302
+ Returns:
303
+ String - singular English language nouns are returned in plural form
304
+ Examples:
305
+ "person".pluralize() == "people"
306
+ "octopus".pluralize() == "octopi"
307
+ "Hat".pluralize() == "Hats"
308
+ "person".pluralize("guys") == "guys"
309
+ */
310
+ if (!String.prototype.pluralize)
311
+ {
312
+ String.prototype.pluralize = function(plural)
313
+ {
314
+ return InflectionJS.apply_rules(
315
+ this,
316
+ this._plural_rules,
317
+ this._uncountable_words,
318
+ plural
319
+ );
320
+ };
321
+ }
322
+
323
+ /*
324
+ This function adds singularization support to every String object
325
+ Signature:
326
+ String.singularize(singular) == String
327
+ Arguments:
328
+ singular - String (optional) - overrides normal output with said String
329
+ Returns:
330
+ String - plural English language nouns are returned in singular form
331
+ Examples:
332
+ "people".singularize() == "person"
333
+ "octopi".singularize() == "octopus"
334
+ "Hats".singularize() == "Hat"
335
+ "guys".singularize("person") == "person"
336
+ */
337
+ if (!String.prototype.singularize)
338
+ {
339
+ String.prototype.singularize = function(singular)
340
+ {
341
+ return InflectionJS.apply_rules(
342
+ this,
343
+ this._singular_rules,
344
+ this._uncountable_words,
345
+ singular
346
+ );
347
+ };
348
+ }
349
+
350
+ /*
351
+ This function adds camelization support to every String object
352
+ Signature:
353
+ String.camelize(lowFirstLetter) == String
354
+ Arguments:
355
+ lowFirstLetter - boolean (optional) - default is to capitalize the first
356
+ letter of the results... passing true will lowercase it
357
+ Returns:
358
+ String - lower case underscored words will be returned in camel case
359
+ additionally '/' is translated to '::'
360
+ Examples:
361
+ "message_properties".camelize() == "MessageProperties"
362
+ "message_properties".camelize(true) == "messageProperties"
363
+ */
364
+ if (!String.prototype.camelize)
365
+ {
366
+ String.prototype.camelize = function(lowFirstLetter)
367
+ {
368
+ var str = this.toLowerCase();
369
+ var str_path = str.split('/');
370
+ for (var i = 0; i < str_path.length; i++)
371
+ {
372
+ var str_arr = str_path[i].split('_');
373
+ var initX = ((lowFirstLetter && i + 1 === str_path.length) ? (1) : (0));
374
+ for (var x = initX; x < str_arr.length; x++)
375
+ {
376
+ str_arr[x] = str_arr[x].charAt(0).toUpperCase() + str_arr[x].substring(1);
377
+ }
378
+ str_path[i] = str_arr.join('');
379
+ }
380
+ str = str_path.join('::');
381
+ return str;
382
+ };
383
+ }
384
+
385
+ /*
386
+ This function adds underscore support to every String object
387
+ Signature:
388
+ String.underscore() == String
389
+ Arguments:
390
+ N/A
391
+ Returns:
392
+ String - camel cased words are returned as lower cased and underscored
393
+ additionally '::' is translated to '/'
394
+ Examples:
395
+ "MessageProperties".camelize() == "message_properties"
396
+ "messageProperties".underscore() == "message_properties"
397
+ */
398
+ if (!String.prototype.underscore)
399
+ {
400
+ String.prototype.underscore = function()
401
+ {
402
+ var str = this;
403
+ var str_path = str.split('::');
404
+ for (var i = 0; i < str_path.length; i++)
405
+ {
406
+ str_path[i] = str_path[i].replace(InflectionJS.uppercase, '_$1');
407
+ str_path[i] = str_path[i].replace(InflectionJS.underbar_prefix, '');
408
+ }
409
+ str = str_path.join('/').toLowerCase();
410
+ return str;
411
+ };
412
+ }
413
+
414
+ /*
415
+ This function adds humanize support to every String object
416
+ Signature:
417
+ String.humanize(lowFirstLetter) == String
418
+ Arguments:
419
+ lowFirstLetter - boolean (optional) - default is to capitalize the first
420
+ letter of the results... passing true will lowercase it
421
+ Returns:
422
+ String - lower case underscored words will be returned in humanized form
423
+ Examples:
424
+ "message_properties".humanize() == "Message properties"
425
+ "message_properties".humanize(true) == "message properties"
426
+ */
427
+ if (!String.prototype.humanize)
428
+ {
429
+ String.prototype.humanize = function(lowFirstLetter)
430
+ {
431
+ var str = this.toLowerCase();
432
+ str = str.replace(InflectionJS.id_suffix, '');
433
+ str = str.replace(InflectionJS.underbar, ' ');
434
+ if (!lowFirstLetter)
435
+ {
436
+ str = str.capitalize();
437
+ }
438
+ return str;
439
+ };
440
+ }
441
+
442
+ /*
443
+ This function adds capitalization support to every String object
444
+ Signature:
445
+ String.capitalize() == String
446
+ Arguments:
447
+ N/A
448
+ Returns:
449
+ String - all characters will be lower case and the first will be upper
450
+ Examples:
451
+ "message_properties".capitalize() == "Message_properties"
452
+ "message properties".capitalize() == "Message properties"
453
+ */
454
+ if (!String.prototype.capitalize)
455
+ {
456
+ String.prototype.capitalize = function()
457
+ {
458
+ var str = this.toLowerCase();
459
+ str = str.substring(0, 1).toUpperCase() + str.substring(1);
460
+ return str;
461
+ };
462
+ }
463
+
464
+ /*
465
+ This function adds dasherization support to every String object
466
+ Signature:
467
+ String.dasherize() == String
468
+ Arguments:
469
+ N/A
470
+ Returns:
471
+ String - replaces all spaces or underbars with dashes
472
+ Examples:
473
+ "message_properties".capitalize() == "message-properties"
474
+ "Message Properties".capitalize() == "Message-Properties"
475
+ */
476
+ if (!String.prototype.dasherize)
477
+ {
478
+ String.prototype.dasherize = function()
479
+ {
480
+ var str = this;
481
+ str = str.replace(InflectionJS.space_or_underbar, '-');
482
+ return str;
483
+ };
484
+ }
485
+
486
+ /*
487
+ This function adds titleize support to every String object
488
+ Signature:
489
+ String.titleize() == String
490
+ Arguments:
491
+ N/A
492
+ Returns:
493
+ String - capitalizes words as you would for a book title
494
+ Examples:
495
+ "message_properties".titleize() == "Message Properties"
496
+ "message properties to keep".titleize() == "Message Properties to Keep"
497
+ */
498
+ if (!String.prototype.titleize)
499
+ {
500
+ String.prototype.titleize = function()
501
+ {
502
+ var str = this.toLowerCase();
503
+ str = str.replace(InflectionJS.underbar, ' ');
504
+ var str_arr = str.split(' ');
505
+ for (var x = 0; x < str_arr.length; x++)
506
+ {
507
+ var d = str_arr[x].split('-');
508
+ for (var i = 0; i < d.length; i++)
509
+ {
510
+ if (this._non_titlecased_words.indexOf(d[i].toLowerCase()) < 0)
511
+ {
512
+ d[i] = d[i].capitalize();
513
+ }
514
+ }
515
+ str_arr[x] = d.join('-');
516
+ }
517
+ str = str_arr.join(' ');
518
+ str = str.substring(0, 1).toUpperCase() + str.substring(1);
519
+ return str;
520
+ };
521
+ }
522
+
523
+ /*
524
+ This function adds demodulize support to every String object
525
+ Signature:
526
+ String.demodulize() == String
527
+ Arguments:
528
+ N/A
529
+ Returns:
530
+ String - removes module names leaving only class names (Ruby style)
531
+ Examples:
532
+ "Message::Bus::Properties".demodulize() == "Properties"
533
+ */
534
+ if (!String.prototype.demodulize)
535
+ {
536
+ String.prototype.demodulize = function()
537
+ {
538
+ var str = this;
539
+ var str_arr = str.split('::');
540
+ str = str_arr[str_arr.length - 1];
541
+ return str;
542
+ };
543
+ }
544
+
545
+ /*
546
+ This function adds tableize support to every String object
547
+ Signature:
548
+ String.tableize() == String
549
+ Arguments:
550
+ N/A
551
+ Returns:
552
+ String - renders camel cased words into their underscored plural form
553
+ Examples:
554
+ "MessageBusProperty".tableize() == "message_bus_properties"
555
+ */
556
+ if (!String.prototype.tableize)
557
+ {
558
+ String.prototype.tableize = function()
559
+ {
560
+ var str = this;
561
+ str = str.underscore().pluralize();
562
+ return str;
563
+ };
564
+ }
565
+
566
+ /*
567
+ This function adds classification support to every String object
568
+ Signature:
569
+ String.classify() == String
570
+ Arguments:
571
+ N/A
572
+ Returns:
573
+ String - underscored plural nouns become the camel cased singular form
574
+ Examples:
575
+ "message_bus_properties".classify() == "MessageBusProperty"
576
+ */
577
+ if (!String.prototype.classify)
578
+ {
579
+ String.prototype.classify = function()
580
+ {
581
+ var str = this;
582
+ str = str.camelize().singularize();
583
+ return str;
584
+ };
585
+ }
586
+
587
+ /*
588
+ This function adds foreign key support to every String object
589
+ Signature:
590
+ String.foreign_key(dropIdUbar) == String
591
+ Arguments:
592
+ dropIdUbar - boolean (optional) - default is to seperate id with an
593
+ underbar at the end of the class name, you can pass true to skip it
594
+ Returns:
595
+ String - camel cased singular class names become underscored with id
596
+ Examples:
597
+ "MessageBusProperty".foreign_key() == "message_bus_property_id"
598
+ "MessageBusProperty".foreign_key(true) == "message_bus_propertyid"
599
+ */
600
+ if (!String.prototype.foreign_key)
601
+ {
602
+ String.prototype.foreign_key = function(dropIdUbar)
603
+ {
604
+ var str = this;
605
+ str = str.demodulize().underscore() + ((dropIdUbar) ? ('') : ('_')) + 'id';
606
+ return str;
607
+ };
608
+ }
609
+
610
+ /*
611
+ This function adds ordinalize support to every String object
612
+ Signature:
613
+ String.ordinalize() == String
614
+ Arguments:
615
+ N/A
616
+ Returns:
617
+ String - renders all found numbers their sequence like "22nd"
618
+ Examples:
619
+ "the 1 pitch".ordinalize() == "the 1st pitch"
620
+ */
621
+ if (!String.prototype.ordinalize)
622
+ {
623
+ String.prototype.ordinalize = function()
624
+ {
625
+ var str = this;
626
+ var str_arr = str.split(' ');
627
+ for (var x = 0; x < str_arr.length; x++)
628
+ {
629
+ var i = parseInt(str_arr[x]);
630
+ if (i === NaN)
631
+ {
632
+ var ltd = str_arr[x].substring(str_arr[x].length - 2);
633
+ var ld = str_arr[x].substring(str_arr[x].length - 1);
634
+ var suf = "th";
635
+ if (ltd != "11" && ltd != "12" && ltd != "13")
636
+ {
637
+ if (ld === "1")
638
+ {
639
+ suf = "st";
640
+ }
641
+ else if (ld === "2")
642
+ {
643
+ suf = "nd";
644
+ }
645
+ else if (ld === "3")
646
+ {
647
+ suf = "rd";
648
+ }
649
+ }
650
+ str_arr[x] += suf;
651
+ }
652
+ }
653
+ str = str_arr.join(' ');
654
+ return str;
655
+ };
656
+ }