Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Axed normal projections for now because they aren't used in Hyperarch…

…y and will be a bit of a pain to bring into the new skipList paradigm
  • Loading branch information...
commit 82d167d27a0470fd581182697bc2150fef4e6de3 1 parent cfff9d1
@nathansobo authored
View
1  lib/monarch/model/relations.js
@@ -5,7 +5,6 @@
//= require "relations/selection"
//= require "relations/inner_join"
//= require "relations/ordering"
-//= require "relations/projection"
//= require "relations/table_projection"
//= require "relations/union"
//= require "relations/difference"
View
92 lib/monarch/model/relations/projection.js
@@ -1,92 +0,0 @@
-(function(Monarch) {
-
-_.constructor("Monarch.Model.Relations.Projection", Monarch.Model.Relations.Relation, {
- initialize: function(operand, projectedColumns) {
- this.operand = operand;
- this.projectedColumnsByName = {};
- _.each(projectedColumns, function(projectedColumn) {
- this.projectedColumnsByName[projectedColumn.name()] = projectedColumn;
- }, this);
-
- this.sortSpecifications = this.projectSortSpecifications(projectedColumns, operand.sortSpecifications);
- this.tupleConstructor = _.constructor(Monarch.Model.Tuple);
- this.tupleConstructor.projectedColumnsByName = this.projectedColumnsByName;
- this.tupleConstructor.initializeFieldReaders();
-
- this.initializeEventsSystem();
- },
-
- projectSortSpecifications: function(projectedColumns, sortSpecifications) {
- var projectedSortSpecifications = [];
-
- _.each(sortSpecifications, function(sortSpec) {
- var projectedColumn = _.detect(projectedColumns, function(projectedColumn) {
- return projectedColumn.column === sortSpec.column;
- });
-
- if (projectedColumn) {
- projectedSortSpecifications.push(new Monarch.Model.SortSpecification(projectedColumn, sortSpec.direction));
- }
- });
-
- return projectedSortSpecifications;
- },
-
- tuples: function() {
- if (this.storedTuples) return this.storedTuples.values();
-
- this.tuplesByOperandRecordId = {};
- return _.map(this.operand.tuples(), function(operandTuple) {
- return this.tuplesByOperandRecordId[operandTuple.id()] = new this.tupleConstructor(operandTuple);
- }, this);
- },
-
- column: function(name) {
- return this.projectedColumnsByName[name];
- },
-
- surfaceTables: function() {
- throw new Error("Projections do not have surface tables");
- },
-
- // private
-
- subscribeToOperands: function() {
- this.operandsSubscriptionBundle.add(this.operand.onInsert(function(operandRecord) {
- var record = new this.tupleConstructor(operandRecord);
- this.tuplesByOperandRecordId[operandRecord.id()] = record;
- this.tupleInsertedRemotely(record);
- }, this));
- this.operandsSubscriptionBundle.add(this.operand.onUpdate(function(operandRecord, operandChanges) {
- var changes = this.translateUpdateChanges(operandChanges);
- if (_.isEmpty(changes)) return;
- this.tupleUpdatedRemotely(this.tuplesByOperandRecordId[operandRecord.id()], changes);
- }, this));
- this.operandsSubscriptionBundle.add(this.operand.onRemove(function(operandRecord) {
- this.tupleRemovedRemotely(this.tuplesByOperandRecordId[operandRecord.id()]);
- }, this));
- },
-
- translateUpdateChanges: function(changes) {
- var translatedChanges = {};
- _.each(changes, function(operandColumnChanges) {
- var projectedColumn = this.projectedColumnFromOperandColumn(operandColumnChanges.column);
- if (projectedColumn) {
- translatedChanges[projectedColumn.name()] = {
- column: projectedColumn,
- oldValue: operandColumnChanges.oldValue,
- newValue: operandColumnChanges.newValue
- }
- }
- }, this);
- return translatedChanges;
- },
-
- projectedColumnFromOperandColumn: function(operandColumn) {
- return _.detect(this.projectedColumnsByName, function(projectedColumn) {
- return projectedColumn.column === operandColumn;
- });
- }
-});
-
-})(Monarch);
View
35 lib/monarch/model/relations/relation.js
@@ -31,32 +31,19 @@ _.constructor("Monarch.Model.Relations.Relation", {
},
project: function() {
- if (arguments.length == 1) {
- var table;
- if (_.isFunction(arguments[0])) {
- table = arguments[0].table;
- } else if (arguments[0].surfaceTables) {
- var surfaceTables = arguments[0].surfaceTables();
- if (surfaceTables.length === 1) {
- table = surfaceTables[0];
- } else {
- throw new Error("Can only project with relations that have a single surface table");
- }
+ var table;
+ if (_.isFunction(arguments[0])) {
+ table = arguments[0].table;
+ } else {
+ var surfaceTables = arguments[0].surfaceTables();
+ if (surfaceTables.length === 1) {
+ table = surfaceTables[0];
+ } else {
+ throw new Error("Can only project with relations that have a single surface table");
}
-
- if (table) return new Monarch.Model.Relations.TableProjection(this, table);
}
- var projectedColumns = _.map(arguments, function(arg) {
- if (arg instanceof Monarch.Model.ProjectedColumn) {
- return arg;
- } else if (arg instanceof Monarch.Model.Column) {
- return new Monarch.Model.ProjectedColumn(arg);
- } else {
- throw new Error("#project takes Columns or ProjectedColumns only");
- }
- });
- return new Monarch.Model.Relations.Projection(this, projectedColumns);
+ return new Monarch.Model.Relations.TableProjection(this, table);
},
join: function(rightOperand) {
@@ -288,6 +275,8 @@ _.constructor("Monarch.Model.Relations.Relation", {
this.onUpdateNode.publish(tuple, changeset, newIndex, oldIndex, newKey, oldKey);
},
+ // default implementations. overridden by many subclasses
+
tupleRemovedRemotely: function(tuple, newKey, oldKey) {
if (!newKey) newKey = oldKey = this.buildSortKey(tuple);
View
58 spec/monarch/model/relations/difference_spec.js
@@ -179,63 +179,5 @@ Screw.Unit(function(c) { with(c) {
});
});
});
-
- describe("when the difference is between two distinct but compatible relations", function() {
- var difference, leftOperand, rightOperand, insertCallback, updateCallback, removeCallback;
-
- before(function() {
- _.constructor('A', Monarch.Model.Record);
- _.constructor('B', Monarch.Model.Record);
- A.columns({ projectedId: "string", baz: "string" });
- B.columns({ projectedId: "string", baz: "string" });
-
- leftOperand = A.project(A.projectedId.as('id'), A.baz);
- rightOperand = B.project(B.projectedId.as('id'), B.baz);
-
- difference = new Monarch.Model.Relations.Difference(leftOperand, rightOperand);
-
- insertCallback = mockFunction('insertCallback');
- updateCallback = mockFunction('updateCallback');
- removeCallback = mockFunction('removeCallback');
-
- difference.onInsert(insertCallback);
- difference.onUpdate(updateCallback);
- difference.onRemove(removeCallback);
- });
-
- after(function() {
- delete window.A;
- delete window.B;
- delete Repository.tables.as;
- delete Repository.tables.bs;
- });
-
- it("only considers the 'id' property when performing the difference", function() {
- A.create({projectedId: "foo", baz: "quux"});
- expect(insertCallback).to(haveBeenCalled, once);
-
- A.find({projectedId: 'foo'}).update({baz: "morning"});
- expect(updateCallback).to(haveBeenCalled, once);
-
- B.create({projectedId: "foo"});
- expect(removeCallback).to(haveBeenCalled, once);
-
- updateCallback.clear();
- var aRecord = A.find({projectedId: 'foo'});
- aRecord.update({baz: "evening"});
- expect(updateCallback).toNot(haveBeenCalled);
-
- removeCallback.clear();
- aRecord.destroy();
- expect(removeCallback).toNot(haveBeenCalled);
-
- insertCallback.clear();
- A.create({projectedId: "foo", baz: "quux"});
- expect(insertCallback).toNot(haveBeenCalled);
-
- B.find({projectedId: 'foo'}).destroy();
- expect(insertCallback).to(haveBeenCalled, once);
- });
- });
});
}});
View
97 spec/monarch/model/relations/projection_spec.js
@@ -1,97 +0,0 @@
-//= require "../../../monarch_spec_helper"
-
-Screw.Unit(function(c) { with(c) {
- describe("Monarch.Model.Relations.Projection", function() {
- useLocalFixtures();
-
- var operand, projectedColumns, projection;
- before(function() {
- operand = Blog.table;
- projectedColumns = [new Monarch.Model.ProjectedColumn(Blog.userId), Blog.name_.as('blogName')];
- projection = new Monarch.Model.Relations.Projection(operand, projectedColumns);
- });
-
-
- describe("#tuples", function() {
- it("returns ProjectedTuples with fields corresponding only to the #projectedColumns", function() {
- var projectionTuples = projection.tuples();
- var operandTuples = operand.tuples();
-
- expect(projectionTuples).toNot(beEmpty);
- expect(projectionTuples.length).to(eq, operandTuples.length);
-
- _.each(operand.tuples(), function(operandRecord, index) {
- var projectionRecord = projectionTuples[index];
- expect(projectionRecord.userId()).to(eq, operandRecord.userId());
- expect(projectionRecord.field(projection.column('blogName')).value()).to(eq, operandRecord.name());
- expect(projectionRecord.field('blogName').value()).to(eq, operandRecord.name());
- expect(projectionRecord.startedAt).to(beNull);
- });
- });
- });
-
-
- describe("event handling", function() {
- var insertCallback, updateCallback, removeCallback;
- before(function() {
- insertCallback = mockFunction("insertCallback");
- updateCallback = mockFunction("updateCallback");
- removeCallback = mockFunction("removeCallback");
- projection.onInsert(insertCallback);
- projection.onUpdate(updateCallback);
- projection.onRemove(removeCallback);
- });
-
- describe("when a record is inserted into the operand", function() {
- it("triggers onInsert callbacks with the inserted record's corresponding ProjectionRecord", function() {
- var operandRecord = operand.create({name: "Radio Flyer"});
-
- expect(insertCallback).to(haveBeenCalled, once);
- expect(insertCallback.mostRecentArgs[0].blogName()).to(eq, "Radio Flyer");
- expect(projection.find(projection.column('blogName').eq("Radio Flyer"))).to(eq, insertCallback.mostRecentArgs[0]);
- });
- });
-
- describe("when a record is updated in the operand", function() {
- context("if any of the updated columns are in #projectedColumns", function() {
- it("triggers onUpdate callbacks with the record's corresponding ProjectionRecord and the changed columns", function() {
- var operandRecord = operand.find('motorcycle');
- var oldName = operandRecord.name();
- var projectionRecord = projection.find(projection.column('blogName').eq(operandRecord.name()));
- expect(projectionRecord).toNot(beNull);
-
- operandRecord.update({name: "Motorcycles: Wheee!"});
-
- expect(updateCallback).to(haveBeenCalled, withArgs(projectionRecord, {
- blogName: {
- column: projection.column('blogName'),
- oldValue: oldName,
- newValue: "Motorcycles: Wheee!"
- }
- }));
- });
- });
-
- context("if none of the updated columns are in #projectedColumns", function() {
- it("does not trigger any callbacks", function() {
- var operandRecord = operand.find('motorcycle');
-
- operandRecord.localUpdate({startedAt: new Date()});
-
- expect(updateCallback).toNot(haveBeenCalled, once);
- });
- });
- });
-
- describe("when a record is removed from the operand", function() {
- it("triggers onRemove callbacks with the removed record's corresponding ProjectionRecord", function() {
- var operandRecord = operand.find('motorcycle');
- var projectionRecord = projection.find(projection.column('blogName').eq(operandRecord.name()));
- operand.remove(operandRecord);
-
- expect(removeCallback).to(haveBeenCalled, withArgs(projectionRecord));
- });
- });
- });
- });
-}});
View
24 spec/monarch/model/relations/relation_spec.js
@@ -195,29 +195,6 @@ Screw.Unit(function(c) { with(c) {
});
describe("#project(projectedColumns...)", function() {
- context("when passed ProjectedColumns", function() {
- it("constructs a Projection with self as #operand and the given ProjectedColumns", function() {
- projectedColumn1 = column1.as('a');
- projectedColumn2 = column2.as('b');
- var projection = relation.project(projectedColumn1, projectedColumn2);
-
- expect(projection).to(beAnInstanceOf, Monarch.Model.Relations.Projection);
- expect(projection.operand).to(eq, relation);
- expect(projection.projectedColumnsByName['a']).to(eq, projectedColumn1);
- expect(projection.projectedColumnsByName['b']).to(eq, projectedColumn2);
- });
- });
-
- context("when passed Columns", function() {
- it("constructs a Projection with self as #operand and the given Columns converted to ProjectedColumns", function() {
- var projection = relation.project(column1, column2);
- expect(projection).to(beAnInstanceOf, Monarch.Model.Relations.Projection);
- expect(projection.operand).to(eq, relation);
- expect(projection.projectedColumnsByName[column1.name].column).to(eq, column1);
- expect(projection.projectedColumnsByName[column2.name].column).to(eq, column2);
- });
- });
-
context("when passed a record constructor", function() {
it("returns a table projection based on the given constructor's table", function() {
var projection = relation.project(BlogPost);
@@ -255,6 +232,5 @@ Screw.Unit(function(c) { with(c) {
expect(difference.rightOperand).to(eq, rightOperand);
});
});
-
});
}});
View
6 spec/monarch/model/relations/table_spec.js
@@ -169,12 +169,14 @@ Screw.Unit(function(c) { with(c) {
it("triggers dirty and clean events at the appropriate times", function() {
var record = User.createFromRemote({id: 1, fullName: "Nathan Sobo"})
+ var sortKey User.table.buildSortKey(record);
+
record.fullName("Mahatma Ghandi");
- expect(dirtyCallback).to(haveBeenCalled, withArgs(record));
+ expect(dirtyCallback).to(haveBeenCalled, withArgs(record, 0, sortKey, sortKey));
record.fullName("Nathan Sobo");
- expect(cleanCallback).to(haveBeenCalled, withArgs(record));
+ expect(cleanCallback).to(haveBeenCalled, withArgs(record, 0, sortKey, sortKey));
});
it("triggers invalid and valid events at the appropriate times", function() {
Please sign in to comment.
Something went wrong with that request. Please try again.