Skip to content

Commit 87986ca

Browse files
committed
Merge pull request cibernox#30 from cibernox/broccolify-all-the-things
Broccolify all the things
2 parents df1b3cf + 6ae3032 commit 87986ca

12 files changed

Lines changed: 218 additions & 129 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/bower_components/
22
/node_modules/
3+
/tmp/

.jshintrc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
"undef": true,
55

66
"predef": [
7-
"Handlebars", "Ember",
7+
"Handlebars",
8+
"Ember",
89
"EmberCPM",
9-
"describe", "it", "expect", "beforeEach", "afterEach"
10+
"describe",
11+
"it",
12+
"expect",
13+
"beforeEach",
14+
"afterEach",
15+
"require",
16+
"module",
17+
"process"
1018
]
1119
}

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: "node_js"
22
node_js:
33
- "0.10"
4-
install: make install_dependencies
54
before_script:
65
- export DISPLAY=:99.0
76
- sh -e /etc/init.d/xvfb start
8-
script: make test
7+
install: "make install_dependencies"
8+
script: "make test-ci"

Brocfile.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var concat = require('broccoli-concat');
2+
var mergeTrees = require('broccoli-merge-trees');
3+
var findBowerTrees = require('broccoli-bower');
4+
var pickFiles = require('broccoli-static-compiler');
5+
var uglifyJavaScript = require('broccoli-uglify-js');
6+
7+
var src = 'src';
8+
9+
var emberCPM = concat(src, {
10+
inputFiles: [
11+
'preamble.js',
12+
'concat.js',
13+
'index.js'
14+
],
15+
outputFile: '/ember-cpm.js'
16+
});
17+
18+
var emberCPMmin = uglifyJavaScript(emberCPM, {compress: true});
19+
var minifiedFile = concat(emberCPMmin, {
20+
inputFiles: ['*.js'],
21+
outputFile: '/ember-cpm.min.js',
22+
});
23+
24+
var outTrees = [emberCPM, minifiedFile];
25+
26+
if (process.argv[2] !== 'build') {
27+
var testDeps = pickFiles('node_modules/testem/public/testem/', {
28+
srcDir: '/',
29+
files: ['mocha.css', 'mocha.js', 'chai.js'],
30+
destDir: '/'
31+
});
32+
33+
var specs = concat('spec', {
34+
inputFiles: ['**/*.js'],
35+
outputFile: '/ember-cpm-specs.js'
36+
});
37+
38+
var html = pickFiles('spec', {
39+
srcDir: '/',
40+
files: ['index.html'],
41+
destDir: '/'
42+
});
43+
44+
outTrees = outTrees.concat(findBowerTrees()).concat([testDeps, specs, html]);
45+
}
46+
47+
48+
module.exports = mergeTrees(outTrees);

Makefile

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,28 @@ jshint_bin = ./node_modules/jshint/bin/jshint
22
testem_bin = ./node_modules/testem/testem.js
33
bower_bin = ./node_modules/bower/bin/bower
44

5-
bower_libs = bower_components/ember/ember.js bower_components/jquery/jquery.js bower_components/handlebars/handlebars.runtime.js
6-
npm_libs = $(jshint_bin) $(phantomjs_bin) $(bower_bin)
7-
src_files = src/preamble.js src/index.js src/concat.js
5+
build: jshint
6+
rm -rf dist && broccoli build dist
87

9-
test: ensure_phantomjs jshint $(bower_libs)
8+
test-ci: ensure_phantomjs jshint
109
@$(testem_bin) ci -l phantomjs
1110

12-
ensure_phantomjs:
13-
@which phantomjs > /dev/null || (echo "Couldn't find phantomjs" && false)
11+
test: ensure_phantomjs jshint $(bower_libs)
12+
@$(testem_bin)
1413

15-
install_dependencies: npm_install bower_install
14+
install_dependencies:
15+
@npm install
16+
@bower install
1617

