micro-rails 0.1.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c88558102874866560fd567001b83370fcd0b4a
4
+ data.tar.gz: 9dbaf135cbda3e86067ade9da53625a934350d20
5
+ SHA512:
6
+ metadata.gz: c4c2f58ad7e3cd30a144b6cbf5ee2f192caec0015d1d3080d56b4517296a16ccd46d0c7bc099a0ddc8b71d36258315ab3febd1453f0b5add7f7e247b10110b92
7
+ data.tar.gz: be97b232c2919d48f2a41467bb32b65e2ed779bf889b6d5620c555b8f522fc2593f36aa29666433fb91aef32ee002e739817abf1c4a6cd1cd16a3d5c53e9846f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in micro-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jérôme Guyon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Micro::Rails
2
+
3
+ This gem ships the javascript library **Micro**, a micro library helping to structure your client code by using the concept of components.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'micro-rails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install micro-rails
20
+
21
+ Then in your application.js file, add:
22
+
23
+ ```javascript
24
+ //= require jquery
25
+ //= require underscore
26
+ //= require micro
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ See [the Micro README](https://github.com/jguyon/micro/blob/master/README.md).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Download micro.js in vendor/assets/javascripts'
4
+ task 'download' do
5
+ puts 'Downloading micro.js'
6
+ puts `curl -o vendor/assets/javascripts/micro.js https://raw.githubusercontent.com/jguyon/micro/v#{Micro::Rails::MICRO_VERSION}/lib/micro.js`
7
+ end
@@ -0,0 +1,6 @@
1
+ module Micro
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Micro
2
+ module Rails
3
+ VERSION = "0.1.0-alpha"
4
+ MICRO_VERSION = VERSION
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "micro/rails/version"
2
+
3
+ module Micro
4
+ module Rails
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ require 'micro/rails'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'micro/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "micro-rails"
8
+ spec.version = Micro::Rails::VERSION
9
+ spec.authors = ["Jérôme Guyon"]
10
+ spec.email = ["jeromep.guyon@gmail.com"]
11
+ spec.summary = %q{Ships the javascript library Micro for the Rails asset-pipeline.'}
12
+ spec.homepage = "https://github.com/jguyon/micro-rails"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "jquery-rails", ">= 4.0"
21
+ spec.add_dependency "underscore-rails", ">= 1.7"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,865 @@
1
+ // Generated by CoffeeScript 1.9.0
2
+ (function() {
3
+ var Component, Factory, Module, Namespace, Unit, factory, splitOnce, splitUnitName, µ,
4
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
5
+ __hasProp = {}.hasOwnProperty,
6
+ __slice = [].slice;
7
+
8
+ splitOnce = function(string, char) {
9
+ var splits;
10
+ splits = string.split(char);
11
+ return [splits.shift(), splits.join(char)];
12
+ };
13
+
14
+ splitUnitName = function(name) {
15
+ var label, namespace, _ref;
16
+ _ref = splitOnce(name, '.'), namespace = _ref[0], label = _ref[1];
17
+ if (!label) {
18
+ label = namespace;
19
+ namespace = factory.config.defaultNamespace;
20
+ }
21
+ return [namespace, label];
22
+ };
23
+
24
+ Factory = (function() {
25
+ function Factory() {
26
+ this.config = {
27
+ defaultNamespace: 'app',
28
+ dataPrefix: 'micro',
29
+ methodPrefix: 'µ'
30
+ };
31
+ this._namespaces = {};
32
+ this._initialize = [];
33
+ this._destroy = [];
34
+ }
35
+
36
+ Factory.prototype.configure = function(config) {
37
+ return _.extend(this.config, config);
38
+ };
39
+
40
+ Factory.prototype.setup = function(setup) {
41
+ return setup.call({
42
+ initialize: (function(_this) {
43
+ return function(initialize) {
44
+ return _this._initialize.push(initialize);
45
+ };
46
+ })(this),
47
+ destroy: (function(_this) {
48
+ return function(destroy) {
49
+ return _this._destroy.unshift(destroy);
50
+ };
51
+ })(this),
52
+ component: (function(_this) {
53
+ return function(name, selector, config) {
54
+ var Component;
55
+ if (!(_.isString(selector) || _.isElement(selector))) {
56
+ config = selector;
57
+ selector = null;
58
+ }
59
+ Component = _this.component(name);
60
+ selector || (selector = Component.config.defaultSelector);
61
+ if (!selector) {
62
+ throw new Error('you must provide a selector or define defaultSelector in the component configuration');
63
+ }
64
+ _this._initialize.push(function() {
65
+ Component.initialize(this.filter(selector), config);
66
+ return Component.initialize(this.find(selector), config);
67
+ });
68
+ return _this._destroy.unshift(function() {
69
+ Component.destroy(this.filter(selector));
70
+ return Component.destroy(this.find(selector));
71
+ });
72
+ };
73
+ })(this)
74
+ });
75
+ };
76
+
77
+ Factory.prototype.initialize = function(el) {
78
+ var $el, initialize, _i, _len, _ref, _results;
79
+ if (el) {
80
+ $el = $(el);
81
+ } else {
82
+ $el = $(document);
83
+ }
84
+ _ref = this._initialize;
85
+ _results = [];
86
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
87
+ initialize = _ref[_i];
88
+ _results.push(initialize.call($el));
89
+ }
90
+ return _results;
91
+ };
92
+
93
+ Factory.prototype.destroy = function(el) {
94
+ var $el, destroy, _i, _len, _ref, _results;
95
+ if (el) {
96
+ $el = $(el);
97
+ } else {
98
+ $el = $(this.config.rootElement);
99
+ }
100
+ _ref = this._destroy;
101
+ _results = [];
102
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
103
+ destroy = _ref[_i];
104
+ _results.push(destroy.call($el));
105
+ }
106
+ return _results;
107
+ };
108
+
109
+ Factory.prototype.clean = function() {
110
+ µ.destroy();
111
+ this._namespaces = {};
112
+ this._initialize = [];
113
+ return this._destroy = [];
114
+ };
115
+
116
+ Factory.prototype.namespace = function(label, doNotRaise) {
117
+ var namespace;
118
+ if (doNotRaise == null) {
119
+ doNotRaise = false;
120
+ }
121
+ if (!((namespace = this._namespaces[label]) || doNotRaise)) {
122
+ throw new Error("could not find namespace " + label);
123
+ }
124
+ return namespace;
125
+ };
126
+
127
+ Factory.prototype.component = function(name, doNotRaise) {
128
+ var label, namespace, _ref;
129
+ if (doNotRaise == null) {
130
+ doNotRaise = false;
131
+ }
132
+ _ref = splitUnitName(name), namespace = _ref[0], label = _ref[1];
133
+ return this.namespace(namespace, doNotRaise).component(label, doNotRaise);
134
+ };
135
+
136
+ Factory.prototype.module = function(name, doNotRaise) {
137
+ var label, namespace, _ref;
138
+ if (doNotRaise == null) {
139
+ doNotRaise = false;
140
+ }
141
+ _ref = splitUnitName(name), namespace = _ref[0], label = _ref[1];
142
+ return this.namespace(namespace, doNotRaise).module(label, doNotRaise);
143
+ };
144
+
145
+ Factory.prototype.createNamespace = function(label) {
146
+ if (this._namespaces[label]) {
147
+ throw new Error("namespace " + label + " already exists");
148
+ }
149
+ return this._namespaces[label] = new Namespace(this, label);
150
+ };
151
+
152
+ Factory.prototype.data = function(el, name, value) {
153
+ if (arguments.length < 3) {
154
+ return $(el).data(this._dataName(name));
155
+ } else {
156
+ return $(el).data(this._dataName(name), value);
157
+ }
158
+ };
159
+
160
+ Factory.prototype.removeData = function(el, name) {
161
+ return $(el).removeData(this._dataName(name));
162
+ };
163
+
164
+ Factory.prototype._dataName = function(name) {
165
+ return this.config.dataPrefix + "-" + name;
166
+ };
167
+
168
+ return Factory;
169
+
170
+ })();
171
+
172
+ Namespace = (function() {
173
+ Namespace.labelFormat = /^[a-z][a-zA-Z0-9]*$/;
174
+
175
+ Namespace.prototype.µObject = function() {
176
+ return {
177
+ configure: (function(_this) {
178
+ return function(config) {
179
+ _this.configure(config);
180
+ return _this.µObject();
181
+ };
182
+ })(this)
183
+ };
184
+ };
185
+
186
+ function Namespace(_at_factory, _at_label) {
187
+ this.factory = _at_factory;
188
+ this.label = _at_label;
189
+ if (!this.label.match(this.constructor.labelFormat)) {
190
+ throw new Error(this.label + " is not a valid namespace name");
191
+ }
192
+ this._components = {};
193
+ this._modules = {};
194
+ this.config = {};
195
+ }
196
+
197
+ Namespace.prototype.configure = function(config) {
198
+ if (config == null) {
199
+ config = {};
200
+ }
201
+ return _.extend(this.config, config);
202
+ };
203
+
204
+ Namespace.prototype.component = function(label, doNotRaise) {
205
+ var Component;
206
+ if (doNotRaise == null) {
207
+ doNotRaise = false;
208
+ }
209
+ if (!((Component = this._components[label]) || doNotRaise)) {
210
+ throw new Error("component " + label + " could not be found in namespace " + this.name);
211
+ }
212
+ return Component;
213
+ };
214
+
215
+ Namespace.prototype.module = function(label, doNotRaise) {
216
+ var Module;
217
+ if (doNotRaise == null) {
218
+ doNotRaise = false;
219
+ }
220
+ if (!((Module = this._modules[label]) || doNotRaise)) {
221
+ throw new Error("module " + label + " could not be found in namespace " + this.name);
222
+ }
223
+ return Module;
224
+ };
225
+
226
+ Namespace.prototype.addUnit = function(Unit) {
227
+ var prototype;
228
+ prototype = Unit.prototype;
229
+ if (prototype instanceof Component) {
230
+ return this._addComponent(Unit);
231
+ } else if (prototype instanceof Module) {
232
+ return this._addModule(Unit);
233
+ } else {
234
+ throw new Error("the unit must be a module or a component");
235
+ }
236
+ };
237
+
238
+ Namespace.prototype._addComponent = function(Component) {
239
+ Component.namespace = this;
240
+ return this._components[Component.label] = Component;
241
+ };
242
+
243
+ Namespace.prototype._addModule = function(Module) {
244
+ Module.namespace = this;
245
+ return this._modules[Module.label] = Module;
246
+ };
247
+
248
+ return Namespace;
249
+
250
+ })();
251
+
252
+ Unit = (function() {
253
+ Unit.namespace = null;
254
+
255
+ Unit.label = null;
256
+
257
+ Unit.labelFormat = /^[A-Z][A-Za-z0-9]*$/;
258
+
259
+ Unit.config = {};
260
+
261
+ Unit._initializeMethods = [];
262
+
263
+ Unit._destroyMethods = [];
264
+
265
+ Unit.extend = function(options) {
266
+ var Child, Parent;
267
+ if (!options.namespace) {
268
+ throw new Error("you must provide a namespace when creating a component");
269
+ }
270
+ if (!options.label) {
271
+ throw new Error("you must provide a name when creating a component");
272
+ }
273
+ if (!options.label.match(this.labelFormat)) {
274
+ throw new Error(options.label + " is not a valid component name");
275
+ }
276
+ Parent = this;
277
+ Child = (function(_super) {
278
+ var _destroy, _initialize;
279
+
280
+ __extends(Child, _super);
281
+
282
+ function Child() {
283
+ return Child.__super__.constructor.apply(this, arguments);
284
+ }
285
+
286
+ Child.label = options.label;
287
+
288
+ Child.config = _.clone(Child.config);
289
+
290
+ if (options.config) {
291
+ _.extend(Child.config, options.config);
292
+ }
293
+
294
+ Child._modules = _.clone(Child._modules);
295
+
296
+ if (options.properties) {
297
+ if (_initialize = options.properties._initialize) {
298
+ Child._initializeMethods = _.clone(Child._initializeMethods);
299
+ Child._initializeMethods.push(_initialize);
300
+ delete options.properties._initialize;
301
+ }
302
+ if (_destroy = options.properties._destroy) {
303
+ Child._destroyMethods = _.clone(Child._destroyMethods);
304
+ Child._destroyMethods.unshift(_destroy);
305
+ delete options.properties._destroy;
306
+ }
307
+ _.extend(Child.prototype, options.properties);
308
+ }
309
+
310
+ return Child;
311
+
312
+ })(Parent);
313
+ return options.namespace.addUnit(Child);
314
+ };
315
+
316
+ Unit.configure = function(config) {
317
+ _.extend(this.config, config);
318
+ return this.µObject();
319
+ };
320
+
321
+ Unit.fullName = function() {
322
+ return this.namespace.label + "-" + this.label;
323
+ };
324
+
325
+ function Unit(_at_config) {
326
+ var initialize, method, name, _i, _len, _ref, _ref1;
327
+ this.config = _at_config != null ? _at_config : {};
328
+ if (this.constructor.__super__) {
329
+ this["super"] = {};
330
+ _ref = this.constructor.__super__;
331
+ for (name in _ref) {
332
+ method = _ref[name];
333
+ this["super"][name] = _.bind(method, this);
334
+ }
335
+ }
336
+ _.defaults(this.config, this.constructor.config, this.constructor.namespace.config);
337
+ _ref1 = this.constructor._initializeMethods;
338
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
339
+ initialize = _ref1[_i];
340
+ initialize.call(this);
341
+ }
342
+ }
343
+
344
+ Unit.prototype.destroy = function() {
345
+ var destroy, _i, _len, _ref, _results;
346
+ _ref = this.constructor._destroyMethods;
347
+ _results = [];
348
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
349
+ destroy = _ref[_i];
350
+ _results.push(destroy.call(this));
351
+ }
352
+ return _results;
353
+ };
354
+
355
+ return Unit;
356
+
357
+ })();
358
+
359
+ Component = (function(_super) {
360
+ __extends(Component, _super);
361
+
362
+ Component._modules = {};
363
+
364
+ Component.µObject = function(options) {
365
+ var prototype;
366
+ prototype = this.prototype;
367
+ if (prototype instanceof Component) {
368
+ return {
369
+ configure: (function(_this) {
370
+ return function(config) {
371
+ return _this.configure(config);
372
+ };
373
+ })(this),
374
+ initialize: (function(_this) {
375
+ return function(el, config) {
376
+ return _this.initialize(el, config);
377
+ };
378
+ })(this),
379
+ instance: (function(_this) {
380
+ return function(el) {
381
+ return _this.instance(el);
382
+ };
383
+ })(this),
384
+ destroy: (function(_this) {
385
+ return function(el) {
386
+ return _this.destroy(el);
387
+ };
388
+ })(this)
389
+ };
390
+ } else {
391
+ return {
392
+ extend: (function(_this) {
393
+ return function(componentName) {
394
+ options.extend = factory.component(componentName);
395
+ return _this.µObject(options);
396
+ };
397
+ })(this),
398
+ module: (function(_this) {
399
+ return function(moduleName) {
400
+ if (options.modules) {
401
+ options.modules.push(moduleName);
402
+ } else {
403
+ options.modules = [moduleName];
404
+ }
405
+ return _this.µObject(options);
406
+ };
407
+ })(this),
408
+ configure: (function(_this) {
409
+ return function(config) {
410
+ if (options.config) {
411
+ _.extend(options.config, config);
412
+ } else {
413
+ options.config = config;
414
+ }
415
+ return _this.µObject(options);
416
+ };
417
+ })(this),
418
+ create: (function(_this) {
419
+ return function(properties) {
420
+ var Parent;
421
+ options.properties = properties;
422
+ Parent = options.extend || _this;
423
+ return Parent.extend(options);
424
+ };
425
+ })(this)
426
+ };
427
+ }
428
+ };
429
+
430
+ Component.extend = function(options) {
431
+ var Child, moduleName, _i, _len, _ref;
432
+ Child = Component.__super__.constructor.extend.call(this, options);
433
+ if (options.modules) {
434
+ _ref = options.modules;
435
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
436
+ moduleName = _ref[_i];
437
+ Child.include(moduleName);
438
+ }
439
+ }
440
+ return Child.µObject();
441
+ };
442
+
443
+ Component.include = function(moduleName) {
444
+ var Module;
445
+ Module = this.namespace.factory.module(moduleName);
446
+ if (this._modules[Module.fullName()]) {
447
+ throw new Error("module " + Module.namespace + "." + Module.label + " is already included");
448
+ }
449
+ this._modules[Module.label] = Module;
450
+ return Module.included(this);
451
+ };
452
+
453
+ Component.initialize = function(el, config) {
454
+ var instances;
455
+ instances = [];
456
+ $(el).each((function(_this) {
457
+ return function(i, el) {
458
+ return instances.push(new _this($(el), config));
459
+ };
460
+ })(this));
461
+ return this._methods(instances);
462
+ };
463
+
464
+ Component.instance = function(el) {
465
+ var instances;
466
+ instances = $(el).map((function(_this) {
467
+ return function(i, el) {
468
+ return _this._instance($(el));
469
+ };
470
+ })(this)).get();
471
+ return this._methods(instances);
472
+ };
473
+
474
+ Component.destroy = function(el, config) {
475
+ $(el).each((function(_this) {
476
+ return function(i, el) {
477
+ return _this._instance($(el)).destroy();
478
+ };
479
+ })(this));
480
+ return this.µObject();
481
+ };
482
+
483
+ Component._methods = function(instances) {
484
+ var methodName, methodNames, methods, prefix, _fn, _i, _len;
485
+ prefix = this.namespace.factory.config.methodPrefix;
486
+ methodNames = _.chain(this.prototype).functions().filter(function(name) {
487
+ return name.match(new RegExp("^" + prefix + "[A-Z]"));
488
+ }).value();
489
+ methods = {};
490
+ _fn = function(oldName) {
491
+ var newName;
492
+ newName = methodName[prefix.length].toLowerCase() + methodName.slice(prefix.length + 1);
493
+ return methods[newName] = function() {
494
+ var args, lastResult, results;
495
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
496
+ results = _.invoke.apply(_, [instances, oldName].concat(__slice.call(args)));
497
+ lastResult = results.pop();
498
+ if (_.isNull(lastResult)) {
499
+ return methods;
500
+ } else {
501
+ return lastResult;
502
+ }
503
+ };
504
+ };
505
+ for (_i = 0, _len = methodNames.length; _i < _len; _i++) {
506
+ methodName = methodNames[_i];
507
+ _fn(methodName);
508
+ }
509
+ methods.destroy = (function(_this) {
510
+ return function() {
511
+ _.invoke(instances, 'destroy');
512
+ return _this.µObject();
513
+ };
514
+ })(this);
515
+ return methods;
516
+ };
517
+
518
+ Component._instance = function($el) {
519
+ var components;
520
+ components = this.namespace.factory.data($el, 'components') || {};
521
+ if (!components[this.fullName()]) {
522
+ throw new Error('component is not initialized');
523
+ }
524
+ return components[this.fullName()];
525
+ };
526
+
527
+ function Component(_at_$el, config) {
528
+ var Module, components, factory, moduleLabel, name, _ref;
529
+ this.$el = _at_$el;
530
+ factory = this.constructor.namespace.factory;
531
+ components = factory.data(this.$el, 'components') || {};
532
+ name = this.constructor.fullName();
533
+ if (components[name]) {
534
+ throw new Error('component is already initialized');
535
+ }
536
+ components[name] = this;
537
+ factory.data(this.$el, 'components', components);
538
+ this._uniqueId = _.uniqueId((this.constructor.fullName()) + "-");
539
+ this._listeners = $();
540
+ this._dataFull = $();
541
+ this._dataNames = [];
542
+ Component.__super__.constructor.call(this, config);
543
+ this._moduleInstances = [];
544
+ _ref = this.constructor._modules;
545
+ for (moduleLabel in _ref) {
546
+ Module = _ref[moduleLabel];
547
+ this._moduleInstances.push(new Module(this, this.config));
548
+ }
549
+ }
550
+
551
+ Component.prototype.destroy = function() {
552
+ var components, dataName, factory, moduleInstance, _i, _j, _len, _len1, _ref, _ref1;
553
+ Component.__super__.destroy.apply(this, arguments);
554
+ _ref = this._moduleInstances;
555
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
556
+ moduleInstance = _ref[_i];
557
+ moduleInstance.destroy();
558
+ }
559
+ factory = this.constructor.namespace.factory;
560
+ this._listeners.off("." + this._uniqueId);
561
+ _ref1 = this._dataNames;
562
+ for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
563
+ dataName = _ref1[_j];
564
+ factory.removeData(this._dataFull, dataName);
565
+ }
566
+ components = factory.data(this.$el, 'components');
567
+ delete components[this.constructor.fullName()];
568
+ return factory.data(this.$el, 'components', components);
569
+ };
570
+
571
+ Component.prototype.$ = function(selector) {
572
+ return this.$el.find(selector);
573
+ };
574
+
575
+ Component.prototype.on = function(el, handlers) {
576
+ var $el, event, handler, selector, type, _ref, _results;
577
+ if (!handlers) {
578
+ handlers = el;
579
+ $el = this.$el;
580
+ } else {
581
+ $el = $(el);
582
+ }
583
+ this._listeners = this._listeners.add($el);
584
+ _results = [];
585
+ for (event in handlers) {
586
+ handler = handlers[event];
587
+ _ref = splitOnce(event, ' '), type = _ref[0], selector = _ref[1];
588
+ type = type + "." + this._uniqueId;
589
+ if (_.isString(handler)) {
590
+ (function(_this) {
591
+ return (function(method) {
592
+ return handler = function() {
593
+ var args;
594
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
595
+ return _this[method].apply(_this, args);
596
+ };
597
+ });
598
+ })(this)(handler);
599
+ }
600
+ if (selector) {
601
+ _results.push($el.on(type, selector, handler));
602
+ } else {
603
+ _results.push($el.on(type, handler));
604
+ }
605
+ }
606
+ return _results;
607
+ };
608
+
609
+ Component.prototype.off = function(el, eventType) {
610
+ var $el;
611
+ if (arguments.length < 2 && _.isString(el) || _.isUndefined(el)) {
612
+ eventType = el;
613
+ $el = this.$el;
614
+ } else {
615
+ $el = $(el);
616
+ }
617
+ if (eventType) {
618
+ return $el.off(eventType + "." + this._uniqueId);
619
+ } else {
620
+ return $el.off("." + this._uniqueId);
621
+ }
622
+ };
623
+
624
+ Component.prototype.trigger = function() {
625
+ var eventType, namespace, params, _ref;
626
+ eventType = arguments[0], params = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
627
+ namespace = this.constructor.fullName();
628
+ return (_ref = this.$el).trigger.apply(_ref, [namespace + ":" + eventType].concat(__slice.call(params)));
629
+ };
630
+
631
+ Component.prototype.data = function(el, name, value) {
632
+ var factory;
633
+ if (arguments.length < 3 && _.isString(el)) {
634
+ value = name;
635
+ name = el;
636
+ el = this.$el;
637
+ }
638
+ factory = this.constructor.namespace.factory;
639
+ name = this._dataName(name);
640
+ if (_.isUndefined(value)) {
641
+ return factory.data(el, name);
642
+ } else {
643
+ factory.data(el, name, value);
644
+ this._dataFull.add(el);
645
+ return this._dataNames.push(name);
646
+ }
647
+ };
648
+
649
+ Component.prototype.removeData = function(el, name) {
650
+ var factory;
651
+ if (arguments.length < 2) {
652
+ name = el;
653
+ el = this.$el;
654
+ }
655
+ factory = this.constructor.namespace.factory;
656
+ name = this._dataName(name);
657
+ return factory.removeData(el, name);
658
+ };
659
+
660
+ Component.prototype._dataName = function(name) {
661
+ return this._uniqueId + "-" + name;
662
+ };
663
+
664
+ return Component;
665
+
666
+ })(Unit);
667
+
668
+ Module = (function(_super) {
669
+ var componentMethod, delegatedMethods, _fn, _i, _len;
670
+
671
+ __extends(Module, _super);
672
+
673
+ Module._extensions = {};
674
+
675
+ Module.µObject = function(options) {
676
+ var prototype;
677
+ prototype = this.prototype;
678
+ if (prototype instanceof Module) {
679
+ return {
680
+ configure: (function(_this) {
681
+ return function(config) {
682
+ return _this.configure(config);
683
+ };
684
+ })(this)
685
+ };
686
+ } else {
687
+ return {
688
+ configure: (function(_this) {
689
+ return function(config) {
690
+ options.config = config;
691
+ return _this.µObject(options);
692
+ };
693
+ })(this),
694
+ extend: (function(_this) {
695
+ return function(moduleName) {
696
+ options.extend = factory.module(moduleName);
697
+ return _this.µObject(options);
698
+ };
699
+ })(this),
700
+ extensions: (function(_this) {
701
+ return function(extensions) {
702
+ options.extensions = extensions;
703
+ return _this.µObject(options);
704
+ };
705
+ })(this),
706
+ create: (function(_this) {
707
+ return function(properties) {
708
+ var Parent;
709
+ options.properties = properties;
710
+ Parent = options.extend || _this;
711
+ return Parent.extend(options);
712
+ };
713
+ })(this)
714
+ };
715
+ }
716
+ };
717
+
718
+ Module.extend = function(options) {
719
+ var Child;
720
+ Child = Module.__super__.constructor.extend.call(this, options);
721
+ if (options.extensions) {
722
+ _.extend(Child._extensions, options.extensions);
723
+ }
724
+ return Child.µObject();
725
+ };
726
+
727
+ Module.included = function(Component) {
728
+ return _.extend(Component.prototype, this._extensions);
729
+ };
730
+
731
+ function Module(_at_component, config) {
732
+ this.component = _at_component;
733
+ this.$el = this.component.$el;
734
+ Module.__super__.constructor.call(this, config);
735
+ }
736
+
737
+ Module.prototype.on = function(el, handlers) {
738
+ var event, handler;
739
+ if (!handlers) {
740
+ handlers = el;
741
+ el = this.$el;
742
+ }
743
+ for (event in handlers) {
744
+ handler = handlers[event];
745
+ if (_.isString(handler)) {
746
+ (function(_this) {
747
+ return (function(method) {
748
+ return handler = function() {
749
+ var args;
750
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
751
+ return _this[method].apply(_this, args);
752
+ };
753
+ });
754
+ })(this)(handler);
755
+ handlers[event] = handler;
756
+ }
757
+ }
758
+ return this.component.on(el, handlers);
759
+ };
760
+
761
+ delegatedMethods = ['$', 'off', 'trigger', 'data', 'removeData'];
762
+
763
+ _fn = function(name) {
764
+ return Module.prototype[name] = function() {
765
+ var args, _ref;
766
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
767
+ return (_ref = this.component)[name].apply(_ref, args);
768
+ };
769
+ };
770
+ for (_i = 0, _len = delegatedMethods.length; _i < _len; _i++) {
771
+ componentMethod = delegatedMethods[_i];
772
+ _fn(componentMethod);
773
+ }
774
+
775
+ return Module;
776
+
777
+ })(Unit);
778
+
779
+ factory = new Factory();
780
+
781
+ µ = function(objectName) {
782
+ var C, M, componentName, currentName, moduleName, names, namespace, namespaceName;
783
+ if (objectName == null) {
784
+ objectName = '';
785
+ }
786
+ names = objectName.split('.');
787
+ currentName = names.shift();
788
+ switch (currentName) {
789
+ case 'components':
790
+ case 'modules':
791
+ case '':
792
+ namespaceName = factory.config.defaultNamespace;
793
+ break;
794
+ default:
795
+ if (currentName.match(Unit.labelFormat)) {
796
+ namespaceName = factory.config.defaultNamespace;
797
+ } else {
798
+ namespaceName = currentName;
799
+ currentName = names.shift();
800
+ }
801
+ }
802
+ if (!(namespace = factory.namespace(namespaceName, true))) {
803
+ namespace = factory.createNamespace(namespaceName);
804
+ }
805
+ if (!currentName) {
806
+ return namespace.µObject();
807
+ }
808
+ switch (currentName) {
809
+ case 'components':
810
+ componentName = names.shift();
811
+ if (C = namespace.component(componentName, true)) {
812
+ return C.µObject();
813
+ } else {
814
+ return Component.µObject({
815
+ namespace: namespace,
816
+ label: componentName
817
+ });
818
+ }
819
+ break;
820
+ case 'modules':
821
+ moduleName = names.shift();
822
+ if (M = namespace.module(moduleName, true)) {
823
+ return M.µObject();
824
+ } else {
825
+ return Module.µObject({
826
+ namespace: namespace,
827
+ label: moduleName
828
+ });
829
+ }
830
+ break;
831
+ default:
832
+ if (C = namespace.component(currentName, true)) {
833
+ return C.µObject();
834
+ } else if (M = namespace.module(currentName, true)) {
835
+ return M.µObject();
836
+ } else {
837
+ return Component.µObject({
838
+ namespace: namespace,
839
+ label: currentName
840
+ });
841
+ }
842
+ }
843
+ };
844
+
845
+ _.extend(µ, {
846
+ configure: function(config) {
847
+ return factory.configure(config);
848
+ },
849
+ setup: function(setup) {
850
+ return factory.setup(setup);
851
+ },
852
+ initialize: function(el) {
853
+ return factory.initialize(el);
854
+ },
855
+ destroy: function(el) {
856
+ return factory.destroy(el);
857
+ },
858
+ clean: function() {
859
+ return factory.clean();
860
+ }
861
+ });
862
+
863
+ window.Micro = window.µ = µ;
864
+
865
+ }).call(this);
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: micro-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Jérôme Guyon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jquery-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: underscore-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - jeromep.guyon@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/micro-rails.rb
82
+ - lib/micro/rails.rb
83
+ - lib/micro/rails/engine.rb
84
+ - lib/micro/rails/version.rb
85
+ - micro-rails.gemspec
86
+ - vendor/assets/javascripts/micro.js
87
+ homepage: https://github.com/jguyon/micro-rails
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">"
103
+ - !ruby/object:Gem::Version
104
+ version: 1.3.1
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.5
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ships the javascript library Micro for the Rails asset-pipeline.'
111
+ test_files: []