added radar, cartesian quadrants and cycles retrieval
authorBruno Trecenti <btrecent@thoughtworks.com>
Tue, 25 Feb 2014 13:24:06 +0000 (10:24 -0300)
committerBruno Trecenti <btrecent@thoughtworks.com>
Tue, 25 Feb 2014 13:24:06 +0000 (10:24 -0300)
src/models/radar.js [new file with mode: 0644]
test/models/radar-spec.js [new file with mode: 0644]

diff --git a/src/models/radar.js b/src/models/radar.js
new file mode 100644 (file)
index 0000000..7e48b63
--- /dev/null
@@ -0,0 +1,65 @@
+tr.models.Radar = function() {
+  var self, quadrants;
+
+  quadrants = { I: null, II: null, III: null, IV: null };
+  self = {};
+
+  self.setFirstQuadrant = function (quadrant) {
+    quadrants.I = quadrant;
+  };
+
+  self.setSecondQuadrant = function (quadrant) {
+    quadrants.II = quadrant;
+  };
+
+  self.setThirdQuadrant = function (quadrant) {
+    quadrants.III = quadrant;
+  };
+
+  self.setFourthQuadrant = function (quadrant) {
+    quadrants.IV = quadrant;
+  };
+
+  function allQuadrants() {
+    var all = [];
+
+    for (var p in quadrants) {
+      if (quadrants.hasOwnProperty(p) && quadrants[p] != null) {
+        all.push(quadrants[p]);
+      }
+    }
+
+    return all;
+  }
+
+  function allBlips() {
+    return allQuadrants().reduce(function (blips, quadrant) {
+      return blips.concat(quadrant.blips());
+    }, []);
+  }
+
+  self.cycles = function () {
+    var cycleHash, cycleArray;
+
+    cycleArray = [];
+    cycleHash = {};
+
+    allBlips().forEach(function (blip) {
+      cycleHash[blip.cycle().name()] = blip.cycle();
+    });
+
+    for (var p in cycleHash) {
+      if (cycleHash.hasOwnProperty(p)) {
+        cycleArray.push(cycleHash[p]);
+      }
+    }
+
+    return cycleArray.slice(0);
+  };
+
+  self.quadrants = function () {
+    return quadrants;
+  };
+
+  return self;
+};
diff --git a/test/models/radar-spec.js b/test/models/radar-spec.js
new file mode 100644 (file)
index 0000000..8b565c7
--- /dev/null
@@ -0,0 +1,89 @@
+describe('tr.models.Radar', function () {
+
+  it('has no quadrants by default', function () {
+    radar = new tr.models.Radar();
+
+    expect(radar.quadrants().I).toBe(null);
+    expect(radar.quadrants().II).toBe(null);
+    expect(radar.quadrants().III).toBe(null);
+    expect(radar.quadrants().IV).toBe(null);
+  });
+
+  it('sets the first quadrant', function () {
+    var quadrant, radar;
+
+    quadrant = new tr.models.Quadrant('First');
+    radar = new tr.models.Radar();
+
+    radar.setFirstQuadrant(quadrant);
+
+    expect(radar.quadrants().I).toEqual(quadrant);
+  });
+
+  it('sets the second quadrant', function () {
+    var quadrant, radar;
+
+    quadrant = new tr.models.Quadrant('Second');
+    radar = new tr.models.Radar();
+
+    radar.setSecondQuadrant(quadrant);
+
+    expect(radar.quadrants().II).toEqual(quadrant);
+  });
+
+  it('sets the third quadrant', function () {
+    var quadrant, radar;
+
+    quadrant = new tr.models.Quadrant('Third');
+    radar = new tr.models.Radar();
+
+    radar.setThirdQuadrant(quadrant);
+
+    expect(radar.quadrants().III).toEqual(quadrant);
+  });
+
+  it('sets the fourth quadrant', function () {
+    var quadrant, radar;
+
+    quadrant = new tr.models.Quadrant('Fourth');
+    radar = new tr.models.Radar();
+
+    radar.setFourthQuadrant(quadrant);
+
+    expect(radar.quadrants().IV).toEqual(quadrant);
+  });
+
+  describe('cycles', function () {
+    var quadrant, radar, firstCycle, secondCycle;
+
+    beforeEach(function () {
+      firstCycle = new tr.models.Cycle('Adopt');
+      secondCycle = new tr.models.Cycle('Hold');
+      quadrant = new tr.models.Quadrant('Fourth');
+      radar = new tr.models.Radar();
+    });
+
+    it('returns an array for a given set of blips', function () {
+      quadrant.add([
+        new tr.models.Blip('A', firstCycle),
+        new tr.models.Blip('B', secondCycle)
+      ]);
+
+      radar.setFirstQuadrant(quadrant);
+
+      expect(radar.cycles()).toEqual([firstCycle, secondCycle]);
+    });
+
+    it('has unique cycles', function () {
+      quadrant.add([
+        new tr.models.Blip('A', firstCycle),
+        new tr.models.Blip('B', firstCycle),
+        new tr.models.Blip('C', secondCycle)
+      ]);
+
+      radar.setFirstQuadrant(quadrant);
+
+      expect(radar.cycles()).toEqual([firstCycle, secondCycle]);
+    });
+  });
+});