17-
index.js: $(src_files)
18-
cat $(src_files) > index.js
18+
ensure_phantomjs:
19+
@which phantomjs > /dev/null || (echo "Couldn't find phantomjs" && false)
1920

2021
jshint: $(jshint_bin)
21-
@$(jshint_bin) index.js src/*.js spec/*Spec.js
22+
@jshint Brocfile.js src/*.js spec/*Spec.js
2223
@echo "JSHint OK"
2324

24-
$(bower_libs): bower_install
25-
26-
bower_install:
27-
@$(bower_bin) install
28-
29-
$(npm_libs): npm_install
30-
31-
npm_install:
32-
@npm install
33-
34-
clobber:
25+
pristine:
3526
@rm -rf ./node_modules/
3627
@rm -rf ./bower_components/
3728

38-
.PHONY: ensure_phantomjs install_dependencies test jshint bower_install npm_install clobber
29+
.PHONY: ensure_phantomjs install_dependencies test test-ci jshint pristine

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ has them.)
1313

1414
### Installation
1515

16-
Just add `index.js` to your page after Ember but before your app.
16+
Just add `ember-cpm.js` to your page after Ember but before your app.
17+
18+
### Build from source
19+
20+
You will need to install the dependencies first. Run `make install_dependencies` to do it.
21+
22+
Then run `make` to generate `ember-cpm.js` from source in the `dist` folder.
23+
24+
### Run tests
25+
26+
To run the rest suite in interactive mode execute `make test` and enter `http://localhost:4200`.
27+
28+
To run the test suite just once and exit run `make test-ci`.
29+
30+
You can also run the test suite in watch mode with `broccoli serve`.
1731

1832
### Run tests
1933

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.1.0",
44
"description": "Computed Propery Macros for Ember.js",
55
"main": [
6-
"index.js"
6+
"dist/ember-cpm.js"
77
],
88
"dependencies": {
99
"ember": "1.2.0",

index.js renamed to dist/ember-cpm.js

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,104 @@
2020
if (Ember.libraries) Ember.libraries.register('Ember-CPM', EmberCPM.VERSION);
2121

2222
}).call(undefined, this, this.Ember, this.jQuery);
23+
24+
(function(window, Ember, $, EmberCPM) {
25+
var a_slice = Array.prototype.slice,
26+
a_forEach = Ember.ArrayPolyfills.forEach,
27+
get = Ember.get;
28+
29+
/*
30+
Returns the index where an item is to be removed from, or placed into, for
31+
an EmberCPM.Macros.concat array.
32+
33+
This is the index of the item within its dependent array, offset by the
34+
lengths of all prior dependent arrays.
35+
*/
36+
function getIndex(changeMeta, instanceMeta, dependentArrayDelta) {
37+
var dependentArrayGuid = Ember.guidFor(changeMeta.arrayChanged);
38+
39+
if (!(dependentArrayGuid in instanceMeta.dependentGuidToIndex)) {
40+
recomputeGuidIndexes(instanceMeta, changeMeta.property._dependentKeys, this);
41+
}
42+
43+
var dependentArrayLengths = instanceMeta.dependentArrayLengths,
44+
dependentArrayIndex = instanceMeta.dependentGuidToIndex[dependentArrayGuid],
45+
offset = 0,
46+
arrayIndex;
47+
48+
// offset is the sum of the lengths of arrays to our left
49+
for (var i = 0; i < dependentArrayIndex; ++i) {
50+
offset += (dependentArrayLengths[i] || 0);
51+
}
52+
53+
arrayIndex = offset + changeMeta.index;
54+
dependentArrayLengths[dependentArrayIndex] = (get(changeMeta.arrayChanged, 'length') || 0) + dependentArrayDelta;
55+
56+
return arrayIndex;
57+
}
58+
59+
function recomputeGuidIndexes(instanceMeta, keys, context) {
60+
instanceMeta.dependentGuidToIndex = {};
61+
a_forEach.call(keys, function (key, idx) {
62+
instanceMeta.dependentGuidToIndex[Ember.guidFor(this.get(key))] = idx;
63+
}, context);
64+
}
65+
66+
/**
67+
Keeps n arrays concatenated using `Ember.ArrayComputed`.
68+
69+
Example:
70+
```js
71+
obj = Ember.Object.createWithMixins({
72+
itemsA: [],
73+
itemsB: [],
74+
itemsC: [],
75+
allItems: EmberCPM.Macros.concat('itemsA', 'itemsB', 'itemsC');
76+
});
77+
78+
obj.get('itemsA').pushObjects(['a', 'b']);
79+
obj.get('allItems') //=> ['a', 'b']
80+
81+
obj.get('itemsB').pushObjects(['c']);
82+
obj.get('allItems') //=> ['a', 'b', 'c']
83+
84+
obj.get('itemsC').pushObjects(['d']);
85+
obj.get('allItems') //=> ['a', 'b', 'c', 'd']
86+
87+
obj.get('itemsB').pushObjects(['e', 'f']);
88+
obj.get('allItems') //=> ['a', 'b', 'c', 'e', 'f', 'd']
89+
```
90+
*/
91+
EmberCPM.Macros.concat = function () {
92+
var args = a_slice.call(arguments);
93+
args.push({
94+
initialize: function (array, changeMeta, instanceMeta) {
95+
instanceMeta.dependentArrayLengths = new Array(changeMeta.property._dependentKeys.length);
96+
// When items are added or removed, we have access to the array that was
97+
// changed, but not its dependent key, so we use its guid as the key to
98+
// determine its index in the array of dependent keys.
99+
instanceMeta.dependentGuidToIndex = {};
100+
101+
return array;
102+
},
103+
104+
addedItem: function (array, item, changeMeta, instanceMeta) {
105+
var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, 0);
106+
array.insertAt(arrayIndex, item);
107+
return array;
108+
},
109+
110+
removedItem: function (array, item, changeMeta, instanceMeta) {
111+
var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, -1);
112+
array.removeAt(arrayIndex);
113+
return array;
114+
}
115+
});
116+
117+
return Ember.arrayComputed.apply(null, args);
118+
};
119+
}).call(undefined, this, this.Ember, this.jQuery, this.EmberCPM);
120+
23121
(function(window, Ember, $, EmberCPM) {
24122

25123
function reverseMerge(dest, source) {
@@ -151,99 +249,3 @@
151249
};
152250

153251
}).call(undefined, this, this.Ember, this.jQuery, this.EmberCPM);
154-
(function(window, Ember, $, EmberCPM) {
155-
var a_slice = Array.prototype.slice,
156-
a_forEach = Ember.ArrayPolyfills.forEach,
157-
get = Ember.get;
158-
159-
/*
160-
Returns the index where an item is to be removed from, or placed into, for
161-
an EmberCPM.Macros.concat array.
162-
163-
This is the index of the item within its dependent array, offset by the
164-
lengths of all prior dependent arrays.
165-
*/
166-
function getIndex(changeMeta, instanceMeta, dependentArrayDelta) {
167-
var dependentArrayGuid = Ember.guidFor(changeMeta.arrayChanged);
168-
169-
if (!(dependentArrayGuid in instanceMeta.dependentGuidToIndex)) {
170-
recomputeGuidIndexes(instanceMeta, changeMeta.property._dependentKeys, this);
171-
}
172-
173-
var dependentArrayLengths = instanceMeta.dependentArrayLengths,
174-
dependentArrayIndex = instanceMeta.dependentGuidToIndex[dependentArrayGuid],
175-
offset = 0,
176-
arrayIndex;
177-
178-
// offset is the sum of the lengths of arrays to our left
179-
for (var i = 0; i < dependentArrayIndex; ++i) {
180-
offset += (dependentArrayLengths[i] || 0);
181-
}
182-
183-
arrayIndex = offset + changeMeta.index;
184-
dependentArrayLengths[dependentArrayIndex] = (get(changeMeta.arrayChanged, 'length') || 0) + dependentArrayDelta;
185-
186-
return arrayIndex;
187-
}
188-
189-
function recomputeGuidIndexes(instanceMeta, keys, context) {
190-
instanceMeta.dependentGuidToIndex = {};
191-
a_forEach.call(keys, function (key, idx) {
192-
instanceMeta.dependentGuidToIndex[Ember.guidFor(this.get(key))] = idx;
193-
}, context);
194-
}
195-
196-
/**
197-
Keeps n arrays concatenated using `Ember.ArrayComputed`.
198-
199-
Example:
200-
```js
201-
obj = Ember.Object.createWithMixins({
202-
itemsA: [],
203-
itemsB: [],
204-
itemsC: [],
205-
allItems: EmberCPM.Macros.concat('itemsA', 'itemsB', 'itemsC');
206-
});
207-
208-
obj.get('itemsA').pushObjects(['a', 'b']);
209-
obj.get('allItems') //=> ['a', 'b']
210-
211-
obj.get('itemsB').pushObjects(['c']);
212-
obj.get('allItems') //=> ['a', 'b', 'c']
213-
214-
obj.get('itemsC').pushObjects(['d']);
215-
obj.get('allItems') //=> ['a', 'b', 'c', 'd']
216-
217-
obj.get('itemsB').pushObjects(['e', 'f']);
218-
obj.get('allItems') //=> ['a', 'b', 'c', 'e', 'f', 'd']
219-
```
220-
*/
221-
EmberCPM.Macros.concat = function () {
222-
var args = a_slice.call(arguments);
223-
args.push({
224-
initialize: function (array, changeMeta, instanceMeta) {
225-
instanceMeta.dependentArrayLengths = new Array(changeMeta.property._dependentKeys.length);
226-
// When items are added or removed, we have access to the array that was
227-
// changed, but not its dependent key, so we use its guid as the key to
228-
// determine its index in the array of dependent keys.
229-
instanceMeta.dependentGuidToIndex = {};
230-
231-
return array;
232-
},
233-
234-
addedItem: function (array, item, changeMeta, instanceMeta) {
235-
var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, 0);
236-
array.insertAt(arrayIndex, item);
237-
return array;
238-
},
239-
240-
removedItem: function (array, item, changeMeta, instanceMeta) {
241-
var arrayIndex = getIndex.call(this, changeMeta, instanceMeta, -1);
242-
array.removeAt(arrayIndex);
243-
return array;
244-
}
245-
});
246-
247-
return Ember.arrayComputed.apply(null, args);
248-
};
249-
}).call(undefined, this, this.Ember, this.jQuery, this.EmberCPM);

dist/ember-cpm.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111
"type": "git",
1212
"url": "git://github.com/jamesarosen/ember-cpm.git"
1313
},
14-
"main": "index.js",
14+
"main": "dist/ember-cpm.js",
1515
"scripts": {
16-
"test": "make test"
16+
"test": "make test-ci"
1717
},
1818
"devDependencies": {
1919
"testem": "~0.6.15",
20-
"jshint": "~2.0.1",
21-
"bower": "~1.2.6"
20+
"jshint": "~2.5.0",
21+
"bower": "~1.3.3",
22+
"broccoli": "~0.9.0",
23+
"broccoli-uglify-js": "~0.1.3",
24+
"broccoli-concat": "0.0.6",
25+
"broccoli-merge-trees": "~0.1.3",
26+
"broccoli-bower": "~0.2.1",
27+
"broccoli-static-compiler": "~0.1.4"
2228
}
2329
}

0 commit comments

Comments
 (0)